加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

小测SQLite内存与非内存万条插入

发布时间:2020-12-12 23:37:19 所属栏目:百科 来源:网络整理
导读:代码: // name: memory_sqlite.c date: 2009/08/10 by JIN RIZE #include stdio.h #include string.h #include stdlib.h #include sys/time.h #include "sqlite3.h" ////////////////////////////////////////////////////////////////////////////////////

代码:


// name: memory_sqlite.c date: 2009/08/10 by JIN RIZE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include "sqlite3.h"

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 在这里不声明,main中callback不能编译通过。
int callback(void* data,int ncols,char** values,char** headers);

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//time caculation
long timecacul () {
struct timeval tv;
struct timezone tz;
gettimeofday(&tv,&tz);
return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Main FUNC
int main(int argc,char **argv)
{
sqlite3 *db;
int rc;
char *sql;
char *zErr;

long starttime,endtime,resulttime;

int i=0;
int num=10000;

// create db
rc=sqlite3_open(":memory:",&db);
if (rc){
fprintf(stderr,"Can't create MMDB: %s/n",sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}

//create table
sql = "create table user(uid int,name varchar(20))";
rc= sqlite3_exec(db,sql,NULL,&zErr);
if (rc!= SQLITE_OK) {
if ( zErr!=NULL) {
fprintf(stderr,"SQL error: %s/n",zErr);
sqlite3_free(zErr);
}
}

//select after insert
//这里非要定义为const,则下五行中参数data前面加(char*),强制转换,否则就不能通过编译
const char* data="callback function called";
char *qy="insert into user values(%d,%Q);";

//计时开始
starttime=timecacul();
/////////加事务的插入操作
sqlite3_exec(db,"BEGIN TRANSACTION;",&zErr);
for (i; i<num; i++)
{
sql=sqlite3_mprintf(qy,i,"ceshi");
rc=sqlite3_exec(db,callback,(char*) data,&zErr);
sqlite3_free(sql);

}
sqlite3_exec(db,"COMMIT TRANSACTION;",NULL);

//计时结束
endtime=timecacul();
resulttime=endtime-starttime;
//输出耗时
/* sql="select * from user;";
rc=sqlite3_exec(db,&zErr);
*/
printf("加事务的插入耗时:%dms./n",resulttime);

//////////不加事务
//计时开始
starttime=timecacul();
//不加事务的插入操作,注意i置0
for (i=0; i<num; i++)
{
sql=sqlite3_mprintf(qy,&zErr);
sqlite3_free(sql);

}
//计时结束
endtime=timecacul();
resulttime=endtime-starttime;
//输出耗时
printf("不加事务的插入耗时:%dms./n",resulttime);
if(rc!=SQLITE_OK){
if(zErr !=NULL){
fprintf(stderr,zErr);
sqlite3_free(zErr);
}
}
sqlite3_close(db);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////
int callback(void* data,char** headers)
{
int i;
fprintf(stderr,"%s:",(const char*) data);
for (i=0; i<ncols; i++) {
fprintf(stderr,"%s=%s.",headers[i],values[i]);
}

fprintf (stderr,"/n");
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////



测试结果:

[db@localhost sqlite3]$ gcc mem_sq.c -o mem_sq -L/usr/local/lib/ -lsqlite3
[db@localhost sqlite3]$ ./mem_sq
加事务的插入耗时:133ms.
不加事务的插入耗时:146ms.
[db@localhost sqlite3]$ ./mem_sq
加事务的插入耗时:133ms.
不加事务的插入耗时:146ms.
[db@localhost sqlite3]$

小结: 做内存数据库加不加事务,没什么太大区别

不做内存数据库,将rc=sqlite3_open(":memory:",&db); 改成 rc=sqlite3_open("iltaek.db",&db);

测试结果:

[db@localhost sqlite3]$ gcc mem_sq.c -o mem_sq -L/usr/local/lib/ -lsqlite3

[db@localhost sqlite3]$ ./mem_sq
加事务的插入耗时:140ms.
不加事务的插入耗时:23899ms.
[db@localhost sqlite3]$ ./mem_sq
加事务的插入耗时:137ms.
不加事务的插入耗时:17174ms.
[db@localhost sqlite3]$ ./mem_sq
加事务的插入耗时:139ms.
不加事务的插入耗时:21210ms.

(生出了个307KB大小的iltaek.db 文件)

小结:不做MMDB加不加事务,相差两个数量级

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读