Sub topic 5
Writing Efficient Queries
Assume you know SELECT, WHERE, GROUP BY, and JOIN, and that you understand blocks, transactions, contracts, and token transfers. This chapter focuses on making DuneSQL queries scan less data and return useful results faster.
Start with the smallest possible scan
A query usually becomes expensive when it reads many rows or columns before filtering them. Write filters that match the table's partition columns. A partition is a storage section grouped by a value, commonly a date. Filtering a partition lets Dune skip unrelated sections.
For blockchain tables, common partition columns include block_date and evt_block_date. Use a bounded date range instead of scanning all history.
SELECT
block_date,
project,
SUM(amount_usd) AS volume_usd
FROM dex.trades
WHERE blockchain = 'ethereum'
AND block_date >= current_date - INTERVAL '30' DAY
GROUP BY block_date, project
ORDER BY block_date, volume_usd DESC;This query also uses column pruning. Column pruning means selecting only the columns needed for filtering, grouping, sorting, or display. Avoid SELECT *, especially on wide event tables with many fields.
A filter on a partition column works best when the column remains unchanged. This may prevent efficient partition pruning:
WHERE date_trunc('day', evt_block_time) >= current_date - INTERVAL '7' DAYPrefer filtering the date partition directly, then transforming the timestamp for grouping:
WHERE evt_block_date >= current_date - INTERVAL '7' DAYReduce rows before joining
Joins can multiply the amount of data processed. Filter and aggregate each side before joining when possible. For example, calculate daily transfers first, then join the smaller result to a calendar or price table.
Use the table that already contains the metric you need. Curated tables such as dex.trades usually require less decoding and interpretation than raw logs. Decoded event tables are useful when you need a specific contract event that is not represented in a curated table.
| Technique | Why it helps | Typical use |
|---|---|---|
| Partition filter | Skips unrelated storage partitions | Limit by block date |
| Column pruning | Reads fewer fields | Select only grouping and metric columns |
| Early aggregation | Produces fewer rows for later operations | Sum transfers by day before a join |
| Curated table | Avoids repeated event decoding | Analyze DEX trades |
Aggregate at the right grain
Choose the output grain before writing the query. A daily result needs GROUP BY at the day level. Avoid grouping by transaction hash, wallet, token, and project unless the analysis needs that detail. Each extra grouping column can create many more result rows.
For a transfer table, sum the token's raw integer value only after converting it to human units. If the token uses six decimals, divide by 1e6. Keep the conversion inside the aggregate so the query returns one value per day.
SELECT
evt_block_date AS day,
SUM(CAST(value AS double) / 1e6) AS usdc_transferred
FROM erc20_ethereum.evt_Transfer
WHERE contract_address = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
AND evt_block_date >= current_date - INTERVAL '7' DAY
GROUP BY evt_block_date
ORDER BY day;Inspect and iterate
Run a narrow test first. Use a short date range and a small set of columns to check names, types, and expected row counts. Then expand the range. This avoids spending credits on a query that fails because of a typo or incorrect assumption.
Compare the number of input rows with the number of grouped rows. An unexpectedly large result often indicates a missing filter or an overly detailed grouping key.
A practical query workflow
Exercise
What should you change first if a query scans all Ethereum history but only needs the last week of data?
Show answer
Add a filter on the table's partition column, such as evt_block_date >= current_date - INTERVAL '7' DAY, and select only the columns required by the result.