Sub topic 12
Using query history and the query profile
This chapter assumes you can write basic SQL, run queries in the SQL editor, and address tables as catalog.schema.table. You will use query history to find a past run and the query profile to see where a query spent its time.
Query history: what ran and how it went
Query history lists the statements that ran on your SQL warehouses. Open Query History in the sidebar. By default you see your own queries. With the right permissions you can also filter by user.
Each row shows the statement text, the user, the warehouse, the start time, and the duration. You can narrow the list with filters:
- Status: running, finished, failed, or canceled.
- User and warehouse.
- Time range.
- Duration, to isolate slow runs.
Use history to answer questions such as "Which query failed at 09:10?" or "Which statements kept the warehouse busy this morning?" Click a row to see its details, including the error message for a failed query.
Opening the query profile
The query profile shows how a finished query executed. To open it, select a query in history and choose View query profile. You can also open it from the result of a run in the SQL editor.
Run this aggregation first so you have something to inspect:
SELECT
pickup_zip,
COUNT(*) AS trips,
ROUND(AVG(fare_amount), 2) AS avg_fare
FROM samples.nyctaxi.trips
GROUP BY pickup_zip
ORDER BY trips DESC
LIMIT 20;The profile shows the query as a tree of operators, such as a scan, an aggregate, and a sort. Each operator reports the time it took and the rows it produced.
Reading the profile
Start from the top-level summary, then move to the operators.
- Check total duration and how it splits into scheduling, planning, and execution. If most of the time was spent waiting for the warehouse to start or for a queue, the query itself is not the problem.
- Find the operator with the largest share of time. That is where tuning pays off.
- Compare rows read with rows returned. A scan that reads many rows and keeps few suggests a missing or weak filter.
- Look at bytes read and whether the data came from cache.
- Look for spilling to disk, which means an operator ran out of memory. That points to a warehouse that is too small for the query.
Plan before you run
EXPLAIN shows the query plan without running the query. Use it to check a statement before you spend compute on it. The profile is the complement, since you read it after a run.
EXPLAIN
SELECT pickup_zip, COUNT(*) AS trips
FROM samples.nyctaxi.trips
GROUP BY pickup_zip;Common mistakes
- Judging a query by its first run. A cold run reads from storage, and a repeat run may hit the cache. Compare several runs before concluding anything.
- Blaming the SQL when the delay was warehouse startup or queueing.
- Resizing the warehouse before you read the profile. A larger size does not help a query whose time goes to one badly filtered scan.
Exercise
A query took 40 seconds. The profile shows 35 seconds in a table scan that read 800 million rows and returned 2 million. What is your first change, and why?
Show answer
Add or tighten a filter, or select fewer columns, so the scan reads less data. The scan dominates the time and keeps few of the rows it reads. A larger warehouse would be a later step.
Exercise
How can you tell that a slow query was delayed by the warehouse rather than by its own SQL?
Show answer
The query profile's time breakdown shows a large share in scheduling or waiting, such as warehouse startup or queueing, and little in execution.