ablog

不器用で落着きのない技術者のメモ

パフォーマンス分析に DuckDB を使ってみる

CSV などに出力された性能分析を行う際によく EXCEL を使ったりするが、DuckDB が便利だと聞いてインストールしてみた。

  • インストール
curl https://install.duckdb.org | sh
  • ~/.zshrc に以下を追加
export PATH='~/.duckdb/cli/latest':$PATH
  • 起動する
source ~/.zshrc 
duckdb
  • SQL を実行する
D  select a.query, trunc(a.total_exec_time/1000/1000/60) exec_time,
      b.query_cpu_time,
      b.query_blocks_read,
      b.query_execution_time,
      b.query_cpu_usage_percent,
      b.query_temp_blocks_to_disk,
      b.segment_execution_time,
      b.cpu_skew,
      b.io_skew
  from read_csv('./STL_WLM_QUERY.csv',ignore_errors = true) a, 
      read_csv('./SVL_QUERY_METRICS_SUMMARY.csv',ignore_errors = true) b 
  where a.query =b.query 
      and a.queue_start_time > '2025-06-12' 
  order by a.total_exec_time desc limit 30;
  • SQL の実行結果を CSV で export する。
COPY (
      select a.query, a.xid, b.query_id, a.pid, a.starttime, a.endtime, a.querytxt
      from read_csv('./STL_QUERY.csv',ignore_errors = true) a, 
          read_csv('./SYS_QUERY_HISTORY_trunc.csv',ignore_errors = true) b
      where a.xid =b.transaction_id
          and a.starttime > '2025-06-12'
  ) TO 'QUERY_ID.csv' (HEADER, DELIMITER '|');
||< 


**参考
-[https://duckdb.org/why_duckdb:title]
-[https://duckdb.org/docs/stable/sql/introduction.html:title]
-[https://www.youtube.com/watch?v=bZOvAKGkzpQ:title]