用mysql C API查询代码如下,对一组ip进行反复查询,确保所有查询都已经在Query Cache中,用GetTickCount测查询时间。刚开始时大部分时间都是0,还有很少是15或16;但16出现的频率越来越高,到后来大部分查询时间都是16,不知道是什么原因,求大神指教。
bool query(unsigned int ip,string &city,string &country)
{
char sql[256];
memset(sql,0,256);
sprintf_s(sql,256,"select country,city FROM citylocation where LocId = (select LocId from ipindex where                    startIpNum <= %u  and endIpNum >=  %u limit 1)",ip,ip); DWORD start,end;
start = GetTickCount();
if( mysql_real_query(mysql,sql,strlen(sql)) !=0 )
return false;                            // 查询失败
end = GetTickCount();
cout << end - start << "  "  << endl;           //查询时间 MYSQL_RES *rs=mysql_store_result(mysql); 
if(rs != NULL)
{
MYSQL_ROW row;
if((row = mysql_fetch_row(rs)) == NULL)
return false;
if(row[0] != NULL)
country = row[0];
if(row[1] != NULL)
city = row[1];
free_result(rs);
return true;
}
        return false;
}