for the heck of it, here's the dump of my results. This is on a 1Ghz PIII machine with gobs of RAM and a 2 column, 908500 row database (containing the words from the linux dictionary, one word per row, 19 times over)
result dump:
13620
Total time: 0.747288
Time to open server connection: 0.002029
Time to open db: 0.000100
Time to do query: 0.744992
Time to store result: 0.000017
Time to fetch rows: 0.000083
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include "/usr/include/mysql/mysql.h"
/* define some variables to use */
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
/* define a more verbose exit function to help debug */
void exiterr(int exitcode)
{
fprintf( stderr, "%s\n", mysql_error(&mysql) );
exit( exitcode );
}
double getElapsedInterval(struct timeval *start, struct timeval *end)
{
return (end->tv_sec + (end->tv_usec * 0.000001)) -
(start->tv_sec + (start->tv_usec * 0.000001));
}
/* the guts of the program */
int main()
{
struct timeval start, openserver, querydone, storeresult,
fetchrows, end;
uint i = 0;
/* start the timer
*/
gettimeofday(&start, NULL);
mysql_init(&mysql);
mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "app_name");
/* initiate a connection to the database server
*/
if (!(mysql_real_connect(&mysql,"localhost","root","", "database_name", 0, NULL, 0)))
{
printf("can't connect to database server\n");
exiterr(1);
}
gettimeofday(&openserver, NULL);
/* issue a simple query
*/
int result = mysql_query(&mysql, the_query.c_str());
switch(result)
{ case CR_COMMANDS_OUT_OF_SYNC:
printf("can't do query\n");
exiterr(3);
return ERROR_DB_FAILURE;
case CR_SERVER_GONE_ERROR:
printf("can't do query\n");
exiterr(3);
return ERROR_DB_FAILURE;
case CR_SERVER_LOST:
printf("can't do query\n");
exiterr(3);
return ERROR_DB_FAILURE;
case CR_UNKNOWN_ERROR:
printf("can't do query\n");
exiterr(3);
return ERROR_DB_FAILURE;
case 0: // success
break;
}
gettimeofday(&querydone, NULL);
/* if it didn't take, error out */
if (!(res = mysql_store_result(&mysql)))
{
printf("can't store mysql result\n");
exiterr(4);
}
gettimeofday(&storeresult, NULL);
/* otherwise, print the results
*/
int rows = mysql_num_rows(res);
printf("result dump:\n");
while((row = mysql_fetch_row(res))) {
for (i=0 ; i < mysql_num_fields(res); i++)
printf("%s\n",row[i]);
}
gettimeofday(&fetchrows, NULL);
/* if we got here, but MySQL doesn't think we're done- error!
*/
if (!mysql_eof(res))
{
printf("something funny happened here; EOF out of sync\n");
exiterr(5);
}
/* give up server resources, and close the connection
*/
mysql_free_result(res);
mysql_close(&mysql);
gettimeofday(&end, NULL);
printf("Total time: %lf\n",
getElapsedInterval(&start, &end));
printf("Time to open server connection: %lf\n",
getElapsedInterval(&start, &openserver));
printf("Time to do query: %lf\n",
getElapsedInterval(&opendb, &querydone));
printf("Time to store result: %lf\n",
getElapsedInterval(&querydone, &storeresult));
printf("Time to fetch rows: %lf\n",
getElapsedInterval(&storeresult, &fetchrows));
return 0;
}
Makefile
CC = gcc
LIBPATH = -L.
INCLS = -I.
CFLAGS = $(INCLS) -Wall -g
TARGET = mysqlCdemo
default: all
all: $(TARGET)
$(TARGET): $(OBJS) $(TARGET).c
$(CC) -o $(TARGET) $(TARGET).c $(CFLAGS) `/usr/bin/mysql_config --libs`
clean:
$(RM) *.o $(TARGET) core
--
MattWalsh - 12 Aug 2002