mysql 测试与mongodb 测试对比
生活随笔
收集整理的這篇文章主要介紹了
mysql 测试与mongodb 测试对比
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
網上有很多相關測試對比,但是與實際項目中性能相差很多,所以還是自己測試對比了一下.mongodb甩mysql很遠啊.
mysql只有在批量操作下性能才接近mongodb,這樣mysql就必須加個緩存服務器來配合使用了,但是在實際項目中要維護緩存服務器的話也是比較繁雜的事情.
1 // mysql_test.cpp : 定義控制臺應用程序的入口點。 2 // 3 4 #include "stdafx.h" 5 #include <boost/timer.hpp> 6 #include <boost/lexical_cast.hpp> 7 #include <string> 8 9 #include <mysql_connection.h> 10 #include <mysql_driver.h> 11 #include <cppconn/statement.h> 12 #include <cppconn/exception.h> 13 14 using namespace sql::mysql; 15 using namespace boost; 16 using namespace std; 17 18 int _tmain(int argc, _TCHAR* argv[]) 19 { 20 MySQL_Driver md; 21 boost::shared_ptr<sql::Connection> con(md.connect("127.0.0.1","root","123456")); 22 if(!con->isClosed()) 23 { 24 try 25 { 26 int tcont = 1000; 27 printf("test count: %d\n", tcont); 28 string tname = "testdb"; 29 string strq = "insert "+tname+" values("; 30 con->setAutoCommit(true); 31 //con->setAutoCommit(false); 32 boost::shared_ptr<sql::Statement> ssm(con->createStatement()); 33 ssm->execute("use test;"); 34 boost::timer tt; 35 for (int i = 0; i<tcont;i++) 36 { 37 ssm->execute(strq + lexical_cast<string>(i) + ",\"test\");"); 38 } 39 //con->commit(); 40 printf("insert time: %lf(s)\n", tt.elapsed()); 41 42 strq = "update "+tname+" set _str = \"abcd\" where _id = "; 43 tt.restart(); 44 for (int i = 0; i<tcont;i++) 45 { 46 ssm->executeUpdate(strq + lexical_cast<string>(i)+";"); 47 } 48 //con->commit(); 49 printf("update time: %lf(s)\n", tt.elapsed()); 50 51 strq = "select * from "+tname+" where _id = "; 52 tt.restart(); 53 for (int i = 0; i<tcont;i++) 54 { 55 boost::shared_ptr<sql::ResultSet> rset(ssm->executeQuery(strq + lexical_cast<string>(i)+";")); 56 } 57 58 printf("find time: %lf(s)\n", tt.elapsed()); 59 60 strq = "delete from "+tname+" where _id = "; 61 tt.restart(); 62 for (int i = 0; i<tcont;i++) 63 { 64 ssm->execute(strq + lexical_cast<string>(i)+";"); 65 } 66 //con->commit(); 67 printf("remove time: %lf(s)\n", tt.elapsed()); 68 } 69 catch(sql::SQLException& e) 70 { 71 printf("error: %s\n", e.what()); 72 } 73 con->close(); 74 } 75 76 getchar(); 77 return 0; 78 } mysql_test.cppmongodb_test: mongodb2.4之前是沒有批量操作的,最近的2.6新增加了批量操作Bulk,效率上應該會更快
mysql_test:
下面是使用批量操作:
轉載于:https://www.cnblogs.com/zhangchengxin/p/3644916.html
總結
以上是生活随笔為你收集整理的mysql 测试与mongodb 测试对比的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mac下安装mongodb
- 下一篇: linux cmake编译源码,linu