# Profile a query Use `profile = query.collect(profile=True).profile`, then print `profile.summary()` to see each operator's input and output rows, worker time, I/O and spilling. Operator wall times add time across workers and may exceed elapsed query time. An operator’s memory peak records query working bytes while that operator allocates. `profile.to_dict()` returns the counters and phase spans. Save `profile.to_chrome_trace_json()` to open the timeline in Perfetto, or send `profile.to_otel_json()` to an OTLP/HTTP trace endpoint as `application/json`. For streamed results, read `stream.profile` after EOF or `close()`. `query.sink_parquet(path, profile=True)` returns the writer and query profile. A scan records reads during execution. Footer reads used to construct the query occur before profiling starts. `FilterAggregate` measures a fused predicate and partial sum. Its output row count is the number of rows that passed the predicate. Trace storage grows within the query memory budget. The timeline retains up to 65,536 phase spans and may retain fewer under memory pressure. `spans_dropped` counts the rest. Operator counters continue throughout. A retained profile counts toward the runtime’s caller-held memory until you release it. ## Save and compare profiles Run a TPC-H query from the source checkout against a generated dataset. ``` uv run --no-sync python benchmarks/tpch/profile.py --scale 10 --query Q09 \ --data-dir build/tpch-data/sf10 --out build/q09-before ``` Use `--query Q09,Q21` or `--query all` to write one subdirectory per query. Each TPC-H result must match its DuckDB golden. For your own query, pass `--script query.py` and define a function named `query(session)` that returns a Quixote query. Set `--threads`, `--io-threads`, `--memory-limit-gib` and `--spill-dir` to reproduce the runtime settings. Read `summary.txt` for the top five operators by share of query CPU. The bundle includes counters in `profile.json`, OTLP in `otel.json`, a Perfetto timeline in `trace.json` and run settings in `metadata.json`. Use `--cpu-profile` with the `profile` CMake preset to collect native CPU samples. Capture a second run in another directory to compare the counters. ``` uv run --no-sync python benchmarks/tpch/profile.py \ --compare build/q09-before build/q09-after --format markdown ``` The table shows head minus base. It matches physical operators by ID, name, detail and parent. Added and removed operators appear on separate rows. Omit `--format markdown` for plain text.