简体   繁体   English

超过最大执行时间的大数据集

[英]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.

相关问题 在不超过最大执行时间的情况下列出谷歌驱动器中的所有文件 - List all files in google drive without exceeding maximum execution time 无能为力-超过执行时间 - Clueless - Exceeding execution time Google Apps Script Calendar Service:如何避免超过最大执行时间? - Google Apps Script Calendar Service: How to avoid exceeding maximum execution time? Google Sheet Apps 脚本超出最长时间 - Google Sheet Apps Script Exceeding maximum time 从 Pipedrive API 获取数据时超出请求速率限制和自定义 function 最大执行时间 - Exceeding request rate limit and custom function max execution time when fetching data from Pipedrive API 超过最大执行时间 - Exceeded maximum execution time 合并 GSheet 数据时超出了 Google 脚本的最长执行时间 - Exceeded Maximum Execution Time for Google Scripts while consolidating GSheet Data 在大型电子表格中,基于单元格值隐藏行会生成错误“超出最大执行时间” - Hiding row based on cell value, in large spreadsheet generates error “Exceeded Maximum Execution Time” Google脚本,按顺序运行函数而不会超过执行时间 - Google Script, Run functions in sequence without exceeding execution time 将Google表格递归转换为Excel时避免超过执行时间 - Avoiding exceeding execution time when recursively converting Google Sheets to Excel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM