您的位置:首页 > 数据库 > Mongodb

mysql 测试与mongodb 测试对比

2014-04-04 12:30 337 查看
网上有很多相关测试对比,但是与实际项目中性能相差很多,所以还是自己测试对比了一下.mongodb甩mysql很远啊.

mysql只有在批量操作下性能才接近mongodb,这样mysql就必须加个缓存服务器来配合使用了,但是在实际项目中要维护缓存服务器的话也是比较繁杂的事情.

// mysql_test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <boost/timer.hpp>
#include <boost/lexical_cast.hpp>
#include <string>

#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/statement.h>
#include <cppconn/exception.h>

using namespace sql::mysql;
using namespace boost;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
MySQL_Driver md;
boost::shared_ptr<sql::Connection> con(md.connect("127.0.0.1","root","123456"));
if(!con->isClosed())
{
try
{
int tcont = 1000;
printf("test count: %d\n", tcont);
string tname = "testdb";
string strq = "insert "+tname+" values(";
con->setAutoCommit(true);
//con->setAutoCommit(false);
boost::shared_ptr<sql::Statement> ssm(con->createStatement());
ssm->execute("use test;");
boost::timer tt;
for (int i = 0; i<tcont;i++)
{
ssm->execute(strq + lexical_cast<string>(i) + ",\"test\");");
}
//con->commit();
printf("insert time: %lf(s)\n", tt.elapsed());

strq = "update "+tname+" set _str = \"abcd\" where _id = ";
tt.restart();
for (int i = 0; i<tcont;i++)
{
ssm->executeUpdate(strq + lexical_cast<string>(i)+";");
}
//con->commit();
printf("update time: %lf(s)\n", tt.elapsed());

strq = "select * from "+tname+" where _id = ";
tt.restart();
for (int i = 0; i<tcont;i++)
{
boost::shared_ptr<sql::ResultSet> rset(ssm->executeQuery(strq + lexical_cast<string>(i)+";"));
}

printf("find time: %lf(s)\n", tt.elapsed());

strq = "delete from "+tname+" where _id = ";
tt.restart();
for (int i = 0; i<tcont;i++)
{
ssm->execute(strq + lexical_cast<string>(i)+";");
}
//con->commit();
printf("remove time: %lf(s)\n", tt.elapsed());
}
catch(sql::SQLException& e)
{
printf("error: %s\n", e.what());
}
con->close();
}

getchar();
return 0;
}


mysql_test.cpp
mongodb_test: mongodb2.4之前是没有批量操作的,最近的2.6新增加了批量操作Bulk,效率上应该会更快



mysql_test:



下面是使用批量操作:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: