"select * from tables"
query in MySQL connector/libmysql C is very slow in getting the results:
Here is my code in 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);
}
Apart from the fact, that there isn't even a question given in your post, you are comparing apples to oranges. Mysql gives you (I think - correct me if I am wrong) the time needed to execute the query, while in your C code you measure the time that passed between the start and the end of the program. This is wrong for at least two reasons:
Difference between two 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.
Even if you did use an appropriate method of retrieving your time, you are timing the wrong part of the code. Instead of measuring the time spent just on executing the query and retrieving the results, you measure the whole execution time: estabilishing connection, copying the query, executing it, storing the results, fetching them, printing them and closing the connection. That's quite different from what mysql measures. And printing hundreds of lines can take quite a lot of time, depending on your shell - more than the actual SQL query execution. If you want to know how much time the connector needs to retrieve the data, you should benchmark only the code responsible for executing the query and data retrieval. Or, even better, use some dedicated performance monitoring tools or libraries. I can't point a specific solution, because I never performed tests like that, but there certainly must be some.