![](/img/trans.png)
[英]How to limit execution time in bigquery client query using python
[英]Need to compute execution time using data in table in BigQuery
我需要使用 Bigquery 中表中的数据计算执行时间(时间戳之间的差异)。 该表包含如下示例数据:
-----------------------------------------------------------
|Command Name | Timestamp | Status |
-----------------------------------------------------------
|Command1 | 2022-11-03T19:16:16.767657+05:30 | START |
-----------------------------------------------------------
|Command1 | 2022-11-03T19:26:16.767657+05:30 | DONE |
-----------------------------------------------------------
|Command2 | 2022-11-03T19:36:16.767657+05:30 | START |
-----------------------------------------------------------
|Command2 | 2022-11-03T19:56:16.767657+05:30 | DONE |
-----------------------------------------------------------
第一列包含命令,第二列包含命令执行的时间戳,最后一列包含相应的状态。 使用此数据,要求计算每个命令的执行时间,定义为:
命令的执行时间 = 状态为 DONE 的命令的时间戳 - 状态为 START 的命令的时间戳。
此数据存储在 BigQuery 中。 由于我是 BigQuery 的新手,任何人都可以帮助我实现这一目标。
感谢和问候,普拉富拉
您可能会考虑使用 window function 进行以下查询,
WITH sample_table AS (
SELECT 'Command1' command, TIMESTAMP '2022-11-03T19:16:16.767657+05:30' timestamp, 'START' status UNION ALL
SELECT 'Command1' command, '2022-11-03T19:26:16.767657+05:30' timestamp, 'DONE' status UNION ALL
SELECT 'Command2' command, '2022-11-03T19:36:16.767657+05:30' timestamp, 'START' status UNION ALL
SELECT 'Command2' command, '2022-11-03T19:56:16.767657+05:30' timestamp, 'DONE' status
)
SELECT command, TIMESTAMP_DIFF(LEAD(timestamp) OVER w, timestamp, SECOND) execution_time
FROM sample_table QUALIFY status = 'START'
WINDOW w AS (PARTITION BY command ORDER BY timestamp);
或使用聚合 function。
SELECT command, TIMESTAMP_DIFF(MAX(timestamp), MIN(timestamp), SECOND) execution_time
FROM sample_table
WHERE status IN ('START', 'DONE')
GROUP BY 1;
查询结果
(更新)计算平均执行时间。
WITH sample_table AS (
-- same as above
)
SELECT AVG(execution_time) avg_execution_tme FROM (
SELECT command, TIMESTAMP_DIFF(LEAD(timestamp) OVER w, timestamp, SECOND) execution_time
FROM sample_table QUALIFY status = 'START'
WINDOW w AS (PARTITION BY command ORDER BY timestamp)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.