I was wondering if someone could explain why running sql on the android platform inside of a transaction is much, much faster. For instance some code such as:
SQLiteDatabase db = dbHelperExt.getWritableDatabase();
db.beginTransaction();
try {
... // some sql inserting/updating/deleting
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
The speed increase for me has been multiples of times faster when dealing with hundred of inserts and I was mainly curious as to why this is. Also, I haven’t noticed the same sort of increase when dealing with php/myqsl, what’s the deal?
Whenever you perform an operation towards your SQLiteDatabase, a transaction is opened by default, which takes care of opening the file, getting the db object, performing the request and releasing the access to the file.
Moreover as you may notice, the first actions are the heavier to the system.
If you don’t merge different operations executed at the same time in an unique transaction, one will be created for each operation (ie.: 10 operations = 10 transactions with all the work of each of them).
When you use 1 transaction for all you perform 95% of the heavier operations just once.
The only downside of that could be that if one of your requests fails, the rest will be canceled. But to me, this is actually an advantage.
EDIT: Here you have further detailed info.