【发布时间】:2015-09-20 17:24:02
【问题描述】:
在 Qt5.4 中,在 Ubuntu14.04 64bit 上使用 QSqlDatabase 和 sqlite3:
首先我打开数据库并调用transaction()。
接下来我进行 54 个单独的插入查询,每个都准备好,每个在执行后删除。
最后我打电话给commit()。
所有调用都没有错误地完成,但执行时间仍然很糟糕(54 次微不足道的插入总共大约 500 毫秒)。
我的电脑相当现代,并且配备了条带 SSD 磁盘以提高性能。使用 Sqliteman 访问 sqlite 文件时速度非常快。
那么发生了什么?
这是插入:
void BottleRigStorage::upsertTag(Tag &tag){
//ScopedTimer st("query time for tag");
if(open()){
QSqlQuery query(db);
query.prepare("INSERT OR REPLACE INTO tags ("
" id"
", batchID"
", retries"
", good"
", status"
", color"
", firstCheckTimestamp"
", createdTimestamp"
", modifiedTimestamp"
", fulfilledTimestamp"
") VALUES ("
" :id"
", :batchID"
", :retries"
", :good"
", :status"
", :color"
", :firstCheckTimestamp"
", :createdTimestamp"
", :modifiedTimestamp"
", :fulfilledTimestamp"
");");
query.bindValue(":id", tag.id);//8 chars
query.bindValue(":batchID", tag.batchID);//8 chars
query.bindValue(":retries", tag.retries);//int
query.bindValue(":good",tag.good?1:0);//bool
query.bindValue(":status", tag.status);//6 chars
query.bindValue(":color", tag.color);//7 chars
query.bindValue(":firstCheckTimestamp", tag.firstCheckTimestamp); //long
query.bindValue(":createdTimestamp", tag.createdTimestamp);//long
query.bindValue(":modifiedTimestamp", tag.modifiedTimestamp);//long
query.bindValue(":fulfilledTimestamp", tag.fulfilledTimestamp);//long
if (query.exec()) {
//qDebug() << "Successfully updated tag database after "<<st.getIntervalCompleteString();
}
else {
qWarning() << "ERROR: could not upsert tag with id " << tag.id<< ". Reason: "<< query.lastError();
}
query.finish();
}
else {
qWarning() << "ERROR: DB not open for upsert tag sqlite3";
}
}
更新:这里是 open() 请求:
bool BottleRigStorage::open(){
if(!db.isOpen()){
if(!db.open()){
qWarning() << "ERROR: could not open database. Reason: "<<db.lastError();
}
}
return db.isOpen();
}
【问题讨论】:
-
你的分析器说什么?
-
是否有用于 sqlite 的分析器?
-
可能,但不太可能是 SQLite,更可能与您使用它的方式有关。分析您的应用程序应该揭示减速的位置(或至少给出一个很好的提示)。
-
被注释掉的ScopedTimer st("query time for tag");代码中的一部分测量了在这个函数中花费的时间,但我想使用分析器我也可以看到子调用。
-
open有什么功能?顺便说一句,你不需要query.finish();
标签: qt sqlite qt5 qtsql qsqldatabase