100W数据查询 ,下面的程序
有键:select confid from testtime where confid=294142697 ; 时间:0.02sec
无键:select confid from testtime where confid=294142697 ; 时间:1.54sec
4 int main()
5 {6 MYSQL *pMysql = NULL;
7 MYSQL_RES *pMysqlRes = NULL;
8 MYSQL_FIELD *pMysqlField = NULL;
9 MYSQL_ROW pMysqlRow = NULL;
10 unsigned long client_flag = 0;
11 int iResult = 0;
12 int i = 0, j = 0;
13 int iRand;
14 int index = 1;
15 struct timeval beforetime;
16 struct timeval aftertime;
17 unsigned long long all = 0,all_1=0,all_2=0,all_3=0;
18
19
20 bzero(&beforetime,sizeof(beforetime)); 21 bzero(&aftertime,sizeof(aftertime));
22
23
24 if ((pMysql = mysql_init(NULL)) == NULL )
25 printf ("mysql init failed\n");
26 if ((mysql_real_connect (pMysql, "127.0.0.1", "mysql", "", "test", 3306,
27 NULL, client_flag)) == NULL )
28 printf("connect db wrong\n");
29 char sql[256]={0};
30 while(i < 1000) {
31 while(j <10) {
32 iRand = rand();
33 snprintf(sql,sizeof(sql),"insert into testtime \
34 values(%d,%d,20161209);",index,iRand);
35 //printf("sql;%s\n",sql);
36 gettimeofday(&beforetime, NULL);
37 //printf("before time=%d\n",beforetime.tv_usec);
38 iResult = mysql_query(pMysql,sql);
39 //printf("time=%d\n",aftertime.tv_usec);
40 gettimeofday(&aftertime, NULL);
41 if ( i < 300)
42 all += (aftertime.tv_sec-beforetime.tv_sec)*1000000+
43 aftertime.tv_usec-beforetime.tv_usec;
44 else if ( i >= 300 && i < 600)
45 all += (aftertime.tv_sec-beforetime.tv_sec)*1000000+46 aftertime.tv_usec-beforetime.tv_usec;
47 else if (i >= 600 && i<800)
48 all += (aftertime.tv_sec-beforetime.tv_sec)*1000000+
49 aftertime.tv_usec-beforetime.tv_usec;
50 else if (i >= 800)
51 all += (aftertime.tv_sec-beforetime.tv_sec)*1000000+
52 aftertime.tv_usec-beforetime.tv_usec;
53
54 index++;
55 if (iResult == 0)
56 ;
57 else
58 {
59 printf("insert mysql failed:%s\n",mysql_error(pMysql));
60 }
61 j++;
62 }
63 j = 0;
64 i++;
65 }
67 printf("all=%u,all_1=%u,all_2=%u,all_3=%u\n",all,all_1,all_2,all_3);
68 }