Skip to main content
This guide provides practical do’s and don’ts specifically tailored to DuneSQL and Dune’s architecture, allowing you to optimize credit spend.

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 by block_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.

Use Incremental Queries for Recurring Workloads

For scheduled queries or dashboards that refresh regularly, Incremental Queries can reduce costs by up to 100x by only processing new data since your last run. Instead of rescanning years of historical data on every refresh, incremental queries:
  • Load your previous results automatically
  • Query only the new data since your last checkpoint
  • Combine old + new results efficiently
This is ideal for daily aggregations, hourly metrics, and any time-series analysis that runs on a schedule. See the full documentation for more patterns and examples.

Minimize Redundant Lookups in Automated Pipelines

In automated pipelines (dbt model runs, scheduled workflows), minimize redundant metadata and schema lookups such as repeated DESCRIBE, SHOW, and ref/source resolution. Each of these occupies the query engine and consumes credits, so reusing or caching results where possible keeps automated runs lean.

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:
  1. Time filtering with time fields for partition pruning
  2. Column selection to leverage columnar storage
  3. Strategic joins with proper filter placement
  4. Avoiding anti-patterns that prevent optimizations