Slow data insertion in SQLite

Source: Internet
Author: User

The reason why SQLite inserts data slowly: SQLite uses transaction operations for each insert operation when transactions are not explicitly used, while SQLite databases exist on disks as files, it is equivalent to opening a file every time you access the file. If you perform a large amount of operations on the data, the time is spent on I/O operations, so it is very slow.
The solution is to explicitly commit in the form of transactions: Because after a transaction is started, a large number of statements for operations are stored in the memory, and all the statements are written to the database only when the transaction is committed. At this time, the database file is opened only once.
I used 100 s when I did not explicitly insert 12.226 data records in the transaction form. In the explicit transaction form, it took 100 seconds to insert 0.172 data records and 1000000 data records, it's very relevant.
Example of explicitly using transactions:

# Include <iostream> # Include <Windows. h> Using   Namespace  STD; # include  "  SQLite/sqlite3.h  "  Int  Main () {sqlite3 *DB;  Int Nresult = sqlite3_open ( "  Test. DB  " ,& DB );  If (Nresult! = Sqlite_ OK) {cout < "  Failed to open database:  " <Sqlite3_errmsg (db) < Endl;  Return   0 ;}  Else  {Cout < "  Database opened successfully  " < Endl ;}  Char * Errmsg; nresult = Sqlite3_exec (dB, "  Create Table fuck (ID integer primary key autoincrement, name varchar (100 ))  " , Null, null ,& Errmsg ); If (Nresult! = Sqlite_ OK) {sqlite3_close (db); cout < Errmsg; sqlite3_free (errmsg );  Return   0  ;}  String  Strsql; strsql + = "  Begin; \ n  "  ;  For ( Int I =0 ; I < 100 ; I ++ ) {Strsql + = "  Insert into fuck values (null, 'heh'); \ n  "  ;} Strsql + = "  Commit;  "  ;  //  Cout <strsql <Endl;  Systemtime tm_s; getlocaltime ( &Tm_s); nresult = Sqlite3_exec (dB, strsql. c_str (), null, null ,& Errmsg); systemtime tm_e; getlocaltime ( & Tm_e );  If (Nresult! = Sqlite_ OK) {sqlite3_close (db); cout <Errmsg < Endl; sqlite3_free (errmsg );  Return   0  ;} Cout < "  Start:  " <Tm_s.wminute < "  :  " <Tm_s.wsecond < "  :  " <Tm_s.wmilliseconds < Endl; cout < "  End:  " <Tm_e.wminute < "  :  " <Tm_e.wsecond < " :  " <Tm_e.wmilliseconds < Endl;  Return   0  ;}  
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.