Sub topic 13
Performance and caching basics
This chapter assumes you can run queries in the SQL editor, read the query profile, and know how warehouse size and auto stop work. It covers the caches a SQL warehouse uses and a few habits that make queries faster.
Two caches that work for you
You do not turn these caches on. SQL warehouses manage them automatically.
- Result cache stores the final result of a query. If you run the same query again and nothing has changed, the warehouse returns the saved result without scanning data.
- Disk cache keeps copies of recently read data files on the local SSDs of the warehouse's compute. A later query that reads the same files reads them from fast local storage instead of cloud object storage.
The result cache saves the most time because it skips execution. The disk cache helps queries that are different but touch the same data.
Seeing the cache work
Run this query twice and compare the durations in query history.
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 second run is usually much faster. Open its query profile to confirm. A result served from the cache is labeled as such, and no files are scanned.
Why you sometimes miss the cache
Most cache misses come from a short list of causes:
- The query text changed. Different columns, a changed filter, or a changed parameter value make a different query.
- The data changed. A write to any table the query reads invalidates the saved result.
- The query is not deterministic. Functions such as
current_timestamp()orrand()produce different values each time, so their results are not reused. - The warehouse restarted. Local caches live on the warehouse's compute and are lost when it stops. A short auto stop timeout means a cold start more often.
Habits that speed up queries
Caching only helps repeated work. For the first run, reduce how much data the query reads:
- Select only the columns you need instead of
SELECT *. - Filter early, preferably on columns the table is clustered or partitioned by, so the engine can skip files.
- Use
LIMITwhile exploring. - Avoid joins that multiply rows. Check the join keys when the row count in the profile grows unexpectedly.
- Compact tables with many small files using
OPTIMIZE. This needs permission to modify the table.
You can inspect the plan before running a query. This shows how the engine intends to scan and aggregate.
EXPLAIN
SELECT pickup_zip, COUNT(*) AS trips
FROM samples.nyctaxi.trips
GROUP BY pickup_zip;After a query has run, the query profile shows the real numbers, such as time per step and rows read. Use EXPLAIN for the plan and the profile for what happened.
Size is not the first fix
A larger warehouse adds compute and can shorten a heavy scan. It does not fix a query that reads too much data. Reduce the data read first, then resize if the query is still slow.
Exercise
You run a query and it takes 20 seconds. You run the identical text again a minute later and it finishes in under a second. Later, a colleague loads new rows into one of the tables and you run it again, and it is slow. Why?
Show answer
The second run was served from the result cache. The load changed the underlying data, which invalidated the saved result, so the query had to execute again. The disk cache may still make it faster than the first run for files that did not change.