自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MongoDB 1000W級數(shù)據(jù)Insert和Query性能測試

數(shù)據(jù)庫 其他數(shù)據(jù)庫 MongoDB
在1000W的數(shù)量級,對MongoDB進(jìn)行Insert和Query的性能測試,分為添加索引和不添加索引,結(jié)果會是如何呢?拭目以待吧!

先看下測試機(jī)性能(64bit):

  1. ^_^[root@:~]#grep "model name" /proc/cpuinfo | cut -f2 -d: 
  2.  Intel(R) Xeon(R) CPU           E5606  @ 2.13GHz 
  3.  Intel(R) Xeon(R) CPU           E5606  @ 2.13GHz 
  4.  Intel(R) Xeon(R) CPU           E5606  @ 2.13GHz 
  5.  Intel(R) Xeon(R) CPU           E5606  @ 2.13GHz 
  6.  Intel(R) Xeon(R) CPU           E5606  @ 2.13GHz 
  7.  Intel(R) Xeon(R) CPU           E5606  @ 2.13GHz 
  8.  Intel(R) Xeon(R) CPU           E5606  @ 2.13GHz 
  9.  Intel(R) Xeon(R) CPU           E5606  @ 2.13GHz 
  10. ^_^[root@:~]#grep MemTotal /proc/meminfo 
  11. MemTotal:      4040580 kB 
  12. ^_^[root@:~]# free -m 
  13.              total       used       free     shared    buffers     cached 
  14. Mem:          3945       3715        230          0         40       2626 
  15. -/+ buffers/cache:       1048       2897 
  16. Swap:         4094          2       4092 
  17. ^_^[root@:~]#getconf LONG_BIT 
  18. 64 
  19. ^_^[root@:~]#more /etc/redhat-release 
  20. Red Hat Enterprise Linux Server release 5.5 (Tikanga) 
  21. ^_^[root@:~]#uname -r 
  22. 2.6.18-194.el5 

測試程序:

  1. #include <iostream> 
  2. #include <mongo/client/dbclient.h> 
  3. using namespace std; 
  4. using namespace mongo; 
  5.  
  6. #define INIT_TIME \ 
  7.         struct timeval time1,time2; \ 
  8.  
  9. #define START_TIME \ 
  10.         gettimeofday(&time1,NULL); \ 
  11.  
  12. #define STOP_TIME \ 
  13.         gettimeofday(&time2,NULL); \ 
  14.  
  15. #define PRINT_TIME \ 
  16.         cout<<"Time:"<<time2.tv_sec-time1.tv_sec<<":"<<time2.tv_usec-time1.tv_usec<<endl; 
  17.  
  18. int main() { 
  19.         srand(time(NULL)); 
  20.         char ar[26+1]; 
  21.         DBClientConnection conn; 
  22.         conn.connect("localhost"); 
  23.         cout<<"MongoDB Connected OK!"<<endl; 
  24.         int count=10000000; 
  25.         INIT_TIME; 
  26.         START_TIME; 
  27. //insert 
  28. #if 1 
  29.         while (count--) { 
  30.                 for (int i=0; i<26; i++) { 
  31.                         ar[i] = rand()%26+97; 
  32.                 } 
  33.                 ar[26]='\0'
  34.                 BSONObj p = BSON("NewsId"<<ar); 
  35.                 conn.insert("test.users",p); 
  36.         } 
  37. #endif 
  38.  
  39.         STOP_TIME; 
  40.         PRINT_TIME; 
  41.         return 0; 

不加索引測試:

......................# MongoDB 不加索引 插入1000W條測試 #...................

  1. ^_^[root@:~/svn/nugget/MongoDB/utest]#./insertData               
  2. MongoDB Connected OK! 
  3. Time:207s:194125μs 

......................# MongoDB 不加索引 1000W條遍歷測試 #...................

我們讓MongoDB全部遍歷一遍:

讓測試數(shù)據(jù)倒序,取倒序后第一條數(shù)據(jù):

  1. > db.users.find().sort({'_id':-1}) 
  2. "_id" : ObjectId("4e2cbdf4a1ca039d82214e33"), "NewsId" : "dgvshdhevmjgunvbepgdkzirqk" } 

第一條數(shù)據(jù)的NewsId為 dgvshdhevmjgunvbepgdkzirqk

測試程序:

  1. ^_^[root@:/usr/local/mongodb/bin]#./mongo<bat.js  
  2. MongoDB shell version: 1.8.2 
  3. connecting to: test 
  4. > var startTime = new Date(); 
  5. >  
  6. > db.users.find({NewsId:"dgvshdhevmjgunvbepgdkzirqk"}); 
  7. "_id" : ObjectId("4e2ccfd2a1ca039d82527b34"), "NewsId" : "dgvshdhevmjgunvbepgdkzirqk" } 
  8. >  
  9. > (new Date().getTime()-startTime.getTime())/1000 
  10. 5.846s 
  11. > bye 

#p#

加索引測試:

  1. > db.users.getIndexes() 
  2.         { 
  3.                 "name" : "_id_"
  4.                 "ns" : "test.users"
  5.                 "key" : { 
  6.                         "_id" : 1 
  7.                 }, 
  8.                 "v" : 0 
  9.         } 
  10. > db.users.ensureIndex({NewsId:1}) 
  11. > db.users.getIndexes()            
  12.         { 
  13.                 "name" : "_id_"
  14.                 "ns" : "test.users"
  15.                 "key" : { 
  16.                         "_id" : 1 
  17.                 }, 
  18.                 "v" : 0 
  19.         }, 
  20.         { 
  21.                 "_id" : ObjectId("4e2cc408572ff09d98851cb7"), 
  22.                 "ns" : "test.users"
  23.                 "key" : { 
  24.                         "NewsId" : 1 
  25.                 }, 
  26.                 "name" : "NewsId_1"
  27.                 "v" : 0 
  28.         } 

......................# MongoDB 加索引 插入1000W條測試 #...................

  1. ^_^[root@:~/svn/nugget/MongoDB/utest]#./insertData  
  2. MongoDB Connected OK! 
  3. Time:2019s:19419μs 

......................# MongoDB 加索引 1000W條測試遍歷測試 #...................

還是一樣取最后一條數(shù)據(jù),然后看下性能:

  1. ^_^[root@:/usr/local/mongodb/bin]#./mongo <bat.js  
  2. MongoDB shell version: 1.8.2 
  3. connecting to: test 
  4. > var startTime = new Date(); 
  5. >  
  6. > db.users.find({NewsId:"nxuvdqtjrrptoyildolesbkqmd"}); 
  7. "_id" : ObjectId("4e2ccc2ea1ca039d82b9e4b3"), "NewsId" : "nxuvdqtjrrptoyildolesbkqmd" } 
  8. >  
  9. > (new Date().getTime()-startTime.getTime())/1000 
  10. 0.022s 
  11. > bye 

總結(jié)下測試數(shù)據(jù)吧:

沒索引

添加 1 千萬 記錄 Time:207s:194125μs

查詢 5.846s

有索引

添加 1 千萬 記錄 Time:2019s:19419μs

查詢 0.022s

原文鏈接:http://blog.csdn.net/crazyjixiang/article/details/6630624

 

【編輯推薦】

  1. 教你如何利用MySQL學(xué)習(xí)MongoDB
  2. 說說MongoDB的基礎(chǔ)
  3. 如何用Java操作MongoDB
  4. MongoDB基于Java、PHP的一般操作和用戶安全設(shè)置
  5. 設(shè)計實(shí)例對比:MySQL vs MongoDB
責(zé)任編輯:艾婧 來源: Crazybaby's blog
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號