Qt sqlite Bulk Insert Optimization (SQLite defaults to each statement as a separate transaction) good

Source: Internet
Author: User
Tags bulk insert sqlite

When you use SQLite to store data, you find that the insertion speed is too slow and the program has been running for almost five minutes before inserting less than 3,000. Online search data only found thatsqlite this file database and the MySQL mechanism is different, each transaction has to open and close the file steps, SQLite by default, each statement as a separate transaction. When I insert data, there will be a lot of file IO operation, the efficiency is not high. the insertion efficiency can be significantly improved by putting multiple inserts into a single transaction.

The methods for using transactions in QT are as follows:

Qsqldatabase app_database;
Setting Database Parameters
// ...
App_database.transaction (); Start a transaction
Execute SQL operation
// ...
App_database.commit (); Submit

However, you will need to obtain an auto-generated ID after insertion in your project. It can be obtained with the Qsqlquery::lastinsertid () method, but after a bulk insert using a transaction, Lastinsertid () cannot be used because we need to get the ID set of the inserted data. To assign an ID to an object, you also need to make sure that the one by one correspondence to the object.

Because the parent object in the project contains more than one child object and is saved in the Qvector container, I use the order of the Qvector to guarantee the object's correspondence. Saves the sub-object in the Qvector index in the database, after bulk inserting the child object, the ID of all child objects and index are removed, according to index set the corresponding object ID. This allows you to bulk insert child objects in a transaction and then populate the child object's ID.

http://windrocblog.sinaapp.com/?p=835

Qt sqlite Bulk Insert Optimization (SQLite defaults to each statement as a separate transaction) good

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.