MySQL connector(libmysql/C) 获取 RES 非常慢

MySQL connector(libmysql/C) is very slow in get RES

"select * from tables" MySQL connector/libmysql 中的查询 C 获取结果的速度很慢:

这是我的 C 代码:

int getfrommysql() {
    time_t starttime, endtime;
    time(&starttime);
    double st;
    st = GetTickCount();
    MYSQL *sqlconn = NULL;
    MYSQL_RES * res = NULL;
    MYSQL_ROW row = NULL;
    MYSQL_FIELD * field;
    /*char ipaddr[16];
    memset(ipaddr,0,sizeof(ipaddr));*/
    char * sqlquery = "select * from seat_getvalue";
    sqlconn = malloc(sizeof(MYSQL));
    sqlconn = mysql_init(sqlconn);
    mysql_real_connect(sqlconn, "111.111.111.111", "root", "password", "database", 0, NULL, 0);
    char query[100];
    memset(query, 0, 100);
    strcpy(query, "select * from seat_getvalue");
    mysql_query(sqlconn, query);
    res = mysql_store_result(sqlconn);
    int col_num, row_num;
    if (res) {
        col_num = res->field_count;
        row_num = res->row_count;
        printf("\nthere is a %d row,%d field table", res->row_count, res->field_count);
    }
    for (int i = 0; i < row_num; i++) {
        row = mysql_fetch_row(res);
        for (int j = 0; j < col_num; j++) {
            printf("%s\t", row[j]);
        }
        printf("\n");
    }
    mysql_close(sqlconn);
    time(&endtime);
    double et = GetTickCount();
    printf("the process cost time(get by GetTickCount):%f",et-st);
    printf("\nthere is a %d row,%d field table", res->row_count, res->field_count);
}

除此之外,您 post 中甚至没有给出任何问题,您是在拿苹果和橙子进行比较。 Mysql 给你(我想 - 如果我错了请纠正我)执行查询所需的时间,而在你的 C 代码中你测量程序开始和结束之间经过的时间。这是错误的至少有两个原因:

  1. 两者的区别GetTickCount() calls gives you the time that has passed between the calls in the whole system, not time spent executing your software. These are two different things, because your process does not have to executed from the beginning to the end uninterrupted - it can (and probably will be) swapped for another process in the middle of the execution, it can be interrupted etc. The whole time the system spent doing stuff outside your program will be added to your measurements. To get time spent on the execution of your code you could probably use GetProcessTimes or QueryProcessCycleTime.

  2. 即使您确实使用了适当的方法来检索您的时间,您也为代码的错误部分计时。您不是测量执行查询和检索结果所花费的时间,而是测量整个执行时间:建立连接、复制查询、执行它、存储结果、获取它们、打印它们和关闭连接。这与 mysql 测量的结果完全不同。打印数百行可能会花费很多时间,具体取决于您的 shell - 比实际的 SQL 查询执行时间还要长。如果您想知道连接器需要多长时间来检索数据,您应该 仅对 负责执行查询和数据检索的代码进行基准测试。或者,更好的是,使用一些专用的性能监控工具或库。我不能指出具体的解决方案,因为我从来没有进行过这样的测试,但肯定有一些。