为什么 s3/parquet 数据的 duckdb 查询不会保存“EXPLAIN ANALYZE”分析信息?

问题描述 投票:0回答:1

(3/10 更新)

基于这个关于分析的 duckdb 文档页面,我认为下面的代码片段应该将分析/计时统计数据的 json 文件保存到

query_profile.json
,我应该能够使用它来生成一个 html 文件
python -m duckdb.query_graph query_profile.json

但是,我下面的代码(可重现,因为它只是命中公共 s3 存储桶,尽管您需要在自己的 .env 文件中使用自己的 aws 凭证)不会生成这样的

query_profile.json
文件:

import duckdb
import s3fs
from dotenv import dotenv_values

# load environment variables from .env file
ENV = dotenv_values(".env")

# Configurable query params
TAXI_COLOR = "yellow"
YEAR = 2023
PROFILE = True

# where to save result (data) locally
dbfile = 'taxi_data.duckdb'

# where to save profiling results
profile_file = 'query_profile.json'

# Define the S3 glob pattern to match the desired parquet files
s3_glob_path = f"s3://nyc-tlc/trip data/{TAXI_COLOR}_tripdata_{YEAR}*.parquet"

# query the s3 parquet data using duckdb
with duckdb.connect(database=dbfile) as con:
    
    # load extension required for reading from s3
    con.execute("INSTALL 'httpfs';")
    con.execute("LOAD 'httpfs';")

    # Set the AWS credentials to access the S3 bucket
    con.execute("SET s3_region='us-east-1';")
    con.execute(f"SET s3_access_key_id = '{ENV['AWS_ACCESS_KEY_ID']}';")  
    con.execute(f"SET s3_secret_access_key = '{ENV['AWS_SECRET_ACCESS_KEY']}';")  

    # Enable profiling and save the profiling results directly to a file
    con.execute(f"SET profiling_output='{profile_file}'")
    con.execute("SET profiling_mode='detailed'")

    # Execute the query to load and save the data directly to the specified DuckDB file
    tablename = f'{TAXI_COLOR}_tripdata_{YEAR}'
    ea = "EXPLAIN ANALYZE " if PROFILE else ""
    query = f"""{ea}CREATE OR REPLACE TABLE {tablename} AS
                SELECT * FROM read_parquet(['{s3_glob_path}'])
            """
    print(query)
    con.execute(query)

print(f"Data saved to {dbfile} as {tablename}")
print(f"Profiling results saved to {profile_file}")
python amazon-web-services amazon-s3 profiling duckdb
1个回答
0
投票

我认为这里的问题是当你跑步时

con.execute(f"SET profiling_output='{profile_file}'")
con.execute("SET profiling_mode='detailed'")

DuckDB 会将

SELECT/UPDATE/DELETE
语句的分析信息输出到文件中。如果您运行
EXPLAIN
EXPLAIN ANALYZE
查询,分析信息将不会写入所需的文件,而是显示在结果中。如果您直接运行
SELECT/UPDATE/DELETE
查询,则分析信息将写入文件。结果就是查询的结果。

如果有帮助请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.