8.非关系型数据库(Nosql)之mongodb的应用场景(关系型数据库 和 Mongodb进行CRUD时数据用时的比较)
測試腳本:
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)."秒";
?>
本測試原則:如果比較結果相近,則擴大數量級。如比較結差距大,則采用最小數量級。
1.測試插入:
單條數據操作
時間:
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有優勢。
2.測試查詢:
單條數據操作
時間:
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倍。
3.測試更新:
測試100萬數據之前操作:
Mysql??? 響應時間為:0.00034689903259277秒MongoDB響應時間為:0.00021195411682129秒
測試100萬數據之后操作:
Mysql??? 響應時間為:0.00043201446533203秒
MongoDB響應時間為:0.0011470317840576秒
分析:
100萬數據之后,Mysql在更新方面要比MongoDB的速度快3倍。
4.測試刪除:
單條刪除操作:
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擁有處理大型數據的能力。
總結
以上是生活随笔為你收集整理的8.非关系型数据库(Nosql)之mongodb的应用场景(关系型数据库 和 Mongodb进行CRUD时数据用时的比较)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 国内知名的油罐车生产厂家有哪些?楚胜汽车
- 下一篇: 什么是汽车?