测试脚本:
Mysql测试脚本:
[php] view plaincopyprint?
1. <?php
2. header("Content-Type:text/html;charset=utf-8");
3. $con = mysql_connect("localhost","root","123456");
4. if (!$con)
5. {
6. die('Could not connect: ' . mysql_error());
7. }
8. mysql_select_db("my_test", $con);
9. mysql_query("set names utf8");
10. $time1 = xdebug_time_index();
11.
12.
13. //测试单条插入
14. for($i=1;$i<2;$i++){
15. mysql_query('INSERT INTO `members_copy`(`uname`,`name`,`password`,`email`) VALUES("chuchuchu_'.$i.'","褚褚褚","e10adc3949ba59abbe56e057f20f883e","dhaig@yahoo.com.cn")');
16. }
17.
18.
19. //测试单条查询
20. $result = mysql_query("select * from members_copy where id=1");
21. //while($row = mysql_fetch_row($result)){
22. // print_r($row);
23. //}
24.
25.
26. //测试更新
27. mysql_query("UPDATE `members` SET `uname`='chuchuchu_1',`name`='褚褚褚',`password`='e10adc3949ba59abbe56e057f20f883e',`email`='dhaig@yahoo.com.cn' WHERE `id`='1'");
28.
29.
30.
31.
32.
33. //测试删除
34. mysql_query("DELETE FROM `members` WHERE `id`='1';");
35.
36.
37. //测试100万条数据插入
38. for($i=1;$i<1000000;$i++){
39. mysql_query('INSERT INTO `members`(`uname`,`name`,`password`,`email`) VALUES("chuchuchu_'.$i.'","褚褚褚","e10adc3949ba59abbe56e057f20f883e","dhaig@yahoo.com.cn")');
40. }
41.
42. //测试100万数据之单条插入
43. mysql_query('INSERT INTO `members`(`uname`,`name`,`password`,`email`) VALUES("chuchuchu_0","褚褚褚","e10adc3949ba59abbe56e057f20f883e","dhaig@yahoo.com.cn")');
44.
45. //测试100万数据之单条查询
46. $result = mysql_query("select * from members limit 0");
47. while($row = mysql_fetch_row($result)){
48. print_r($row);
49. }
50.
51. //测试100万数据之单条更新
52. mysql_query("UPDATE `members` SET `uname`='chuchuchu_1',`name`='褚褚褚',`password`='e10adc3949ba59abbe56e057f20f883e',`email`='dhaig@yahoo.com.cn' WHERE `id`='1'");
53.
54. //测试100万数据之单条删除
55. mysql_query("DELETE FROM `members` WHERE `id`='1';");
56.
57.
58. mysql_close($con);
59. $time2 = xdebug_time_index();
60. echo "Mysql 响应时间为:".($time2-$time1)."秒";
61. ?>
<?php
header("Content-Type:text/html;charset=utf-8");
$con =mysql_connect("localhost","root","123456");
if(!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_test",$con);
mysql_query("setnames utf8");
$time1= xdebug_time_index();
//测试单条插入
for($i=1;$i<2;$i++){
mysql_query('INSERTINTO `members_copy`(`uname`,`name`,`password`,`email`) VALUES("chuchuchu_'.$i.'","褚褚褚","e10adc3949ba59abbe56e057f20f883e","dhaig@yahoo.com.cn")');
}
//测试单条查询
$result= mysql_query("select * from members_copy where id=1");
//while($row= mysql_fetch_row($result)){
// print_r($row);
//}
//测试更新
mysql_query("UPDATE`members` SET `uname`='chuchuchu_1',`name`='褚褚褚',`password`='e10adc3949ba59abbe56e057f20f883e',`email`='dhaig@yahoo.com.cn'WHERE `id`='1'");
//测试删除
mysql_query("DELETEFROM `members` WHERE `id`='1';");
//测试100万条数据插入
for($i=1;$i<1000000;$i++){
mysql_query('INSERTINTO `members`(`uname`,`name`,`password`,`email`)VALUES("chuchuchu_'.$i.'","褚褚褚","e10adc3949ba59abbe56e057f20f883e","dhaig@yahoo.com.cn")');
}
//测试100万数据之单条插入
mysql_query('INSERTINTO `members`(`uname`,`name`,`password`,`email`)VALUES("chuchuchu_0","褚褚褚","e10adc3949ba59abbe56e057f20f883e","dhaig@yahoo.com.cn")');
//测试100万数据之单条查询
$result= mysql_query("select * from members limit 0");
while($row= mysql_fetch_row($result)){
print_r($row);
}
//测试100万数据之单条更新
mysql_query("UPDATE`members` SET `uname`='chuchuchu_1',`name`='褚褚褚',`password`='e10adc3949ba59abbe56e057f20f883e',`email`='dhaig@yahoo.com.cn'WHERE `id`='1'");
//测试100万数据之单条删除
mysql_query("DELETEFROM `members` WHERE `id`='1';");
mysql_close($con);
$time2= xdebug_time_index();
echo"Mysql 响应时间为:".($time2-$time1)."秒";
?>
MongoDB测试脚本:
[php] view plaincopyprint?
1. <?php
2. header("Content-Type:text/html;charset=utf-8");
3. //MongoDB有用户名密码并指定数据库admin
4. $conn = new Mongo("mongodb://root:123456@127.0.0.1:27017/admin");
5.
6. $db = $conn->admin;
7. //定制结果集(表名things)
8. $collection = $db->members;
9. $time1 = xdebug_time_index();
10.
11.
12. //测试单条插入
13. for($i=1;$i<2;$i++){
14. $user = array('uname' => 'chuchuchu_'.$i, 'name' => '褚褚褚', 'password' => 'e10adc3949ba59abbe56e057f20f883e', 'email' => 'dhaig@yahoo.com.cn');
15. $collection->insert($user);
16. }
17.
18. //测试单条查询
19. $cursor = $collection->find()->limit(1);
20. //while($cursor->hasNext())
21. //{
22. // var_dump($cursor->getNext());
23. //}
24.
25. //测试更新
26. $newdata = array('$set' => array("email" => "test@test.com"));
27. $collection->update(array("uname" => "chuchuchu_1"), $newdata);
28.
29.
30. //测试删除
31. $collection->remove(array('email'=>'dhaig@yahoo.com.cn'), array("justOne" => true));
32.
33. //测试100万条数据插入
34.
35. for($i=1;$i<1000000;$i++){
36. $user = array('uname' => 'chuchuchu_'.$i, 'name' => '褚褚褚', 'password' => 'e10adc3949ba59abbe56e057f20f883e', 'email' => 'dhaig@yahoo.com.cn');
37. $collection->insert($user);
38. }
39.
40. //测试100万数据之单条插入
41. $user = array('uname' => 'chuchuchu_0', 'name' => '褚褚褚', 'password' => 'e10adc3949ba59abbe56e057f20f883e', 'email' => 'dhaig@yahoo.com.cn');
42. $collection->insert($user);
43.
44. //测试100万数据之单条查询
45. $user = $collection->findOne(array('uname' => 'chuchuchu_0'));
46. var_dump($user);
47.
48. //测试100万数据之单条更新
49. $newdata = array('$set' => array("email" => "test@test.com"));
50. $collection->update(array("uname" => "chuchuchu_0"), $newdata);
51. var_dump($user);
52.
53. //测试100万数据之单条删除
54. $collection->remove(array('uname'=>'chuchuchu_0'), array("justOne" => true));
55.
56.
57. $conn->close();
58. $time2 = xdebug_time_index();
59. echo "MongoDB响应时间为:".($time2-$time1)."秒";
60. ?>
<?php
header("Content-Type:text/html;charset=utf-8");
//MongoDB有用户名密码并指定数据库admin
$conn =new Mongo("mongodb://root:123456@127.0.0.1:27017/admin");
$db =$conn->admin;
//定制结果集(表名things)
$collection= $db->members;
$time1= xdebug_time_index();
//测试单条插入
for($i=1;$i<2;$i++){
$user =array('uname' => 'chuchuchu_'.$i, 'name' => '褚褚褚', 'password' =>'e10adc3949ba59abbe56e057f20f883e', 'email' => 'dhaig@yahoo.com.cn');
$collection->insert($user);
}
//测试单条查询
$cursor= $collection->find()->limit(1);
//while($cursor->hasNext())
//{
// var_dump($cursor->getNext());
//}
//测试更新
$newdata= array('$set' => array("email" => "test@test.com"));
$collection->update(array("uname"=> "chuchuchu_1"), $newdata);
//测试删除
$collection->remove(array('email'=>'dhaig@yahoo.com.cn'),array("justOne" => true));
//测试100万条数据插入
for($i=1;$i<1000000;$i++){
$user =array('uname' => 'chuchuchu_'.$i, 'name' => '褚褚褚', 'password' =>'e10adc3949ba59abbe56e057f20f883e', 'email' => 'dhaig@yahoo.com.cn');
$collection->insert($user);
}
//测试100万数据之单条插入
$user =array('uname' => 'chuchuchu_0', 'name' => '褚褚褚', 'password' =>'e10adc3949ba59abbe56e057f20f883e', 'email' => 'dhaig@yahoo.com.cn');
$collection->insert($user);
//测试100万数据之单条查询
$user =$collection->findOne(array('uname' => 'chuchuchu_0'));
var_dump($user);
//测试100万数据之单条更新
$newdata= array('$set' => array("email" =>"test@test.com"));
$collection->update(array("uname"=> "chuchuchu_0"), $newdata);
var_dump($user);
//测试100万数据之单条删除
$collection->remove(array('uname'=>'chuchuchu_0'),array("justOne" => true));
$conn->close();
$time2= xdebug_time_index();
echo"MongoDB响应时间为:".($time2-$time1)."秒";
?>
本测试原则:如果比较结果相近,则扩大数量级。如比较结差距大,则采用最小数量级。
单条数据操作
时间:
Mysql 响应时间为:0.00045895576477051秒
MongoDB响应时间为:0.00031495094299316秒
100条数据操作
Mysql 响应时间为:0.014914989471436秒
MongoDB响应时间为:0.010399103164673秒
1000条数据操作
Mysql 响应时间为:0.17900490760803秒
MongoDB响应时间为:0.096189975738525秒
100万条数据操作Mysql 响应时间为:168.32936501503秒
MongoDB响应时间为:87.314424991608秒
测试100万数据之后单条插入:
Mysql 响应时间为:0.00042891502380371秒
MongoDB响应时间为:0.00025105476379395秒
分析:
在查询方面数量级越大相应时间差距越大。100万数据测试中mongo要比mysql至少快2倍。MongoDB要比Mysql有优势。
单条数据操作
时间:
Mysql 响应时间为:0.00082182884216309秒
MongoDB响应时间为:0.00055313110351562秒
100条数据操作
Mysql 响应时间为:0.00066590309143066秒
MongoDB响应时间为:0.00087094306945801秒
1000条数据操作
Mysql 响应时间为:0.002295970916748秒
MongoDB响应时间为:0.00048995018005371秒
测试100万数据之后单条查询:
Mysql 响应时间为:0.0011050701141357秒
MongoDB响应时间为:0.00045204162597656秒
分析:
在测试中我们发现,当100条以内查询时mysql优于mongodb但是当操作数据100万后mongodb要比mysql快至少3倍。
测试100万数据之前操作:
Mysql 响应时间为:0.00034689903259277秒MongoDB响应时间为:0.00021195411682129秒
测试100万数据之后操作:
Mysql 响应时间为:0.00043201446533203秒
MongoDB响应时间为:0.0011470317840576秒
分析:
100万数据之后,Mysql在更新方面要比MongoDB的速度快3倍。
单条删除操作:
Mysql 响应时间为:0.00081205368041992秒MongoDB响应时间为:0.00023102760314941秒
多条删除操作:Mysql
响应时间为:0.00092816352844238秒
MongoDB响应时间为:0.0092201232910156秒
测试100万数据之后单条删除操作:
Mysql 响应时间为:0.00066685676574707秒
MongoDB响应时间为:0.0011069774627686秒
分析:
100万数据之后,Mysql在更新方面要比MongoDB的速度快2倍。
总结:MongoDB在数据插入及查询上性能表现优异,MongoDB拥有处理大型数据的能力。