简体   繁体   中英

QSqlQueryModel Usage and Performance

I have a small administrative application for a database that I maintain. There will never be more than between 1000 and 1500 records in the table that I'm focusing on, and there are only about eight columns of data. The form where I view this is subclassed from QTableView, and I'm using QSqlQueryModel to populate it. For years, I've been running this application on the server where MSSQL is, and the performance has been OK, but a little sluggish...not been a big deal. I'm specifically prefetching all data because I don't mind the small delay on startup in order to have better UI performance after that. There are about 1300 records in the table.

I can't very easily post the code but it is not complex.

I need to make some improvements, so for development purposes, I'm running it from my development machine, with the SQL Server port tunneled through an intermediate server. I expect a little bit of a slow down because of that, but the tunneling has good performance, and it shouldn't be dramatic.

But...it's horrible to the point of being unusable. I can't find documentation on how QSqlQueryModel works internally, but based on performance, it seems to be a live connection to the database even though I've prefetched all the data. I can't scroll through the form; it can take 20 or 30 seconds to respond to a single movement of the scroll bar. I found that resizeRowsToContents takes minutes. Every trivial interaction with the table view seems to trigger a huge delay, which I can only attribute to a lot of unexpected back-and-forth with the server.

I've searched a lot and tried a few experiments. First, I've removed all of the resizing rows and columns to get that out of the way (but I would like it back). I put a timer on the prefetching and found that I'm getting all of the data in about 18 to 19 seconds. (That's acceptable under the circumstances.)

I thought maybe I should just query the data and build my own model, so I created a query using QSqlQuery to get the same data and loop through all of the records. It has taken anywhere from 90 seconds to 3 minutes. I've used "prepare" and setForwardOnly to true. There's no parameters, so no bindings.

I've used Qt extensively for years, but I don't have much experience using it for SQL database access. The documentation describes functions, but seems to assume that I know what's happening behind the scenes, so I'm just guessing.

First of all, I'm looking for a better understanding of what QSqlQueryModel does behind the scenes in terms of staying current with the database table (if anything). Second, I'm looking for a technique where I can simply get all of the data from the table the fastest way possible. I can deal with everything else after that.

I solved my performance issue by adding a call to "setForwardOnly (true)" before calling "prepare" on the query. It's been quite a while since I dealt with this, but I think I was calling "setForwardOnly" after "prepare", which didn't work.

I continued using QSqlQueryModel without switching to any alternate solution because this change met my needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM