问题
I am using ionic v1 and angularjs and add value into database by using foreach loop but its getting slow to me(500 times execute this loop).
Factory 1:
self.query = function(query, parameters) {
return $cordovaSQLite.execute(db, query, parameters).then(function(result) {
//console.log(result);
return result;
}, function (error) {
console.log(error);
return error;
});
};
Factory 2:
self.add = function (url,data) {
return DBA.query("INSERT OR REPLACE INTO offline_data (id, data) VALUES (?, ?)", [url, JSON.stringify(data)]);
};
adding db via:
angular.forEach(result.style_list, function(value){
OfflineDataService.add(value.style_id, value)
});
how we can faster....
回答1:
Sounds like you need to find a way to do a bulk insert, ie execute many inserts in a single SQL statement:
INSERT OR REPLACE INTO offline_data (id, data)
VALUES (?, ?), (?, ?), (?, ?), ...
This will depend on your DB and what it supports, but if it does then you can just batch up your data to insert into say batches of 50 rows at a time.
来源:https://stackoverflow.com/questions/48396222/write-multiple-row-at-a-time