迭代一个有10000行的mysql结果集,耗时3.5秒.这正常吗?
Iterate a mysql resultset which has 10000 rows, it takes 3.5 seconds. Is it normal?
我使用c++和mysql++来获取结果集,并使用fetch_row()进行迭代
结果集有10000行,这个"While"循环花费了将近3.5秒。(我已经注释掉了while循环中的内容。)
这种情况正常吗?我认为它应该完成得更快!
Connection* conn = ConnPool::getSingletonPtr()->getConn();
Query qr = conn->query(sql);
SYSTEMTIME lpsystime;
GetLocalTime(&lpsystime);
UseQueryResult res = qr.use();
while(FryRow row = res.fetch_row())
{
/*
MyObject obj;
for(int i=0; i<row.size(); i++)
{
obj.setValue(res.fetch_field(i).name(), row[i]);
}
objList->push_back(obj);
*/
}
GetLocalTime(&lpsystime);
MyObject有属性:
int procedureNo;
int index;
int employeeNo;
int procCount;
int state;
int procPermission;
int procDeadline;
int advanceAlert;
DateTime procTime;
int resultFlag;
string comment;
int flowDirection;
int isHideComment;
int isTrack;
DateTime arriveTime;
string preNodesJsonStr;
string nextNodesJsonStr;
string attachStr;
string employeeName;
DateTime employeeBirthDay;
************************** 分支器 *****************************谢谢大家!我修改了代码,并像这样再次测量时间:
Connection* conn = ConnPool::getSingletonPtr()->getConn();
Query qr = conn->query(sql);
SYSTEMTIME lpsystime;
GetLocalTime(&lpsystime); // get the time before use().
UseQueryResult res = qr.use();
GetLocalTime(&lpsystime); // get the time before While loop.
while(FryRow row = res.fetch_row())
{
/*
MyObject obj;
for(int i=0; i<row.size(); i++)
{
obj.setValue(res.fetch_field(i).name(), row[i]);
}
objList->push_back(obj);
*/
}
GetLocalTime(&lpsystime); // get the time when While loop finished.
use()函数只花费10ms。
While循环耗时1.677秒。
如果我没有注释掉while循环中的内容。
setValue()函数定义如下:
MyObject::setValue(const char * colName, const mysqlpp::String& ele)
{
if(strcmp(colName,"column010") == 0)
procedureNo = ele;
else if(strcmp(colName,"column020") == 0)
index = ele;
else if(strcmp(colName,"column030") == 0)
employeeNo = ele;
else if(strcmp(colName,"column040") == 0)
procCount = ele;
else if(strcmp(colName,"column050") == 0)
state = ele;
else if(strcmp(colName,"column060") == 0)
procPermission = ele;
else if(strcmp(colName,"column070") == 0)
procDeadline = ele;
else if(strcmp(colName,"column080") == 0)
advanceAlert = ele;
else if(strcmp(colName,"column090") == 0)
procTime = ele;
else if(strcmp(colName,"column100") == 0)
resultFlag = ele;
else if(strcmp(colName,"column110") == 0)
comment = ele;
else if(strcmp(colName,"column120") == 0)
flowDirection = ele;
else if(strcmp(colName,"column130") == 0)
isHideComment = ele;
else if(strcmp(colName,"column140") == 0)
isTrack = ele;
else if(strcmp(colName,"column150") == 0)
arriveTime = ele;
else if(strcmp(colName,"column160") == 0)
preNodesJsonStr = ele;
else if(strcmp(colName,"column170") == 0)
nextNodesJsonStr = ele;
else if(strcmp(colName,"column180") == 0)
attachStr = ele;
else if(strcmp(colName,"column190") == 0)
employeeName = ele;
else if(strcmp(colName,"column200") == 0)
employeeBirthDay = ele;
}
您的问题是您还测量了查询的执行时间。conn->query(sql);
不执行查询。它只是构建一个Query
对象。然后use
方法实际执行查询。例如,在您的代码中,这一行实际上执行了查询:
UseQueryResult res = qr.use();
来自mysql++文档:
UseQueryResult mysqlpp::Query::use ()
执行一个可以返回行并按顺序访问行的查询。使用Use()中的一个如果内存效率很重要,则会过载。它们返回一个对象它可以逐个遍历结果记录,而不需要获取来自服务器的整个结果集。
但是请注意,即使您取出use
调用,您仍然可能在循环中获得一些查询执行时间,因为use
逐个提取行。因此,数据库一个元组一个元组地执行查询,并且只在从结果中取出下一个元组时才计算下一个元组。如果您真的想通过所有的结果来测量一个循环,那么您必须使用store
函数。它彻底执行查询并将结果存储在一块内存中。然后,你的循环会非常快。但是,您仍然应该选择use
,因为首先将所有结果存储到内存中通常只是浪费时间和内存。
相关文章:
- 自一周开始以来的秒数?
- Gstreamer:每 5 秒使用多文件墨水保存图像/jpeg
- C++每 x 秒调用一次函数
- 如何在C++中将 chrono::秒转换为 HH:MM:SS 格式的字符串?
- Poco::Net::FTPClientSession 在 open() 方法上挂起 129 秒,如果 ftp 主机不存
- 计算过去 60 秒的最低和最高温度
- 如何找到两个日期之间的时间差异(以秒和纳秒为单位)?
- std::chrono::d uration 可以按秒初始化,但不能按毫秒初始化?
- OpenCL 内核计时测量 0 秒或导致 SIGABRT
- C++进程在 4.017 秒后退出,返回值3221225477
- 如何在C++中将秒更改为 HH:MM:SS 格式?
- 如何制作每秒从 30 乘 1 倒计时的计时器?
- 纳秒级C++程序空闲/节流
- 在 1.5 秒内找到 3 到 4 个不同整数的中位数超过 2000 万
- 从"LLONG_MAX 秒"构造 std::chrono::毫秒变量时发生了什么?
- 如何在C++中进行耗时的三重组合
- 测量精确的时间,单位为纳秒C++
- 从午夜开始以最低的延迟获得纳秒
- 迭代一个有10000行的mysql结果集,耗时3.5秒.这正常吗?
- 为什么排序算法耗时零秒