0

Which will increase my performance or which has faster performance for the following scenario phonegap sqllite db?

Scenario : I have multiple insert statements around 1000 statements.

Approach : 1. Having one transaction [db.transaction] for all the insert statements

db.transaction(function(tx){
   data.forEach(function(dataItem) {     
         tx.executeSql("INSERT INTO test (id, name) VALUES (?,?)", [dataItem.id, dataItem.name]});
}, errorInsertingSectorTable, successInsertSector);}

or 2. for each insert statement having seperate db.transaction

 db.transaction(function(tx){
   data.forEach(function(dataItem) {   
       dbExecution.databaseVar.transaction(function(tx) {
         tx.executeSql("INSERT INTO test (id, name) VALUES (?,?)", [dataItem.id, dataItem.name]});
});}, error, success);}

Which approach has fast performance or have a less overhead for the phonegap app and the best approach? Please help us clarify.

vinod
  • 8,350
  • 9
  • 32
  • 36

1 Answers1

-1

Example of batch insert in sqlite: Is it possible to insert multiple rows at a time in an SQLite database?

It is faster to do a batch insert in a transaction than to run many requests. Imagine how much communication is reduced to a single batch request. If you have many rows you can cut them down to chunks.

EDIT: Because of the fact that suggestion was not enough, let me give you a few more ideas.

  • you need to loop all your data
  • in each iteration add a (?, ?) to your query string
  • in each iteration add the values to an associative array
  • call your function by passing the query string an the values
Community
  • 1
  • 1
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175