//============================================================================ // Name : BlobOperations.cpp // Author : Dmitry A Romanov // Version : // Copyright : (c) Dmitry A Romanov // Description : Hello World in C++, Ansi-style //============================================================================ #include #include #include #include #include using namespace std; void Query(); void RandomRead(); class Timer { public: typedef double diff_type; // Same as Timer t; t.begin(); Timer(): start(std::clock()), elapsed(0) {} // Last result before a call to begin() diff_type last() const { return elapsed; } // Reset the timer void begin() { start = std::clock(); elapsed = 0; } // Save the result diff_type end(); private: std::clock_t start; diff_type elapsed; }; Timer::diff_type Timer::end() { elapsed = (diff_type)std::clock() - start; elapsed /= CLOCKS_PER_SEC; return elapsed; } int main(int argc, char **argv) { printf("MySQL client version: %s\n", mysql_get_client_info()); //Query(); RandomRead(); } /* Simple C program that connects to MySQL Database server*/ #include #include void Query() { MYSQL *conn; MYSQL_RES *res; MYSQL_ROW row; const char *server = "localhost"; const char *user = "first"; const char *password = ""; /* set me first */ const char *database = "first"; conn = mysql_init(NULL); /* Connect to database */ if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); } /* send SQL query */ if (mysql_query(conn, "show tables")) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); } res = mysql_use_result(conn); /* output table name */ printf("MySQL Tables in mysql database:\n"); while ((row = mysql_fetch_row(res)) != NULL) printf("%s \n", row[0]); Timer timer; timer.begin(); for(int i=0; i<1100000; i++){ char *insSQL = "INSERT INTO `first`.`values` (`blob`) VALUES ( '1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|1943.4445|');"; /* send SQL query */ if (mysql_query(conn, insSQL)) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); } } printf("Time is: %f\n", timer.end()); /* close connection */ mysql_free_result(res); mysql_close(conn); } void RandomRead() { //( value % 100 ) is in the range 0 to 99 MYSQL *conn; MYSQL_RES *res; MYSQL_ROW row; char *server = "localhost"; char *user = "first"; char *password = ""; /* set me first */ char *database = "first"; conn = mysql_init(NULL); /* Connect to database */ if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); } Timer timer; timer.begin(); for(int i=0; i<1100; i++) { const char *tmp = "SELECT `id`, `blob` FROM `first`.`values` WHERE id = %i;"; char sql[200]; sprintf(sql, tmp, (rand()%11000) + 1); /* send SQL query */ if (mysql_query(conn, sql)) { fprintf(stderr, "%s\n", mysql_error(conn)); exit(1); } res = mysql_use_result(conn); /* output table name */ //printf("Result is:\n"); while ((row = mysql_fetch_row(res)) != NULL); // printf("%s \n", row[1]); /* close connection */ mysql_free_result(res); } printf("Time is: %f\n", timer.end()); mysql_close(conn); }