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;
}