Optimize queries
Reduce query work and compare runtime statistics.
Optimize queries
Measure query work before you change a query. Compare equivalent results on the same data.
Query best practices
Use this checklist before you run a query on a large table.
- Select only the paths that the result needs.
- Add time or key filters when the result needs a subset.
- Add
LIMITwhile you explore rows. - Aggregate data in SQL before you return rows to a client.
- Add
ORDER BYwhen row order matters. - Use
EXPLAIN indexes = 1to inspect index use. - Compare
statistics.rows_readandstatistics.bytes_readfor equivalent queries. - Check
dynamicTypewhen a path contains mixed types.
Before you begin
- Use the
analyticsdatabase. Create it with the database API if it does not exist. - Use a
requeststable withevent_time,service, andduration_mspaths. - Send
event_timeas Unix time in seconds.
1. Select only required paths
Name the paths that the result needs.
SELECT service, duration_ms FROM requests LIMIT 202. Add a time filter
Filter on a time range to reduce the rows that the query reads.
SELECT count() FROM requests WHERE event_time >= toUnixTimestamp(now() - INTERVAL 1 DAY)3. Bound exploratory queries
Use LIMIT while you inspect rows. Add ORDER BY when row order matters.
SELECT service, duration_ms FROM requests ORDER BY event_time DESC LIMIT 1004. Aggregate before you return rows
Group data in SQL instead of returning every event to the client.
SELECT service, count() AS requests, avg(duration_ms) AS mean_duration FROM requests GROUP BY service ORDER BY requests DESC LIMIT 205. Inspect index use
Use EXPLAIN indexes = 1 to inspect the plan.
EXPLAIN indexes = 1 SELECT count() FROM requests WHERE event_time >= toUnixTimestamp(now() - INTERVAL 1 DAY)6. Compare query statistics
Compare statistics.rows_read and statistics.bytes_read for equivalent
filtered and unfiltered queries.
Check mixed types
Use dynamicType to inspect a field before you compare, sort, or aggregate mixed values.
Choose a conversion or exact extraction that matches the field's meaning.
Inspect hints for runtime type-compatibility warnings about values that an operation skips or replaces.
See Query fields with mixed types for examples of type inspection, conversion, and warning checks.
Check the result
The filtered query must read fewer rows than the unfiltered query.
The verifier compares the rows_read values.