Writing On-Chain SQL Queries in Dune

Writing On-Chain SQL Queries in Dune

A comprehensive guide to harnessing the power of Dune’s on-chain SQL engine for blockchain analytics and dashboarding.

Introduction

In the evolving world of blockchain analytics, Dune has emerged as a leading platform that democratizes access to on-chain data through an intuitive SQL interface.
This article explores the end-to-end process of writing on-chain SQL queries in Dune, from environment setup to advanced performance tuning,
and illustrates real-world examples for Ethereum, Polygon, and other chains.

What is Dune

Dune is a powerful analytics platform designed specifically for blockchain data. By providing a SQL-based query engine on top of fully indexed
on-chain data, Dune enables analysts, developers, and researchers to extract insights without running their own nodes or ETL pipelines.

  • On-Chain Data Indexing: Complete historical indexing of blocks, transactions, events, logs.
  • SQL Interface: Flexible, familiar language for data exploration.
  • Visualization Dashboards: Combine multiple queries into interactive dashboards.
  • Collaboration: Public queries and community-contributed views.

Dune’s Data Model and Schema

Understanding the schema is crucial. Dune organizes data into schemas like ethereum, polygon, etc. Within each schema, key tables include:

Table Description Key Columns
blocks All chain blocks number, timestamp, miner
transactions Transactions per block hash, from_address, to_address, value
logs Event logs (topics, data) address, topics, data

For an up-to-date schema reference, consult the official documentation
here.

Setting Up Your Dune Workspace

  1. Create an Account: Sign up on dune.com.
  2. Start a New Query: Click “New Query” and select your target schema (e.g., ethereum).
  3. Familiarize with the IDE: The editor supports autocomplete for table names, columns, and SQL functions.
  4. Save Version: Use the version control features to iterate safely.

Essentials of On-Chain SQL Queries

While traditional SQL skills apply, on-chain analytics introduces unique patterns:

  • Filtering by Block Range: Use WHERE block_number BETWEEN X AND Y for time-bounded analyses.
  • Decoding Logs Events: Leverage the decode_event function to parse ABI-encoded data.
  • Joining on Addresses: Join transactions with logs on transaction_hash to aggregate token transfers.
  • Working with Big Integers: Use to_number or to_numeric to convert hex or string types.

Sample Query: Uniswap V2 Swap Volume

SELECT
nbspnbspdate_trunc(day, b.timestamp) AS day,
nbspnbspSUM(to_number(data->>amount0) price0 to_number(data->>amount1) price1) AS daily_volume_usd
FROM
nbspnbspethereum.logs l
JOIN
nbspnbspethereum.blocks b ON l.block_number = b.number
WHERE
nbspnbspl.address = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f– Uniswap V2 Factory
AND
nbspnbspl.topics[1] = 0xd78ad95fa46c994b6551d0da85fc275fe613361f2cfb05bd9e212cf0f1b0d4d5– SwapTopic
GROUP BY 1
ORDER BY 1

Key SQL Functions and Operators

  • date_trunc(unit, timestamp): Bucket timestamps by hour/day/month.
  • to_number(string): Convert string or hex to numeric types.
  • ucase/lcase(string): Normalize text fields.
  • lag/lead(): Window functions for time-series deltas.
  • decode_event(data, abi_definition): ABI-decode complex event payloads (docs).

Advanced Query Techniques

Once comfortable, explore:

  • Recursive CTEs: Track address activity over successive blocks.
  • Materialized Views: Persist intermediate results for faster dashboards.
  • Dynamic Filtering: Parameterize queries with Dune variables for interactive dashboards.
  • Cross-Chain Joins: Combine tables from multiple schemas (e.g., ethereum polygon).

Performance Optimization Strategies

Tip: Query performance on large data sets depends on appropriate filtering and avoiding full-table scans.
  • Limit Scans: Always include WHERE block_number gt= X.
  • Selective Projections: Only include necessary columns.
  • Index-Friendly Joins: Join on indexed columns (e.g., transaction_hash).
  • Use LIMIT OFFSET Wisely: For pagination, prefer keyset pagination over large OFFSET.
  • Caching: Leverage Dune’s query result cache—re-run identical queries to benefit from speed-ups.

Best Practices Common Pitfalls

  • Schema Versioning: Watch for breaking changes in Dune’s schema updates.
  • Time Zones: Use UTC consistently to avoid DST-related offsets.
  • Data Freshness: On-chain data may lag by a few minutes—account for this in real-time dashboards.
  • Resource Limits: Queries have time and memory limits break complex logic into smaller steps or materialized views.

Real-World Query Examples

Below are example analyses you can adapt:

  • Daily Active Users (DAU): Count unique from_address in transactions per day.
  • Top Protocols by TVL: Join deposits and withdrawals logs to compute TVL for lending protocols.
  • NFT Sales Heatmap: Aggregate erc721_transfers by token ID and timestamp.

Integrating Dune Queries into Dashboards

Once queries are tuned, build dashboards:

  1. Define query parameters for dynamic filtering (e.g., date range).
  2. Visualize results using line charts, bar tables, heatmaps.
  3. Arrange panels into meaningful layouts.
  4. Share publicly or embed via iFrame.

Security Data Integrity

  • Validate event signatures before decoding logs to prevent misinterpretation.
  • Cross-reference on-chain source data with alternative indexing services.
  • Be mindful of EVM forks and re-orgs when analyzing recent blocks.

Troubleshooting Debugging

Common debugging steps:

  • Run subqueries in isolation to validate intermediate results.
  • Use LIMIT 10 to inspect sample rows.
  • Check Dune’s query logs for errors or timeouts.
  • Consult the community forum for known issues (community.dune.com).

Future of On-Chain Analytics with Dune

Dune continually evolves with new features:

  • Cross-chain multi-indexing
  • Real-time streaming and alerting
  • AI-driven query suggestions
  • Advanced permissioning and data privacy

Conclusion

Mastering on-chain SQL queries in Dune empowers you to unlock rich, actionable insights from blockchain data.
By following the principles, techniques, and best practices outlined above, you can build performant, reliable,
and visually compelling dashboards that drive smarter decisions in the crypto ecosystem.

For more details, visit the official Dune documentation at
docs.dune.com and Ethereum developer resources at
docs.ethereum.org.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *