Availability: Pre-aggregates are an Early Access feature available on Enterprise plans only.
Getting started
Define pre-aggregates in your dbt project and configure scheduling.
Monitoring and debugging
Track materialization status, debug query matching, and view hit/miss stats.
How it works
Pre-aggregates follow a four-step cycle:- Define — You add a
pre_aggregatesblock to your dbt model YAML, specifying which dimensions and metrics to include. - Materialize — Lightdash runs the aggregation query against your warehouse and stores the results. This happens automatically on compile, on a cron schedule you define, or when you trigger it manually.
- Match — When a user runs a query, Lightdash checks if every requested dimension, metric, and filter is covered by a pre-aggregate.
- Serve — If a match is found, the query is served from the materialized data instead of hitting your warehouse.
Example
Suppose you have anorders table with thousands of rows, and you define a pre-aggregate with dimensions status and metrics total_amount (sum) and order_count (count), with a day granularity on order_date.
Your warehouse data:
| order_date | status | customer | amount |
|---|---|---|---|
| 2024-01-15 | shipped | Alice | $100 |
| 2024-01-15 | shipped | Bob | $50 |
| 2024-01-15 | pending | Charlie | $75 |
| 2024-01-16 | shipped | Alice | $200 |
| 2024-01-16 | pending | Charlie | $30 |
| … | … | … | … |
| order_date_day | status | total_amount | order_count |
|---|---|---|---|
| 2024-01-15 | shipped | $150 | 2 |
| 2024-01-15 | pending | $75 | 1 |
| 2024-01-16 | shipped | $200 | 1 |
| 2024-01-16 | pending | $30 | 1 |
| order_date_month | status | total_amount |
|---|---|---|
| January 2024 | shipped | $350 |
| January 2024 | pending | $105 |
sum can be re-aggregated — summing daily sums gives the correct monthly sum.
Query matching
When a user runs a query, Lightdash automatically checks if a pre-aggregate can serve the results. A pre-aggregate matches when all of the following are true:- Every dimension in the query is included in the pre-aggregate
- Every metric in the query is included in the pre-aggregate
- Every dimension used in filters is included in the pre-aggregate
- All metrics use supported metric types
- The query does not contain custom dimensions, custom metrics, or table calculations
- If the query uses a time dimension, the requested granularity is equal to or coarser than the pre-aggregate’s granularity (for example, a
daypre-aggregate can serveday,week,month, oryearqueries, but nothour)
Dimensions from joined tables
Pre-aggregates support dimensions from joined tables. Reference them by their full name (for example,customers.first_name) in the dimensions list.
Supported metric types
Pre-aggregates support metrics that can be re-aggregated from pre-computed results:sumcountminmaxaverage
Current limitations
Not all metrics work this way. Considercount_distinct with the same daily pre-aggregate from above. If a daily pre-aggregate stores “2 distinct customers on 2024-01-15” and “1 distinct customer on 2024-01-16”, you can’t sum those to get the monthly distinct count — Alice ordered on both days and would be counted twice:
| order_date_day | status | distinct_customers |
|---|---|---|
| 2024-01-15 | shipped | 2 (Alice, Bob) |
| 2024-01-16 | shipped | 1 (Alice) |
count_distinct through approximation algorithms. Follow this issue for updates.
For similar reasons, the following metric types are also not supported:
sum_distinct,average_distinctmedian,percentilepercent_of_total,percent_of_previousrunning_total- Custom SQL metrics — Follow this issue
number,string,date,timestamp,boolean