PDA

View Full Version : QSqlQueryModel speed problem



tbscope
23rd March 2008, 11:47
Hello,

I have a SQLite database with a large dataset, around 200000 rows.
These rows are selected from the database with a QSqlQueryModel like this:



QSqlQueryModel *model = new QSqlQueryModel;
QString q = QString("SELECT name FROM groups");
model->setQuery(q);


This only returns the first 255 rows.
Thus I need to use canFetchMore() and fetchMore() in a while loop to get all the rows.

The disadvantage of that is that the userinterface becomes unresponsive for about 5 seconds. I like to solve that in such a way that all the rows are added without the userinterface freezing.



If I don't use the query model and just do a QSqlQuery with the same SELECT command and using while(query.next()), I get all the rows in less than a second. This is my debug output:

Time = QTime("11:35:26")
Number of rows returned: -1
Calculated rows: 192792
Query is active: true
Time = QTime("11:35:26")

This is the code:


QSqlQuery speedQuery;
qDebug() << "Time = " << QTime::currentTime();
speedQuery.exec("SELECT name FROM groups;");

int i = 0;
while(speedQuery.next())
{
++i;
}

qDebug() << "Number of rows returned: " << speedQuery.size();
qDebug() << "Calculated rows: " << i;
qDebug() << "Query is active: " << speedQuery.isActive();
qDebug() << "Time = " << QTime::currentTime();


How can I make QSqlQueryModel as fast as QSqlQuery?
I think the speed differences are in inserting rows in the model. Is there a way to speed this up?

If I don't use the canFetchMore() - fetchMore() loop, then there's no speed problem, but the slider on the listview doesn't work correct. If I drag the slider to the bottom, more items are fetched and the more I go down, the slower it becomes.

Can anyone give me any pointers on how to load all the items of the database in the listview without freezing the userinterface for a couple of seconds?

wysota
23rd March 2008, 12:07
If you load all items at once, you're get a huge lag at the beginning.

If you want, you can make a lazy initialization of the model in the background by an external thread.

You can get some info on that here: http://blog.wysota.eu.org/index.php/2006/12/26/remote-models/

tbscope
23rd March 2008, 12:31
Thanks wysota,

That's exactly what I was looking for.