Results 1 to 3 of 3

Thread: Extracting QSqlQuery results in PyQt4

  1. #1
    Join Date
    Sep 2008
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Extracting QSqlQuery results in PyQt4

    I am trying to use SQLite in a PyQt4 application. I am running into a problem where it is taking a really long time to get the data out of the QSqlQuery object after a query is completed. Here is a code snippet with the relevant parts:
    Qt Code:
    1. # Begin Snippet
    2. self.selected_columns = self.table_definition['track']
    3. select_string = ','.join(self.selected_columns)
    4.  
    5. query_str = 'SELECT %s FROM track %s'%(select_string, where_string)
    6.  
    7. start_time = time.clock()
    8. self.query.prepare(query_str)
    9. self.query.exec_()
    10. util.message('Query took:', time.clock() - start_time, 'seconds')
    11.  
    12. print ''
    13. print query_str
    14.  
    15. results = []
    16. header = self.selected_columns
    17.  
    18. self.query.first()
    19.  
    20. while self.query.isValid():
    21. record = [self.query.value(index).toString() for index in range(len(header))]
    22. results.append(record)
    23. self.query.next()
    24.  
    25. util.message('Selected', len(results), 'records.')
    26.  
    27. util.message('Unpacking Took:', time.clock() - start_time, 'seconds')
    To copy to clipboard, switch view to plain text mode 

    While the query is quick, extracting the data takes a really long time. Way longer than I think it should take.

    Here are the results:

    Qt Code:
    1. Query took: 0.00094797466213 seconds
    2.  
    3. SELECT artist,album,title,url,musicbrainz_trackid,hystrix_trackid FROM track
    4. Selected 16668 records.
    5. Unpacking Took: 5.29902267251 seconds
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. Query took: 0.0317866355896 seconds
    2.  
    3. SELECT artist,album,title,url,musicbrainz_trackid,hystrix_trackid FROM track WHERE (album LIKE '%it%' OR title LIKE '%it%' OR artist LIKE '%it%')
    4. Selected 3262 records.
    5. Unpacking Took: 1.4495204451 seconds
    To copy to clipboard, switch view to plain text mode 

    I have tried using setForwardOnly() but it made no difference.

    Does anyone have any idea on why this is taking so long or how I can improve things?

    -- amicitas

    Hystrix Audio

  2. #2
    Join Date
    Sep 2008
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Update: Extracting QSqlQuery results in PyQt4

    I decided to try just cycling through the code instead of actually extracting the data:

    Qt Code:
    1. while self.query.next():
    2. pass
    To copy to clipboard, switch view to plain text mode 

    Results:
    Qt Code:
    1. Query took: 0.171307952858 seconds
    2.  
    3. SELECT artist,album,title,url,musicbrainz_trackid,hystrix_trackid FROM track
    4. Selected 0 records.
    5. Unpacking Took: 1.24349849063 seconds
    To copy to clipboard, switch view to plain text mode 

    This is certainly faster, but still this is a crazy long time to just to cycle though the records.

    If I use a simplified extraction:
    Qt Code:
    1. while self.query.next():
    2. record = [self.query.value(index) for index in range(len(header))]
    3. results.append(record)
    To copy to clipboard, switch view to plain text mode 

    Results:
    Qt Code:
    1. Query took: 0.00130084772843 seconds
    2.  
    3. SELECT artist,album,title,url,musicbrainz_trackid,hystrix_trackid FROM track
    4. Selected 16667 records.
    5. Unpacking Took: 3.68243214225 seconds
    To copy to clipboard, switch view to plain text mode 


    I also tried doing the loop (including string conversion) with out appending the results. That took 4.5 seconds.

    What this means is that the time break down is:
    1.2 seconds just to loop through the records.
    1.7 seconds to extract the data from the records.
    0.8 seconds to convert to a string.
    0.7 seconds to create the python list.

    While I probably can't reduce the time it takes to create the python list much, is there anyway to reduce the rest of it?

    -- amicitas

  3. #3
    Join Date
    Sep 2008
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extracting QSqlQuery results in PyQt4

    After getting some suggestions on the PyQt mailing list I have done a few more tests.

    I tried using the python packages sqlite3 and also apsw. Using either of these modules the unpacking time is reduced to 1-2 seconds.

    Furthermore just cycling through the results takes 1-1.5 seconds, meaning that adding the results to a list is much quicker than using QSqlQuery ( less than 1 second ).

    The query time is basically unaffected.

    So it looks like the major problem I was having is in how Qt (or PyQT) handles the query extraction (I suspect that part of it has to do with Qt converting everything into QVariants).

    With sqlite3 or apsw things are much more tolerable, though if any one has ideas on how to make the extraction even faster I would love to here them. What I am doing for now is to use:

    Qt Code:
    1. results = [row for row in self.cursor]
    To copy to clipboard, switch view to plain text mode 

    I think if I want my app to feel fast I need to do the query/extraction in the background and return stuff in chunks. I'll have to see how well the gui handles that sort of thing.

    I also need to try using the QSqlQueryModel and see how that works since It will bypass the need for me to get the data into python in the first place.

    -- amicitas

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.