简体   繁体   English

在sqlite3 C ++中批量插入的更好方法

[英]Better way to bulk insert in sqlite3 C++

I've bulk insert statements to be executed. 我已经执行了批量插入语句。 I can create a single sql string with all the insert statements and execute once like, 我可以使用所有insert语句创建一个sql字符串,然后执行一次,

std::string sql_string = "";
int i;
for(i=0;i<1000; i++) {
   Transaction transaction = transactions[i];
   std::string tx_sql = CREATE_SQL_STRING(transaction, lastTxId); // "INSERT INTO (...) VALUES (...);"
   sql_string = sql_string+tx_sql;
   lastTxId++;
}
sqlite3_exec(db, sql_string.c_str(), callback, 0, &zErrMsg);

Is there any other better-performing way to bulk insert with sqlite3 c++ API? 还有其他使用sqlite3 c ++ API进行批量插入的性能更好的方法吗?

Insertions are not security critical. 插入对安全性不是至关重要的。 Only the performance is considered. 仅考虑性能。 Should I use prepared statements? 我应该使用准备好的陈述吗?

most important: put all inserts into a single transaction. 最重要的是:将所有插入插入单个事务中。

for more detailed stuff regarding performance see the official documentation: https://sqlite.org/speed.html (a bit outdated) 有关性能的更多详细信息,请参见官方文档: https : //sqlite.org/speed.html (有些过时)

for more recent information i'd recomment https://medium.com/@JasonWyatt/squeezing-performance-from-sqlite-insertions-971aff98eef2 有关更多最新信息,我建议https://medium.com/@JasonWyatt/squeezing-performance-from-sqlite-insertions-971aff98eef2

as for prepared statements: i am not sure about performance, but it is a good practice to always use them. 至于准备好的陈述:我不确定性能,但是最好始终使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM