Research note No. 18 · Research Notes
Query Hygiene and Materialized Views for Research Desks
Research is only as reliable as the queries behind it. The desk's standards for naming, versioning, testing and pre-aggregating on-chain queries so results stay reproducible.
Key takeaways
- Every published figure should trace back to a saved, versioned query with explicit time bounds.
- Most silent errors come from units, decimals, duplicate events and reorg-affected recent data — test for them.
- Pre-aggregate expensive, frequently used series into materialized views, and document their definitions.
- Treat definitions as code: a metric's meaning should not change without a recorded revision.
On-chain research has an unusual advantage: anyone can check the data. It also has an unusual liability: a single misplaced decimal, a double-counted event or an undocumented change in definition can produce a confident, wrong conclusion that looks exactly like a right one. The desk's answer is a set of unglamorous habits we call query hygiene. This note describes them, along with the role materialized views play in keeping heavy analysis fast and consistent.
Principle: every number has a query
Any figure that appears in a note must trace back to a saved query, with its parameters fixed. That rule sounds bureaucratic; in practice it is what makes corrections possible. When a reader questions a number, the desk can rerun the exact query, check the logic and either confirm or correct it — rather than reconstructing an analysis from memory.
The desk keeps queries in the Wevr query engine with names that encode their purpose and version, for example exchange_netflow_btc_daily_v3, and records which version produced each published chart.
Explicit bounds, always
Queries that use now() produce different answers every time they run. For exploratory work, that is fine. For published figures, replace relative windows with explicit start and end timestamps, so the result is fixed.
Explicit bounds also avoid a quieter error: including the most recent blocks, which may still be reorganised or incompletely indexed. The desk excludes the most recent period from published series unless the analysis is specifically about real-time conditions.
The five silent errors
Most data mistakes the desk has caught — in our work and others' — fall into five categories:
- Units and decimals. Tokens store amounts in their smallest unit with different decimal places; transaction values are in wei; fees combine gas and price. Every amount column in a query should be converted explicitly, with the conversion visible.
- Duplicate events. Joins on non-unique keys multiply rows; some contracts emit several events for one action. Counting rows rather than distinct identifiers silently inflates totals.
- Address formatting. Case and prefix mismatches cause joins to drop rows without error. Normalise addresses before comparing.
- Filter leakage. A filter applied in one subquery but not another produces totals that cannot be compared. Shared filters belong in one common table expression.
- Definition drift. "Active address", "exchange flow" and "whale" each have several reasonable definitions. Mixing them across charts produces contradictions that look like findings.
The desk tests new queries against known values before publishing — a well-documented transaction checked in the live tables, a known daily total, a figure reported by the protocol itself — and keeps those tests alongside the query.
Materialized views
Some series are expensive to compute and used constantly: daily exchange flows by venue, daily active addresses by chain, holder balances by cohort. Recomputing them from raw events in every analysis wastes time and, worse, invites slightly different versions of the same series.
ClickHouse's materialized views solve both problems. A materialized view runs a query incrementally as new data arrives and stores the result in its own table. Instead of scanning billions of raw events, downstream queries read a compact pre-aggregated table — and every analysis that uses it inherits the same definition.
The desk's rules for materialized views are simple:
- One definition, documented. Each view has a written description of what it counts, which filters it applies and which labels it depends on.
- Aggregations that merge correctly. Views should store intermediate states (sums, counts, uniqueness sketches) that can be combined correctly across time ranges, rather than pre-computed averages that cannot.
- Backfills are revisions. When a view's logic changes, the historical data is recomputed and the change is recorded, so published figures can be traced to the version that produced them.
Labels are data too
Entity labels — exchanges, funds, bridges — change over time as attribution improves. A query that joins against today's labels to compute last year's exchange flows uses information that was not available last year. For most purposes that is acceptable, even desirable; for backtests it is look-ahead bias. The desk records which label snapshot each analysis used.
Review
Before publication, each note's queries are reviewed by a second analyst against a short checklist: units converted, bounds explicit, joins on unique keys, filters shared, definitions consistent with prior notes, test values matched. The review rarely changes conclusions, but when it does, it changes them before readers see them.
An example of the habit paying off
In one internal review, a chart showing a sharp rise in stablecoin transfers turned out to double-count transfers routed through an aggregator, which emitted an event for each hop. The fix — counting distinct transaction hashes rather than event rows — removed most of the "rise". The underlying trend was real but modest. Without the habit of checking event uniqueness, the note would have overstated it by a wide margin.
A minimal hygiene checklist
For teams starting out, five checks catch most problems before publication:
- Are all amounts converted with the correct decimals, and is the conversion visible in the query?
- Are time bounds explicit, and is the most recent, possibly incomplete period excluded?
- Is every join on a key that is unique on at least one side?
- Are counts of events, transactions and distinct entities the right kind of count for the question?
- Does the result match at least one independently known value?
Conclusion
Reproducibility is not a property of on-chain data; it is a property of how the data is queried. Save every query, fix its bounds, test it against known values, pre-aggregate the series everyone uses, and treat definitions as versioned code. The habits are tedious. The alternative is publishing errors that anyone with a query editor can find.