SQLite Bulk Insert Optimization method

Source: Internet
Author: User
Tags bulk insert sqlite database

SQLite database is essentially a disk file, so all the database operation will be converted to the operation of the file, and frequent file operations will be a good process, will greatly affect the speed of database access. For example, insert 1 million data into the database and, by default, if only the execution
Sqlite3_exec (db, "INSERT into name values ' lxkxf ', '"; ", 0, 0, &zerrmsg);
The database file will be opened repeatedly 1 million times, so the speed of course will be very slow. So we should use "transaction" for this situation.
Here's how to do this: before executing the SQL statement and after executing the SQL statement, add
rc = sqlite3_exec (db, "BEGIN;", 0, 0, &zerrmsg);
Execute SQL statement
rc = sqlite3_exec (db, "COMMIT;", 0, 0, &zerrmsg), so SQLite will put all the SQL statements to be executed in memory first, and then wait until COMMIT to write to the database once, In this way, the database file is only opened and closed once, the efficiency naturally greatly improved. There is a set of data comparisons: Test 1:1000 Inserts
CREATE TABLE T1 (a integer, b integer, C VARCHAR (100));
INSERT into T1 VALUES (1,13153, ' Thirteen thousand one hundred fifty Three ');
INSERT into T1 VALUES (2,75560, ' Seventy five thousand five hundred sixty ');
... 995 lines omitted
INSERT into T1 VALUES (998,66289, ' Sixty Six thousand, Hundred Eighty Nine ');
INSERT into T1 VALUES (999,24322, ' Twenty four thousand three hundred Twenty ');
INSERT into T1 VALUES (1000,94142, ' Ninety four thousand one hundred Forty ');
SQLite 2.7.6:
13.061
SQLite 2.7.6 (nosync):
0.223
Test 2: Using transaction 25000 inserts
BEGIN;
CREATE TABLE T2 (a integer, b integer, C VARCHAR (100));
INSERT into T2 VALUES (1,59672, ' Fifty nine thousand six hundred Seventy ');
... 24997 lines omitted
INSERT into T2 VALUES (24999,89569, ' Eighty nine thousand five hundred sixty ');
INSERT into T2 VALUES (25000,94666, ' Ninety four thousand six hundred sixty Six ');
COMMIT;
SQLite 2.7.6:
0.914
SQLite 2.7.6 (nosync):
0.757
It can be seen that the use of transactions has greatly improved the efficiency of the database. However, we also have to note that the use of transactions is also a certain cost, so for the small amount of data operations can not be used, so as not to cause the consumption of the outer.
internalstaticvoid Fastinsertmany (DbConnection CNN) {using(Dbtransaction Dbtrans =CNN. BeginTransaction ()) {using(DbCommand cmd =CNN. CreateCommand ()) {Try{Cmd.commandtext="INSERT into TestCase (myvalue) VALUES (?)";D bparameter Field1=cmd. CreateParameter (); cmd. Parameters.Add (Field1); for(intn =0; N <100000; n++) {Field1.value= n +100000; cmd.  ExecuteNonQuery ();}}   Dbtrans.commit (); }Catch{dbtrans.rollback (); }}}

SQLite Bulk Insert Optimization method

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.