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.
|