What is faster, one transaction with many queries or for every query a separate transaction?
One transaction with many queries:
db.transaction(function (tx){
tx.executeSql('INSERT INTO TEST VALUES ("Halo1" ,"Halo2")', [], win, fail);
tx.executeSql('INSERT INTO TEST VALUES ("Halo3" ,"Halo4")', [], win, fail);
tx.executeSql('INSERT INTO TEST VALUES ("Halo5" ,"Halo6")', [], win, fail);
.
.
.
.
});
One transaction per query:
db.transaction(function(tx){
tx.executeSql('INSERT INTO TEST VALUES ("Halo1" ,"Halo2")', [], win, fail);
});
db.transaction(function(tx){
tx.executeSql('INSERT INTO TEST VALUES ("Halo3" ,"Halo4")', [], win, fail);
});
and so on.....
I need this info, because i have to insert many records in one table.
It’s a trade-off. It’s almost certainly going to be faster doing one transaction with lots of statements since you’re avoiding the overhead of committing those transactions until the end, but measure, don’t guess!
On the other hand, you don’t want to put too much into a single transaction since the DBMS has to maintain control information about the transaction (rollback or rollforward logs and so on).
I would usually opt for a halfway position. Do your inserts in a function which automagicaly commits every
Ninserts. Something like the pseudo-code:Just make sure you’re not circumventing the whole reason transactions were created. They are the
Ain yourACIDproperties (atomicity). If the inserts are required to be done as a single transaction to maintain the data properly, then do them that way.