0

我使用 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() 函数仅需 10 毫秒。

ant While 循环花费 1.677 秒。

如果我不注释掉 while 循环中的内容。它花费 3.386 秒。

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;
}
4

1 回答 1

2

您的问题是您还测量了查询的执行时间。conn->query(sql);执行查询。它只是构建一个Query对象。然后该use方法实际执行查询。即,在您的代码中,这一行实际上执行了查询:

UseQueryResult res = qr.use();

来自 mysql++ 文档:

 UseQueryResult mysqlpp::Query::use ()  

执行一个可以返回行的查询,并按顺序访问这些行。如果内存效率很重要,请使用其中一个 use() 重载。它们返回一个对象,该对象可以逐个遍历结果记录,而无需从服务器获取整个结果集。

但请注意,即使您拉出use调用,您可能仍会在循环中获得一些查询执行时间,因为use会一一拉出行。因此,数据库按元组执行查询元组,并且仅在您从结果中获取下一个元组时才计算下一个元组。如果您真的想测量所有结果的循环,则必须改用该store函数。它彻底执行查询并将结果存储在一块内存中。然后,您的循环将非常快。但是,您仍然应该更喜欢use,因为首先将所有结果存储到内存中通常只是浪费时间和内存。

于 2014-07-08T07:45:32.993 回答