English 中文(简体)
QSqlTableModel.insertRecord() is very slow
原标题:

I am using PyQt to insert records into a MySQL database. the code basically looks like

self.table = QSqlTableModel()
self.table.setTable( mytable )
while True:
  rec = self.table.record()
  values = getValueDictionary()
  for k,v in values.items():
    rec.setValue(k,QVariant(v))
  self.table.insertRecord(-1,rec)

The table currently has ~ 50,000 rows in it. I have timed each line and found that the insertRecord function is taking ~5 seconds to execute, which is unacceptably slow. Everything else is fast.

For comparison, I also made a version of the code that uses

QSqlQuery.prepare("INSERT INTO mytable (f1,f2,...) VALUES (:f1, :f2,...)")
query.bindValue(":f1",blah)
query.exec_()

In this case, the whole thing takes only ~ 20 milliseconds, so the delay is not in the database connection as far as I can tell.

I d really prefer to use the QtSql stuff instead of the awkward MySQL commands. Any ideas on how to add a bunch of rows to a MySQL database with QtSql instead of raw comands and with reasonable speed?

Thanks, G

问题回答

Things to try:

and see if it helps...

you should use begin before and commit after the loop, or turn off the autocommit feature from MySQL .. this will give you usually a performance increase of 50% or more ..





相关问题
What to look for in performance analyzer in VS 2008

What to look for in performance analyzer in VS 2008 I am using VS Team system and got the performance wizard and reports going. What benchmarks/process do I use? There is a lot of stuff in the ...

SQL Table Size And Query Performance

We have a number of items coming in from a web service; each item containing an unknown number of properties. We are storing them in a database with the following Schema. Items - ItemID - ...

How to speed up Visual Studio 2008? Add more resources?

I m using Visual Studio 2008 (with the latest service pack) I also have ReSharper 4.5 installed. ReSharper Code analysis/ scan is turned off. OS: Windows 7 Enterprise Edition It takes me a long time ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

热门标签