Key Principles
Dune’s query engine is optimized for blockchain data analysis with time-partitioned tables and columnar storage. Understanding the DuneSQL architecture will help you write queries that execute faster and consume fewer resources.✅ DO’s: Query Optimization Best Practices
1. Leverage Time-Based Partitioning
Dune partitions most tables byblock_date
or by block_time
Check on the data explorer (left-side panel) to see which fields are used as partitions.
Always include time filters to enable partition pruning.
Cross-chain tables: Large cross-chain tables like tokens.transfers
, dex.trades
, and evms.erc20_evt_transfers
are most often also partitioned by blockchain
in addition to time. Wherever possible, specify the blockchain filter along with time filters to dramatically reduce the amount of data scanned.
2. Select Only Required Columns
Specify only the columns you need. Dune’s columnar storage makes this especially effective.3. Use Efficient JOIN Strategies
Put time filters in the ON clause and join on indexed columns when possible.4. Use CTEs for Complex Logic
Break complex queries into readable Common Table Expressions.5. Use LIMIT with Large Result Sets
Always use LIMIT when you don’t need all results, especially for exploratory queries.Use Curated Data Tables
Leverage Dune’s curated tables which are pre-computed and optimized rather than use raw logs and traces.Use Materialized Views
Materialized views are a way to store the results of a query in a table that can be queried like any other table. This is useful when you have a query that takes a long time to run, as you can re-use the results without having to re-execute your whole query.Query Performance Troubleshooting
- Monitor query execution time in the Dune interface
- Check the query plan for full table scans (run EXPLAIN ANALYZE YOUR_QUERY)
- Consider query complexity vs. result value trade-offs
❌ DON’Ts: Query Anti-Patterns to Avoid
1. DON’T Use SELECT *
on Large Tables
Avoid selecting all columns, especially on transaction and log tables.
2. DON’T Create Unnecessary Subqueries
Avoid nested subqueries when JOINs or window functions work better.3. DON’T Use ORDER BY Without LIMIT on Large Results
Sorting large result sets is expensive.Summary
Efficient DuneSQL queries on Dune require understanding the platform’s time-partitioned architecture. Focus on:- Time filtering with time fields for partition pruning
- Column selection to leverage columnar storage
- Strategic joins with proper filter placement
- Avoiding anti-patterns that prevent optimizations