You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用boto3 Athena的get_query_execution()未返回EngineExecutionTimeInMillis如何解决

Athena get_query_execution 缺失EngineExecutionTimeInMillis字段解决方案

字段缺失原因

  • 查询未执行完成:当get_query_execution()返回的QueryExecution.Status.State处于QUEUED、RUNNING状态时,引擎还未生成执行统计数据,此时Statistics字典中不会包含EngineExecutionTimeInMillis字段,只有查询进入终态(SUCCEEDED/FAILED/CANCELLED)后才会返回该字段。
  • boto3版本过旧:1.18.0以下版本的boto3对Athena新API字段适配不全,会遗漏部分统计参数,可先执行pip install --upgrade boto3升级到最新版本验证。
  • 特殊查询场景:DDL语句、命中结果缓存的查询可能不会返回该字段,可使用时间戳差值替代计算。

获取查询执行耗时的实现方式

标准读取方案

先轮询查询状态直到进入终态,再读取统计字段,示例代码如下:

import boto3
import time

athena_client = boto3.client('athena')
query_exec_id = "替换为你的查询执行ID"

# 轮询查询状态
while True:
    resp = athena_client.get_query_execution(QueryExecutionId=query_exec_id)
    query_state = resp['QueryExecution']['Status']['State']
    if query_state not in ['QUEUED', 'RUNNING']:
        break
    time.sleep(0.5)

# 读取引擎执行时间
exec_time = resp['QueryExecution']['Statistics'].get('EngineExecutionTimeInMillis')
if exec_time is not None:
    print(f"查询引擎执行耗时:{exec_time}ms")

兼容替代方案

如果确认无法获取EngineExecutionTimeInMillis字段,可以通过查询提交时间和完成时间的差值计算耗时,精度满足绝大多数业务场景需求:

submission_time = resp['QueryExecution']['Status']['SubmissionDateTime']
completion_time = resp['QueryExecution']['Status']['CompletionDateTime']
# 转换为毫秒级整数
calculated_exec_time = int((completion_time - submission_time).total_seconds() * 1000)
print(f"计算得到的查询耗时:{calculated_exec_time}ms")

内容的提问来源于stack exchange,提问作者shivam pandita

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 03:09:01