C语言API—sqlite3的速度问题
在这里:http://web.utk.edu/~jplyon/sqlite/SQLite_optimization_FAQ.html#timing
大拿说道:The first thing you should know is that most of the time spent by SQLite (and most other DB systems) is on disk access, which is slow compared to memory operations.
我写了个测试程序,意外的看到读写只是运行时占用资源的一个小头,大头被sqlite3_get_table()占了,请教是怎么回事?
////////////////////////////////////////////////////////////////////////////////////////
/* 纯净版, 无出错处理 */
//////////////////////// 头文件 /////////////////////////
#include <stdio.h>
#include <sqlite3.h>
#include <time.h> /* for time */
#include <stdlib.h> /* for system */
#include <string.h> /* for memset strcpy */
////////////////////// 宏及结构体定义 ///////////////////////
#define DB_FILE "test_0.1.db"
struct s_table1
{
int id;
int mark;
char name[33];
unsigned char type;
char con[513];
};
//////////////////////// 全局变量 /////////////////////////
sqlite3 *db = NULL;
int ret = -1; /* 各函数返回值 */
//////////////////////// 函数声明 /////////////////////////
void way01(); // 打开、关闭的影响
int createdb();
int insertdb();
int getdatas_no_oc1 ( sqlite3 *db, struct s_table1 *tb1 );
///////////////////////// 代码 /////////////////////////
int main ( void )
{
int c = 0;
ret = createdb();
ret = insertdb();
printf ( "createdb() -> createtable() -> insertdb() over\n" );
printf ( "////////////////////////////////\n" );
printf ( "1 : 打开、关闭的影响 ///\n" );
while ( (c=getchar()) != 'q' )
{
switch (c)
{
case '1':
way01(); // 打开、关闭的影响
break;
default:
break;
}
}
system ( "rm -rf test_0.1.db" );
return 0;
}
///////////////////////////////////////////////////////////////
// 打开、关闭的影响
void way01()
{
time_t tick1, tick2;
int i = 0;
int num = 100000;
struct s_table1 tb_data;
time ( &tick1 );
for ( i=0; i<num; i++ )
{
ret = sqlite3_open ( DB_FILE, &db );
sqlite3_close (db);
}
time ( &tick2 );
printf("单纯打开、关闭数据库文件 100000 次, 时间为: %4ld s\n", tick2 - tick1 );
time ( &tick1 );
for ( i=0; i<num; i++ )
{
ret = sqlite3_open ( DB_FILE, &db );
ret = getdatas_no_oc1 ( db, &tb_data );
sqlite3_close (db);
}
time ( &tick2 );
printf("打开、关闭并操作数据库文件 100000 次, 时间为: %4ld s\n", tick2 - tick1 );
}
///////////////////////////////////////////////////////////////
int createdb()
{
system ( "rm -rf test_0.1.db" );
char *sql1 = "CREATE TABLE table1 (id INTEGER, m INTEGER, n VARCHAR(32), t CHAR(1), con VARCHAR(512))";
ret = sqlite3_open ( DB_FILE, &db );
ret = sqlite3_exec ( db, sql1, NULL, NULL, NULL );
sqlite3_close (db);
return 0;
}
int insertdb()
{
time_t ticks1, ticks2;
int i = 0;
int num = 1000;
ret = sqlite3_open ( DB_FILE, &db );
char *qf1 = "INSERT INTO table1 VALUES (%d, %d, %Q, %d, %Q)";
char *sql = NULL;
time ( &ticks1 );
//下面是对所以插入进行手动提交, 这样可以加快插入速度, 大于200倍
sqlite3_exec ( db, "BEGIN", NULL, NULL, NULL );
//插入num条记录
for (i=0;i<num;i++)
{
sql = sqlite3_mprintf ( qf1, i, i*2, "goodc", i%(num/10), "test - varcharvarcharvarchar" );
ret = sqlite3_exec(db, sql, NULL, NULL, NULL);
sqlite3_free (sql);
}
sqlite3_exec(db,"COMMIT",NULL,NULL,NULL);
time ( &ticks2 );
//printf ( "the time of insertdb is: %ld s\n", ticks2-ticks1 );
sqlite3_close (db);
return 0;
}
int getdatas_no_oc1 ( sqlite3 *db, struct s_table1 *tb1 )
{
char *sql = "SELECT * FROM table1 WHERE id=500;";
int row = 0;
int column = 0;
char **resultp = NULL;
ret = sqlite3_get_table ( db, sql, &resultp, &row, &column, NULL );
sqlite3_free_table(resultp);
return 0;
}
大拿说道:The first thing you should know is that most of the time spent by SQLite (and most other DB systems) is on disk access, which is slow compared to memory operations.
我写了个测试程序,意外的看到读写只是运行时占用资源的一个小头,大头被sqlite3_get_table()占了,请教是怎么回事?
////////////////////////////////////////////////////////////////////////////////////////
/* 纯净版, 无出错处理 */
//////////////////////// 头文件 /////////////////////////
#include <stdio.h>
#include <sqlite3.h>
#include <time.h> /* for time */
#include <stdlib.h> /* for system */
#include <string.h> /* for memset strcpy */
////////////////////// 宏及结构体定义 ///////////////////////
#define DB_FILE "test_0.1.db"
struct s_table1
{
int id;
int mark;
char name[33];
unsigned char type;
char con[513];
};
//////////////////////// 全局变量 /////////////////////////
sqlite3 *db = NULL;
int ret = -1; /* 各函数返回值 */
//////////////////////// 函数声明 /////////////////////////
void way01(); // 打开、关闭的影响
int createdb();
int insertdb();
int getdatas_no_oc1 ( sqlite3 *db, struct s_table1 *tb1 );
///////////////////////// 代码 /////////////////////////
int main ( void )
{
int c = 0;
ret = createdb();
ret = insertdb();
printf ( "createdb() -> createtable() -> insertdb() over\n" );
printf ( "////////////////////////////////\n" );
printf ( "1 : 打开、关闭的影响 ///\n" );
while ( (c=getchar()) != 'q' )
{
switch (c)
{
case '1':
way01(); // 打开、关闭的影响
break;
default:
break;
}
}
system ( "rm -rf test_0.1.db" );
return 0;
}
///////////////////////////////////////////////////////////////
// 打开、关闭的影响
void way01()
{
time_t tick1, tick2;
int i = 0;
int num = 100000;
struct s_table1 tb_data;
time ( &tick1 );
for ( i=0; i<num; i++ )
{
ret = sqlite3_open ( DB_FILE, &db );
sqlite3_close (db);
}
time ( &tick2 );
printf("单纯打开、关闭数据库文件 100000 次, 时间为: %4ld s\n", tick2 - tick1 );
time ( &tick1 );
for ( i=0; i<num; i++ )
{
ret = sqlite3_open ( DB_FILE, &db );
ret = getdatas_no_oc1 ( db, &tb_data );
sqlite3_close (db);
}
time ( &tick2 );
printf("打开、关闭并操作数据库文件 100000 次, 时间为: %4ld s\n", tick2 - tick1 );
}
///////////////////////////////////////////////////////////////
int createdb()
{
system ( "rm -rf test_0.1.db" );
char *sql1 = "CREATE TABLE table1 (id INTEGER, m INTEGER, n VARCHAR(32), t CHAR(1), con VARCHAR(512))";
ret = sqlite3_open ( DB_FILE, &db );
ret = sqlite3_exec ( db, sql1, NULL, NULL, NULL );
sqlite3_close (db);
return 0;
}
int insertdb()
{
time_t ticks1, ticks2;
int i = 0;
int num = 1000;
ret = sqlite3_open ( DB_FILE, &db );
char *qf1 = "INSERT INTO table1 VALUES (%d, %d, %Q, %d, %Q)";
char *sql = NULL;
time ( &ticks1 );
//下面是对所以插入进行手动提交, 这样可以加快插入速度, 大于200倍
sqlite3_exec ( db, "BEGIN", NULL, NULL, NULL );
//插入num条记录
for (i=0;i<num;i++)
{
sql = sqlite3_mprintf ( qf1, i, i*2, "goodc", i%(num/10), "test - varcharvarcharvarchar" );
ret = sqlite3_exec(db, sql, NULL, NULL, NULL);
sqlite3_free (sql);
}
sqlite3_exec(db,"COMMIT",NULL,NULL,NULL);
time ( &ticks2 );
//printf ( "the time of insertdb is: %ld s\n", ticks2-ticks1 );
sqlite3_close (db);
return 0;
}
int getdatas_no_oc1 ( sqlite3 *db, struct s_table1 *tb1 )
{
char *sql = "SELECT * FROM table1 WHERE id=500;";
int row = 0;
int column = 0;
char **resultp = NULL;
ret = sqlite3_get_table ( db, sql, &resultp, &row, &column, NULL );
sqlite3_free_table(resultp);
return 0;
}