Sub topic 2
Getting around Dune: the query editor, engine and credits
This chapter covers the parts of Dune you use before you write any analysis: the workspace, the engine that runs your SQL, and the credit system that limits how much you can run. It assumes you can write basic SQL (SELECT, WHERE, GROUP BY, JOIN) and know what blocks, transactions and smart contracts are. The previous chapter covered how on-chain data is structured. This one covers the tool you query it with.
What Dune is
Dune is a web platform that indexes blockchain data from many chains and exposes it as SQL tables. You write a query in the browser, run it on Dune's servers, and get a result table. You can chart that table and place the chart on a dashboard.
Three things matter for how you work:
- You never download the chain. Dune stores and indexes the data. You send SQL and receive results.
- Queries are shared artifacts. Each saved query has a numeric ID and a URL. Other users can open, run and fork it. Forking copies a query into your account so you can change it.
- Compute is metered. Every execution uses resources, and your plan gives you a limited amount of them. That limit is the credit system, described below.
The workspace
When you create a new query, you see several areas.
The data explorer
A sidebar lists the schemas and tables you can query. You can search for a table such as dex.trades and expand it to see its columns and types. Use it to check column names, and to see which tables are raw, decoded or curated. A later chapter covers those layers in detail. Checking a table's columns here is faster than guessing and getting an error.
The query editor
The editor is a SQL text area with syntax highlighting and autocomplete. It accepts DuneSQL, which is a SQL dialect based on Trino. If you know Trino or Presto syntax, most of it carries over. Features such as INTERVAL '30' DAY (with the number in quotes) and date_trunc work as in Trino.
Useful habits in the editor:
- Run the query with the Run button or the keyboard shortcut shown next to it (usually Ctrl or Cmd plus Enter).
- If you select part of the text, Dune can run only the selection in many versions of the editor. Check the button label before you rely on this.
- Write queries so they are readable. Dune users fork each other's work constantly, so clear aliases and short comments pay off.
The results panel
After a run, the bottom of the screen shows a table of results, plus information about the run: how long it took and how many rows came back. The results panel also has tabs for visualizations. Each chart you create is attached to a query and reads from its result set. Chapter 7 covers charts and dashboards.
Saving, naming and visibility
A new query is a draft until you save it. Saving gives it an ID and a URL, which you need if you want to put it on a dashboard or call it through the API. Give queries descriptive names, because a dashboard built from queries called "Untitled 14" is hard to maintain. Free accounts generally have public queries, and private queries are tied to paid plans. Check the current plan page before you put anything sensitive in a query.
A first query, run carefully
Start with a small query so you can see the whole cycle. This one sums daily DEX trading volume on Ethereum for the last 30 days.
SELECT
block_date,
SUM(amount_usd) AS volume_usd
FROM dex.trades
WHERE blockchain = 'ethereum'
AND block_date >= current_date - INTERVAL '30' DAY
GROUP BY 1
ORDER BY 1Two lines in this query keep it cheap. The filter on blockchain removes every other chain. The filter on block_date limits the scan to 30 days of data. block_date is a partition column, which means the table is stored in chunks by date, and the engine can skip the chunks outside your range. Without the date filter, the same query reads the whole history of the table.
Before you commit to a heavy query, preview the table with a bounded one:
SELECT *
FROM dex.trades
WHERE blockchain = 'ethereum'
AND block_date >= current_date - INTERVAL '1' DAY
LIMIT 10This shows you the real column names and a few real rows. The date filter is what limits the work. A LIMIT alone does not reliably reduce the amount of data the engine reads.
The engine
DuneSQL is the query engine behind the editor. It is built on Trino, a distributed SQL engine that splits a query into stages and runs them in parallel on a cluster. Dune stores the data in a columnar format, so a query reads only the columns it names. That is why selecting a few columns costs less than SELECT * on a wide table.
Points to know about how the engine behaves:
- Types are strict. Trino does not silently convert between many types. Values such as token amounts are often stored as large integers or as
uint256. Casting them todoubleordecimalbefore dividing by a token's decimals is something you do deliberately. The next chapter covers this. - Addresses are binary values, not strings. In DuneSQL you write an address as an unquoted hex literal such as
0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48. Putting it in quotes turns it into text, and the comparison then fails or returns no rows. - Timestamps are in UTC. Block times use UTC, so
date_trunc('day', ...)groups by UTC days. - Queries have limits. Long-running queries can hit a time limit, and very large result sets can hit a size limit. If a query times out, narrow the date range or the columns, or aggregate earlier in the query.
Engine sizes
Dune offers different engine tiers. The default tier suits most queries. A larger tier gives a query more compute and can finish heavy queries that time out on the default, but it costs more credits per run. The sensible order is to first make the query efficient, then move to a larger engine only if it still does not finish. Paying for a bigger engine to run a query that scans years of unfiltered data is wasteful when a date filter would solve it.
Credits
Dune measures usage in credits. Each plan includes a monthly amount, and executions consume credits based on the compute they use. Two consequences follow.
- A heavy query costs more than a light one. A query that scans a large table without a date filter uses far more credits than one that filters by
block_date. - Repeated runs add up. Running the same expensive query many times while you tweak a chart drains credits. A dashboard that refreshes often multiplies this.
The exact allowances, prices and limits change over time, so read the current pricing page instead of relying on a number from a tutorial. The habits that save credits do not change:
- Filter on the partition column in every query that touches a large table.
- Select only the columns you need.
- Test on a short date range, then widen it.
- Prefer curated or decoded tables over raw tables when they hold what you need, because they are smaller and already organized.
- Do not schedule refreshes more often than the data needs. A weekly metric does not need hourly refreshes.
Reading what a run tells you
After each run, look at three things before you trust the result:
| What to check | Why it matters |
|---|---|
| Row count | A result of zero rows usually means a wrong filter, such as a quoted address or a misspelled chain name. |
| Execution time | A run that takes much longer than similar queries suggests a missing date filter. |
| Column types | Large numeric columns may need a cast before you do arithmetic. |
When a query fails, read the error message. Trino errors are usually specific. A message naming a missing column means a spelling or schema error, and a message about types means you need a cast. A timeout or memory error means the query does too much work and you should reduce it.
Common mistakes in the first week
- Forgetting the date filter. This is the main cause of slow queries and wasted credits.
- Quoting hex addresses.
'0xA0b8...'is text.0xA0b8...is a binary value. - Using
SELECT *on large tables. Name the columns you need. - Forking and trusting without reading. A forked query can contain a wrong decimal assumption or an outdated filter. Read it before you reuse its numbers.
- Scaling the engine first. Optimize the query before you pay for more compute.
Exercise
A colleague's query on dex.trades times out on the default engine. It has no WHERE clause on dates and selects every column. Name two changes you would make before switching to a larger engine, and say why each helps.
Show answer
First, add a filter on the partition column, such as block_date >= current_date - INTERVAL '30' DAY. The engine can then skip every date partition outside the range, so it reads far less data. Second, replace SELECT * with only the columns the analysis needs. The data is stored in columns, so the engine reads only the named ones. You could also filter on blockchain if the question concerns a single chain. A larger engine is the last step, because it costs more credits per run and does not fix a query that scans more than it needs.
Exercise
You run a query that compares contract_address to '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' and get zero rows, although you know the token has transfers. What is the likely cause, and how do you fix it?
Show answer
The address is written as a quoted string, so DuneSQL treats it as text instead of a binary address value. Remove the quotes and write it as a hex literal: 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48.