各种数据库性能比较(初步)

// 最近稍微有点时间 测试下nosql(reids,mongdb)和mysql性能问题

// 现在用的最普遍的redis非关系型数据库

// 循环十万次和一百万次消耗时间(最简单的数据插入):

 $redis=new Redis();

 $redis->connect('127.0.0.1',6379); // 本机IP等(先安装redis服务及dll拓展)

 $redis->auth('123456'); // 密码

 

 $StartTime = microtime(true);  

 for($i=1;$i<= 1000001;$i++){

    $redis->set($i,$i);

 }

扫描二维码关注公众号,回复: 320216 查看本文章

 $StopTime = microtime(true);  

 $TimeSpent=$StopTime-$StartTime;

 echo number_format($TimeSpent*1000, 4).'毫秒';  

 // 十万次 100001  8,050.7162毫秒

 // 一百万次 1000001  78,719.3971毫秒



 

// mysql批量插入的效率如下:

$link = mysql_connect('127.0.0.1','root',''); 

if (!$link) { 

die('Could not connect to MySQL: ' . mysql_error()); 

}

mysql_select_db('test');

$StartTime = microtime(true);  

$str = '';

for($i=1;$i<=100000;$i++){

$str .= '('.$i.'),';

}

$sql1 ="insert into test(val) values".$str;

$sql2 = rtrim($sql1, ",").';';  // 拼接sql字符串太长可能不太好

mysql_query($sql2,$link); 

/*

for($i=1;$i<=1000000;$i++){

$sql1 ="insert into test(val) values($i)";

mysql_query($sql1,$link); 

}*/

$StopTime = microtime(true);

$TimeSpent=$StopTime-$StartTime;

echo number_format($TimeSpent*1000, 4).'毫秒';  

exit;

// sql值拼接的情况下

// 100000 => 1,960.1121毫秒/1,662.9341毫秒

// 10000000 => Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 98888926 bytes) in D:\wamp_php\wamp\www\testmysql.php on line 14

// 5000000 => Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 48888924 bytes) in D:\wamp_php\wamp\www\testmysql.php on line 15

// 3000000 => Warning: Error while sending QUERY packet. PID=2928 in D:\wamp_php\wamp\www\testmysql.php on line 16

// 1000000 => 

//Warning: mysql_query(): MySQL server has gone away in D:\wamp_php\wamp\www\testmysql.php on line 16

//Warning: mysql_query(): Error reading result set's header in D:\wamp_php\wamp\www\testmysql.php on line 16

// 循环

// 1000000 实际插入表 1627条 耗时122秒03毫秒

由此可见:mysql大数据量的插入数据拼接形式的效率远大于循环插入的效率,不过我自己的笔记本可能配置久和低,实际在服务器的效率应该会高很多的。

 

 mongdb批量插入效率:在3月份的时候在公司电脑用mongdb插入1000万条记录(含ID,name,age三个字段),用时43分钟不到一小时,这个效率应该和redis相差不大;

 

总结:非关系型数据库效率要比关系型数据库效率 高很多。。。

猜你喜欢

转载自chenhaibo0806999.iteye.com/blog/2292944
今日推荐