When doing the backup and restore function of SMS on Android, the idea of SMS recovery is very simple, the loop parsing file, each get a text message, call Smsprovider Insert method to insert the text message into the database, Smsprovider is the most basic class of SMS database operation, The Query,insert,delete and update methods of the parent class ContentProvider are overloaded, and in addition to the Insert method, there is a Bulkinsert method in the parent class ContentProvider, which is a bulk insert with the following code:
<span style= "FONT-SIZE:14PX;" >public int Bulkinsert (URI uri, contentvalues[] values) { int numvalues = values.length; for (int i = 0; i < numvalues; i++) { Insert (URI, values[i]); } return numvalues; } </span>
It can be seen that the function is simply a circular call to the Insert method, and insert is a virtual method, so the actual use will call to the Smsprovider insert method, and there is no speed advantage;
In the actual use of the process, the number of messages to restore a long time, the speed is a bit unbearable, decided to consider the speed of optimization;
Android database using ingenious Sqlite3, fortunately Sqlite3 is a support transaction, naturally think of the benefits of unified transaction to BULK INSERT, this is the first direction to try speed optimization,
<span style= "FONT-SIZE:14PX;" >db.begintransaction (); try { //here database operation db.settransactionsuccessful ();//Do not forget this phrase} catch (XXX) { //xxxxx} finaly { Db.endtransaction (); Commit}</span>
Poses other risks that are not allowed for SMS applications.
..........................................................................................................................................................................................
This was written very early, and the current attempt failed to write the reason:
The above also mentioned Bulkinsert this function, he does not use unified transaction method to bulk insert SMS, but loop call insert, and there is no speed on the advantages and considerations, I think Android developers, it is impossible not to know Sqlite3 unified Transaction method, Then why do they have to leave it??
The reason is that if the use of unified transactions brings another risk-the loss of the received text messages, the opening of the transaction is to lock the DB, that is, after the opening of a transaction, the other database write operations are not successful, so that if in a unified transaction operation of a lot of data, need to consume a certain amount of time, Then the text message arrives, it is unable to insert into the database, also will not receive this text message, and the text message restores this operation slow to compare, this more cannot be allowed; another risk is a deadlock, in the end because the Sqlite3 support for concurrency is not very good;
After my actual test, found that when the original data in the database is not a long time, the method of unified transaction batch insertion consumption is quite small, but with more and more data, will be more and more slowly ~ ~ ~ Better way to realize their own slowly groping it, the above attempt to learn it ....
Android developers come in, help solve------------android SMS batch speed optimization