Improve SQLite performance and a simple understanding of transactions

Source: Internet
Author: User
During SQLite transaction processing, a default statement is a transaction when SQLite inserts data, disk operations such as 5000 records mean 5000 disk read/write operations.


Solution:


Add transaction processing and insert 5000 records as a transaction


Database. begintransaction (); // manually set the start transaction


// Data insertion operation cycle


Database. settransactionsuccessful (); // sets whether the transaction is successfully processed. If this parameter is not set, the system automatically rolls back and does not submit the transaction.

// Transaction operations cannot be performed between them.

Database. endtransaction (); // processing completed


Db.exe csql ("insert into user values (4, 'zhang San 2 ')");
Db.exe csql ("insert into user values (4, 'Li Si 2 ')");
Db.exe csql ("insert into user values (4, 'wang Wu 2 ')");
Db.exe csql ("insert into user values (4, 'zhao liu2')"); DB. begintransaction (); // enable transaction
Long time1 = system. currenttimemillis ();
Try {
Db.exe csql ("insert into user values (1, 'zhang san ')");
Db.exe csql ("insert into user values (2, 'lily ')");
Db.exe csql ("insert into user values (3, 'wang wu ')");
Db.exe csql ("insert into user values (4, 'zhao liu ')");
DB. settransactionsuccessful ();
} Catch (sqlexception e ){
E. printstacktrace ();
DB. endtransaction ();
}
DB. endtransaction ();
Long time2 = system. currenttimemillis ();
System. Out. println ("consumed time:" + (time2-time1 ));

Time consumed: 4 or 10.

Remove the transaction execution result:


Time consumed: 76 or 128.

So as I mentioned above, "the problem of SQLite transaction processing, when SQLite inserts data, a default statement is a transaction, and how many disk operations are performed for how many data records"

In summary: To improve the performance, it is best to implement transaction control when performing multi-step operations on data tables, which can greatly improve the performance. Another important factor is that data is complete.





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.