简体   繁体   English

迭代具有10000行的mysql结果集,这需要3.5秒。 正常吗

[英]Iterate a mysql resultset which has 10000 rows, it takes 3.5 seconds. Is it normal?

I use C++ and mysql++ to get a resultset and iterate it with fetch_row() 我使用C ++和mysql ++获取结果集,并使用fetch_row()对其进行迭代

The resultset has 10000 rows, this "While" Loop took nearly 3.5 seconds.(I have comment out the content in while loop.) 结果集有10000行,此“ While”循环花了将近3.5秒。(我已注释了while循环中的内容。)

is this situation normal? 这种情况正常吗? I thought it should completed much faster! 我认为应该更快完成!

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 has properties: 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;

**************************Spliter***************************** Thank you guys! **************************分散*********************** ******谢谢大家! I modified the code and measured the time again like this: 我修改了代码并再次测量时间,如下所示:

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.

the use() function only costs 10 ms. use()函数仅花费10毫秒。

ant the While loop costs 1.677 seconds. 蚂蚁While循环耗时1.677秒。

If I don't comment out the content in while Loop. 如果我不注释掉while循环中的内容。 It costs 3.386 seconds. 耗时3.386秒。

The setValue() function is defined as below: 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;
}

Your problem is that you also measure the execution time of your query. 您的问题是您还测量了查询的执行时间。 conn->query(sql); does not execute the query. 执行查询。 It just builds a Query object. 它只是建立一个Query对象。 The use method then actually executes the query. 然后, use方法实际上执行查询。 Ie, in your code, this line actually executes the query: 即,在您的代码中,此行实际执行查询:

UseQueryResult res = qr.use();

From the mysql++ documentation: 从mysql ++文档中:

 UseQueryResult mysqlpp::Query::use ()  

Execute a query that can return rows, with access to the rows in sequence.Use one of the use() overloads if memory efficiency is important. 执行查询以返回行并按顺序访问行。如果内存效率很重要,请使用use()重载之一。 They return an object that can walk through the result records one by one, without fetching the entire result set from the server. 它们返回一个对象,该对象可以一个一个地遍历结果记录,而无需从服务器获取整个结果集。

But note that even if you pull the use call out, you might still get some query execution time in your loop, because use pulls the rows one by one. 但是请注意,即使您拉出use调用,您也可能会在循环中获得一些查询执行时间,因为use逐行拉出行。 Consequently, the database executes the query tuple by tuple and only computes the next tuple when you take it from the result. 因此,数据库将按元组执行查询元组,并且仅当从结果中获取下一个元组时才计算下一个元组。 If you really want to measure a loop through all of the results, you must use the store function instead. 如果您真的想测量所有结果的循环,则必须使用store函数。 It executes the query thoroughly and stores the result in a block of memory. 它彻底执行查询,并将结果存储在内存块中。 Then, your loop will be blazingly fast. 然后,您的循环将非常快。 However, you should still prefer use because first storing all results into memory is usually just a waste of time and memory. 但是,您仍应首选use因为首先将所有结果存储到内存中通常只是浪费时间和内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM