mysql 测试与mongodb 测试对比
网上有很多相关测试对比,但是与实际项目中性能相差很多,所以还是自己测试对比了一下.mongodb甩mysql很远啊.
mysql只有在批量操作下性能才接近mongodb,这样mysql就必须加个缓存服务器来配合使用了,但是在实际项目中要维护缓存服务器的话也是比较繁杂的事情.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
mongodb_test: mongodb2.4之前是没有批量操作的,最近的2.6新增加了批量操作Bulk,效率上应该会更快
mysql_test:
下面是使用批量操作: