获得QSqlQuery的结果非常慢

时间:2013-02-04 16:09:14

标签: sqlite qt

我看到Qt中的sqlite数据库出现了令人费解的缓慢行为。下面的代码中的查询,当从同一数据库上的sqlite命令行实用程序执行时,立即完成。

qDebug() << "Beginning" << QDateTime::currentDateTime ().toString(Qt::ISODate);
QSqlQuery q(QSqlDatabase::database(mFilename));
q.prepare( "select TextFormIndex.Id from TextFormIndex,MorphologicalAnalysisMembers,Allomorph on TextFormId=Id and AllomorphId=Allomorph._id where LexicalEntryId=:Id;" );
q.bindValue(":Id", id);

QSet<qlonglong> textFormIds;
if( !q.exec()  )
{
    qWarning() << "DatabaseAdapter::lexicalEntryTextForms" << q.lastError().text() << q.executedQuery();
    return textFormIds;
}

qDebug() << "Before while" << QDateTime::currentDateTime ().toString(Qt::ISODate);
int nResults = 0;
while( q.next() )
{
    qDebug() << nResults++ << QDateTime::currentDateTime().toString(Qt::ISODate);
    q.value(0).toLongLong();
}
qDebug() << "After while";

此代码的调试输出为:

Beginning "2013-02-04T20:31:24" 
Before while "2013-02-04T20:31:26" 
0 "2013-02-04T20:31:26" 
1 "2013-02-04T20:31:27" 
2 "2013-02-04T20:31:41" 
3 "2013-02-04T20:31:44" 
After while 

不知何故,第三次通话需要十四秒钟。

这是一个大型项目中的单一方法。在此之前,我的所有查询都没有缓慢执行。

create table if not exists TextFormIndex ( TextName text, LineNumber integer, Id integer );
create table if not exists MorphologicalAnalysisMembers ( _id integer primary key autoincrement, TextFormId integer, AllomorphId integer, AllomorphOrder integer );
create table if not exists Allomorph ( _id integer primary key autoincrement, LexicalEntryId integer, WritingSystem integer, Form text );

MorphologicalAnalysisMembers.AllomorphId对应Allomorph._id。 TextFormIndex.Id对应于MorphologicalAnalysisMembers.TextFormId。

MorphologicalAnalysisMembers有4082行 TextFormIndex有17710行 Allomorph有1660行

1 个答案:

答案 0 :(得分:0)

您的Qt中使用的SQLite版本似乎太旧了。

更新到最新的Qt或recompile Qt with a newer SQLite

相关问题