QSqlQueryModel::Data() Speed
-
wrote on 28 Mar 2012, 19:47 last edited by
Hi guys, i have this code :
@
for (int i=0;i<cantidad;i++) //cantidad = 13k rows
{ant= model->data(model->index(i,0)).toString(); //This line is slowing my App (5 seg and is a lot) }
@
but for example when i do this:
@
for (int i=0;i<cantidad;i++) //cantidad = 13k rows
{ant= model->data(model->index(5000,0)).toString(); //replace i for 5000 }
@
The App run in less than 1s, what can be the cause? QSqlQueryModel bug?Any solution? :S
Thx!
Pd: Happen the same uing model->record(i).value(0).toString();
[Edit: Added @-tags to code for formatting; mlong]
-
wrote on 28 Mar 2012, 20:12 last edited by
Caching, probably. Once you've queried model->index(5000, 0)->data(), all further queries will only access an internal cache for the 5000th row and thus be much faster.
You can verify this by timing each call with a high performance timer (~microseconds). If my guess is correct, the first call to data() (or index()) will be orders of magnitude slower than the subsequent. -
wrote on 28 Mar 2012, 20:18 last edited by
But that slow is normal? how can i navigate in the model data then? i cant use variables in data/record func?
-
wrote on 28 Mar 2012, 20:27 last edited by
If you said it takes 5 seconds, and you have 13000 rows, that means one query takes 0.3ms. that's not too bad. I don't know how QSqlQueryModel works internally, whether it actually queries the database when accessing indexes or only reads the internal model which was built with setQuery. If it actually queries the database everytime, then 0.3ms is actually quite good. Either way, if you want more performance, you'll need to get that data inside an intermediary cache that's specifically built to perform the accesses you want with high performance. You could either build that ontop of QSqlQueryModel or as a replacement. This might become alot of work, so make sure you have no other way around this, i.e. improved program logic that you don't even need to access 13000 indexes instantly.
1/4