PDA

View Full Version : How to select one table with QSqlTableModel (QSqlQuery or ...) in multithreading



silentyears
26th February 2014, 15:44
Hi,
To select all the rows(around 20000 rows) from table in the Oracle database, i use QSqlTableModel like this:


QSqlTableModel model;
model.setTable(tableA);
model.select();

while (model.canFetchMore())
{
model.fetchMore();
}

int nRowCount = model.rowCount();

for(int i = 0; i<nRowCount; i++)
{
//handle each row
//..
//..
}



But using canFetchMore() and fetchMore() in a while loop to get all the rows need too much time, so i refer to this article http://www.qtcentre.org/threads/12637-QSqlQueryModel-speed-problem. Then i plan to start serval threads in MyModel(inherits RemoteTableModel as illustrated in article), each thread start to select part of table and add the part into MyModel, but i am not sure how to do it in details, could i use QSqlTableModel to select in each thread?
Could you give me some tips or any better way?
Thanks!

Scorp2us
26th February 2014, 15:59
You have to investigate your driver. Not all drivers support multi-threads. Some do. I think Oracle's does, but don't quote me on that. In addition Qt has threading limitations with drivers too. You'll probably need a connection per thread (see QSqlDatabase::addDatabase(). Note when I was connecting to oracle, the oracle connect time took a while.

Once you've got all that sorted out, you can just use Qt primitive types like QMap Or QList to share data between threads.

However I think there is a better approach overall. Why do you need all 20k rows at once? Either have the database do the work, or if displaying them, let a ModelView handle displaying them, which also handles loading.

anda_skoa
26th February 2014, 20:54
could i use QSqlTableModel to select in each thread?


Probably, but that would be an unnecessary overhead given that you do not need a model in the thread.
Just run QSqlQuery and notify the model of data as it comes in.

It is not very wide to split your problem over so many threads, because anyone not following it through all your threads will not have the overview and always concentrate on what your most current thread asks for instead of helping you solve the problem.

Do you expect all readers of your threads to click on your name and research which postings you might have already made on the topic?

Cheers,
_