Key concepts
- Aggregate awareness - Omni understands the level of aggregation required by a query and dynamically selects the appropriate table (granular or aggregated) in the model to retrieve the data.
- Bring your own aggregates - You, the user, create the aggregated tables using your preferred transformation tool. This provides flexibility and control over the aggregation process. Omni then leverages these tables for optimized query performance.
Setup
Once you’ve identified the query you’d like to optimize, follow these steps to implement aggregate awareness:Build an aggregate or rollup table
Build an aggregate or rollup table within Omni or some other transformation layer such as dbt.If you do this outside of Omni and don’t see your table, refresh the schema to have Omni build it. You can also navigate to Model > Refresh schema in the model IDE to trigger a refresh for specific tables.To demonstrate, let’s say you have a table with the following definition and you want to optimize the performance of the daily aggregations:In your transformation layer, you have defined the following materialized table, which has pre-aggregated the metrics daily:
order_items table with aggregation you want to optimize
Pre-aggregated table definition
Add a materialized_query parameter
From the model IDE, add a
materialized_query parameter to the optimized view to enable Omni to match the aggregate table to the underlying views:Implementation of aggregate awareness
The
fields parameter your materialized query definition should be in the same order as the dimensions in the optimized table.Verify the setup
Now, at query runtime, Omni will dynamically swap in the aggregated table for the underlying table if it contains all the necessary fields to execute the query. Because the aggregated table is smaller than the underlying table, queries running against it will be faster and more efficient.You can verify that aggregate awareness is working by inspecting the generated SQL using Omni’s SQL inspector. The SQL inspector will show whether Omni is querying the aggregate table or the granular table.
Example of fallback to pre-aggregated view
Troubleshooting
If you expect Omni to use the aggregated table but it’s not, consider the following:-
Missing fields: Ensure that all the fields being queried are present in the aggregated table and correctly mapped in the
materialized_queryparameter. -
Incompatible queries: Certain queries, such as
count_distinctover a different level of aggregation than the aggregate table, may not be compatible with aggregate awareness. -
Cache: If the query results are being retrieved from the cache, Omni may not need to rewrite the SQL.
- Try clearing the cache to test if aggregate awareness is working as expected.
- You can also test by setting the
cache_policyto0so that it never uses cache.
If a query cannot be answered entirely by the
materialized_query, aggregate awareness is not likely to be used on that query.