为什么这个duckdb查询s3/parquet数据无法保存'EXPLAIN ANALYZE'的分析信息?
(更新于3月10日)
根据这篇关于duckdb性能分析的文档,我原以为下面的代码片段应该会把性能分析和计时的统计信息保存到一个叫做query_profile.json
的文件里,然后我可以用python -m duckdb.query_graph query_profile.json
来生成一个html文件。
然而,我下面的代码(可以复现,因为它只是访问一个公共的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}")
1 个回答
1
我觉得这里的问题是,当你运行
con.execute(f"SET profiling_output='{profile_file}'")
con.execute("SET profiling_mode='detailed'")
时,DuckDB会把SELECT/UPDATE/DELETE
语句的性能分析信息输出到一个文件里。如果你运行EXPLAIN
或EXPLAIN ANALYZE
查询,性能分析信息就不会写入到你想要的文件,而是会直接显示在结果中。如果你直接运行SELECT/UPDATE/DELETE
查询,那么性能分析信息就会写入文件,而查询的结果则是你执行的查询的结果。
如果这样解释有帮助,请告诉我。