SQLite 插入大量数据慢的解决方法
发布时间:2020-12-12 19:41:35 所属栏目:百科 来源:网络整理
导读:sqlite 插入数据很慢的原因:sqlite在没有显式使用事务的时候会为每条insert都使用事务操作,而sqlite数据库是以文件的形式存在磁盘中,就相当于每次访问时都要打开一次文件,如果对数据进行大量的操作,时间都耗费在I/O操作上,所以很慢。 解决方法是显式使
sqlite 插入数据很慢的原因:sqlite在没有显式使用事务的时候会为每条insert都使用事务操作,而sqlite数据库是以文件的形式存在磁盘中,就相当于每次访问时都要打开一次文件,如果对数据进行大量的操作,时间都耗费在I/O操作上,所以很慢。 #include <iostream> #include <windows.h> using namespace std; #include "sqlite/sqlite3.h" int main() { sqlite3* db; int nResult = sqlite3_open(test.db",&db); if (nResult != SQLITE_OK) { cout<<打开数据库失败:"<<sqlite3_errmsg(db)<<endl; return 0; } else { cout<<数据库打开成功"<<endl; } char* errmsg; nResult = sqlite3_exec(db,create table fuck(id integer primary key autoincrement,name varchar(100))errmsg); if (nResult != SQLITE_OK) { sqlite3_close(db); cout<<errmsg; sqlite3_free(errmsg); 0; } string strSql; strSql+=begin;n"; for (int i=0;i<100;i++) { strSql+=insert into fuck values(null,'heh');n"; } strSql+=commit;//cout<<strSql<<endl; SYSTEMTIME tm_s; GetLocalTime(&tm_s); nResult = sqlite3_exec(db,strSql.c_str(),&errmsg); SYSTEMTIME tm_e; GetLocalTime(&tm_e); if (nResult != SQLITE_OK) { sqlite3_close(db); cout<<errmsg<<endl; sqlite3_free(errmsg); 0; } cout<<start:"<<tm_s.wMinute<<:"<<tm_s.wSecond<<"<<tm_s.wMilliseconds<<endl; cout<<end :"<<tm_e.wMinute<<"<<tm_e.wSecond<<"<<tm_e.wMilliseconds<<endl; 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |