[英]Large data set exceeding maximum execution time
I have a script that is loading the contents of a spreadsheet into scriptDB but the spreadsheet has around 15,000 rows, 9 columns and keeps giving me an "exceeded maximum execution time" error. 我有一个脚本将电子表格的内容加载到scriptDB中,但电子表格大约有15,000行,9列并且不断给出“超出最大执行时间”错误。
I'm using the function given in Google's documentation to load the data: 我正在使用Google文档中提供的功能来加载数据:
function loadDatabaseFromSheet() {
var spreadsheet = SpreadsheetApp.openById(SPREADSHEET_KEY);
var sheet = spreadsheet.getActiveSheet();
var columns = spreadsheet.getLastColumn();
var data = sheet.getDataRange().getValues();
var keys = data[0];
var db = ScriptDb.getMyDb();
for (var row = 1; row < data.length; row++) {
var rowData = data[row];
var item = {};
for (var column = 0; column < keys.length; column++) {
item[keys[column]] = rowData[column];
}
db.save(item);
}
}
Is there any way to speed things up or am I just going to have to break it up into chunks of a few thousand? 有什么方法可以加快速度,或者我只是要把它分解成几千块?
Calling db.save(item)
15000 times is what is causing the slowness. 调用db.save(item)
15000次是造成缓慢的原因。 Instead, use bulk operations if you're going to be saving that much data. 相反,如果您要保存那么多数据,请使用批量操作 。
var db = ScriptDb.getMyDb();
var items = [];
for (var row = 1; row < data.length; row++) {
var rowData = data[row];
var item = {};
for (var column = 0; column < keys.length; column++) {
item[keys[column]] = rowData[column];
}
items.push(item);
}
db.saveBatch(items,false);
Calling the save operation once at the end saves you all of the round-trip times, so this should speed up your code a lot and finish before it exceeds the maximum execution time. 最后一次调用保存操作可以节省所有往返时间,因此这样可以大大加快代码的速度,并在超过最大执行时间之前完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.