March 28, 2012

xjox xjox
Lab Rat
2 posts

QSqlQueryModel::Data() Speed

 

Hi guys, i have this code :

  1. for (int i=0;i<cantidad;i++)   //cantidad = 13k rows
  2.     {
  3.  
  4.          ant= model->data(model->index(i,0)).toString(); //This line is slowing my App (5 seg
  5.                                                                                                            and is a lot)
  6.     }
but for example when i do this:
  1. for (int i=0;i<cantidad;i++)   //cantidad = 13k rows
  2.     {
  3.  
  4.          ant= model->data(model->index(5000,0)).toString();  //replace i for 5000
  5.  
  6.     }

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]

3 replies

March 28, 2012

DerManu DerManu
Lab Rat
510 posts

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.

March 28, 2012

xjox xjox
Lab Rat
2 posts

But that slow is normal? how can i navigate in the model data then? i cant use variables in data/record func?

March 28, 2012

DerManu DerManu
Lab Rat
510 posts

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.

 
  ‹‹ Sharing data between an offscreen QGLContext and a QGLWidget      Compiling on MacOSX ››

You must log in to post a reply. Not a member yet? Register here!