防止MySQL查询在Apps脚本中超时

时间:2018-11-08 14:24:34

标签: mysql google-apps-script google-sheets

我们客户的在线商店使用Opencart,出于营销目的,他们希望将Opencart订单与Google表格自动同步。我修改了Google表格中的脚本,以提取过去一年的客户订单(基于https://www.transpacific-software.com/blog/opencart-to-google-docs-pull-and-sync-data-auto-through-scriptinghttps://gist.github.com/pradeepbheron/e6129c9fd9bc74e814d0

SQL查询为:

SELECT order_id, firstname AS first_name, lastname AS last_name, 
email, date_added AS order_date, CONCAT("£", FORMAT(total,2)) 
order_value, payment_address_1 AS billing_address_1, 
payment_address_2 AS billing_address_2, payment_city AS 
billing_city, payment_postcode AS billing_postcode, 
payment_country AS billing_country, items_purchased FROM 
(
    SELECT order_id, firstname, lastname, email, date_added, 
    total, payment_address_1, payment_address_2, payment_city, 
    payment_postcode, payment_country 
    FROM ocbw_order 
    GROUP BY order_id
) AS A 
JOIN (
     SELECT order_id AS product_order_id, GROUP_CONCAT(name 
     SEPARATOR ", ") AS items_purchased 
     FROM ocbw_order_product 
     GROUP BY product_order_id
) AS B 
     ON A.order_id=B.product_order_id 
     WHERE date_added >= DATE_SUB(NOW(),INTERVAL 1 YEAR) 
     AND firstname != ''

它可以在phpMyAdmin中正常运行,但Google脚本编辑器会生成“超出最大执行时间”错误。

似乎有7357行(从myPhpAdmin导出)。有没有更好的方法来编写查询?另外,我试图重命名列标题,但只能进行两项操作,即:

GROUP_CONCAT(name SEPARATOR ", ") AS items_purchased 

CONCAT("£", FORMAT(total,2)) order_value

任何想法

快速更新:2018年11月9日星期五11:54:03

1)应@ThisGuyHasTwoThumbs的要求,以下是说明表结果的屏幕截图

enter image description here

2)我研究了@bcperth提到的最佳实践文档。我试图重写Google表格脚本,但遇到了问题。

这是修改后的脚本。

function p1MySQLFetchData() { 

    // Change it as per your database credentials
    var conn = 
    Jdbc.getConnection('jdbc:mysql://[dbHostIp]:3306/[dbName]', 
    '[dbUsername]', '[dbPassword]'); 

    var stmt = conn.createStatement();
    var start = new Date(); // Get script starting time

    //change table name as per your database structure
    var rs = stmt.executeQuery('[sqlQuery]'); 
    // It sets the limit of the 
    // maximum nuber of rows in a ResultSet object

    // Returns the currently active spreadsheet
    var doc = SpreadsheetApp.getActiveSpreadsheet(); 
    var cell = doc.getRange('a1');
    var row = 0;

    // Mysql table column name count.
    var getCount = rs.getMetaData().getColumnCount(); 

    // Create array to hold mysql data
    var tempArray = []; 

    // get row and column count for later
    var colCount = getCount;

    // ATTEMPT TO GET ROW COUNT 1 
    //rs.last();
    //var rowCount = rs.getRow();
    //rs.beforeFirst(); // resets rs cursor
    //Logger.log(RowCount); 
    // DOESN'T WORK! result => ReferenceError: "RowCount" is not 
    // defined. (line 28, file "Code")

    // ATTEMPT TO GET ROW COUNT 2
    //var rsCount = stmt.executeQuery('SELECT COUNT(*) AS rowcount FROM 
    //[sqlQuery]); 
    // It sets the limit of the maximum number of rows in a ResultSet 
    // object
    //rsCount.next();
    //var rowCount = rsCount.getString("rowcount");
    //Logger.log(rowCount); 
    // DOESN'T WORK!  result => 0


    // Build TempArray using MySQL data
    for (var i = 0; i < getCount; i++){ 
        tempArray[0][i] = rs.getMetaData().getColumnName(i+1); 
        // DOESNT WORK => ERROR 
        // TypeError: Cannot set property "0.0" of undefined to "order_id". 
        // (line 39, file "Code")
        //Logger.log(rs.getMetaData().getColumnName(i+1));
    }  

    var row = 1; 
        while (rs.next()) {
           for (var col = 0; col < rs.getMetaData().getColumnCount(); 
           col++) { 
                tempArray[row][col] = rs.getString(col + 1);
                //Logger.log(rs.getString(col + 1));
            }
        row++;
    }

    // BELOW DOESNT AS I CANT GET A ROW COUNT (SEE ABOVE)
    // Fill Spreadsheet from tempArray 
    //for (var row = 0; row < rowCount; row++) {
        //for (var col = 0; col < colCount; col++) {
            //cell.offset(row, col).setValue(tempArray[row][col + 1]);
        //}
    // }

    rs.close();
    stmt.close();
    conn.close();
    var end = new Date(); // Get script ending time
    Logger.log('Time elapsed: ' + (end.getTime() - start.getTime())); 
    // To generate script log. To view log click on View -> Logs.
}

但是正如您从注释中看到的那样,我遇到了很多错误。不知道下一步该怎么做。

更新2018年11月30日星期五15:26:14

回答下面的@Tedinoz评论。

PhpMyAdmin生成6862个结果,查询花费了13.3074秒。

当我在Google脚本编辑器中运行脚本时,大约花了2分钟30来完成,只提取了6348条记录(92.5%)。记录在2018年10月3日之后停止。

关于您的建议:

1)我尝试运行修改后的脚本,将INTERVAL设置为:

  • 1个月=>我在Google上没有结果(应该是529)
  • 2个月=>我得到14个结果(应该是1029)
  • 3 MONTH =>我得到299个结果(应该是1669)。

在myPhpAdmin中,它们全部花费了大约4-7秒,而对于Google脚本编辑器,它们花费了5-20秒

2)您的意思是从phpMyAdmin导出csv并导入到Google表格吗?好吧,我做到了,而且效果很好。

我注意到的另一件事是Google表格中的order_id与phpMyAdmin中的不匹配。很奇怪。

0 个答案:

没有答案