Skip to main content
Aggregate Tables are an experimental feature, the API might be changed in any release.To use this feature, please add the following JVM argument: -Dactiveviam.feature.aggregate_tables.enabled=true.
DirectQuery is using the external database to feed some aggregate providers and answer some queries. This can result in some large queries with heavy load on the database, and if the same cube is started multiple times, the same query can be repeated. Aggregate tables are a way to cache some aggregated data in the external database to make some queries faster and cheaper.

How does it work

Aggregate Tables are tables added in the external database as a table fed from aggregated data. Whenever possible, DirectQuery will query the Aggregate Table instead of the base Schema for a more optimized query: For instance, consider a Sales table containing sales such as this one: It is possible to aggregate the sum of the Quantity per Date and Product in the external database into an other table agg_sales: If this table is added as an Aggregate Table in DirectQuery, it will be used whenever possible instead of the original Sales table. For instance when asking for the SUM(Quantity) per Product in the Sales table, DirectQuery will retrieve the SUM(sum_of_quantity) per product in agg_sales. As the agg_sales is smaller and already aggregated the result will be faster to compute and will potentially avoid a huge scan of the database, saving both time and compute costs.

Use cases

There are a few classic use cases for using an Aggregate Table:
  • Feeding an aggregate provider faster by pre-aggregating over the provider fields
  • Having an “external database aggregate provider”, i.e. being able to answer some queries from the pre-aggregated data in the external database but without having to store additional data in-memory.
  • Feeding some hierarchies faster

Supported aggregations

The following aggregation function can be used in an Aggregate Table:
  • Sum
  • Min
  • Max
  • Average
  • Sum product
  • Count
  • Long
  • Short
  • Gross
  • Square sum

Custom Aggregations

If you have a custom aggregation functions, you must implement an equivalent ISqlReAggregationFunction plugin value and register it in the Registry with the same key as your aggregation function.

Examples

Create a simple Aggregate Table to feed an aggregate provider

Let’s create a new table that will pre-aggregate the quantity (in the tutorial sales table) per Date and Product:
We feed it with the data from the other tables:
The pre-aggregated data looks like this: Let’s first discover the SQL table corresponding to the Aggregate Table:
We will map the PRODUCT field of the sales table to the product field of the Aggregate Table:
We will also map the sum of the sales quantity column into a column called quantity_sum:
We can now build the AggregateTable:
And add it to the schema:
Let’s now define a bitmap aggregate provider on the sum of quantity per Product, the feeding of the in-memory provider will be done directly from the Aggregate Table:
Note that it is possible to make sure that a provider will be fed by a given Aggregate Table by adding the provider coordinate to this Aggregate Table. This will simply trigger a check when starting the application to make sure that they are compatible.

More complex aggregations such as average

In the previous example, SUM was based on a single input and pre-aggregated into a single column, but there are some more complex cases. For instance:
  • count has no input and produces 1 pre-aggregated column
  • sumProduct can takes N input columns and produces 1 pre-aggregated column
  • average takes a single input column but requires 2 pre-aggregated column in order to be able to re-aggregate: one column for the sum and one for the count

Count

The count is measure automatically added to any query sent to the database. When adding an aggregate table, a check is therefore performed to ensure it contains a column with a count. (otherwise it would never be used).

Slicing hierarchies

Slicing hierarchies are always expressed in the queries sent to the database. When a field expressed in a query is not part of the Aggregate Table, the query will not use the Aggregate Table. Therefore it is recommended to include the slicing hierarchies in Aggregate Table definition.

Aggregate tables with vectors

Arrays can be aggregated in an aggregate table but they need to have a specific format depending on the type of original array being aggregated: See the DirectQuery vector documentation for more information about array types.

Generate SQL for Aggregate Table matching an Aggregate Provider

Aggregate Tables are a good tool to feed Aggregate Providers quickly because it can use the aggregated data instead of running a new aggregation. DirectQuery provides a bootstrapper to generate the SQL to create and feed an Aggregate Table matching an Aggregate Provider. Let’s imagine we have the following Aggregate Provider defined:
We can create a bootstrapper for it:
This bootstrapper can provide the SQL to create and feed the provider:
You will need to execute this SQL to create and feed the table. Creating and feeding the SQL table only need to be done once: remember that the purpose of using an Aggregate Table is to avoid re-running the same queries at every start up. A good way to do that is to run the queries manually. When the table exists, it is possible to use the Aggregate Table in the application, the Aggregate Provider will use it for feeding:

Is my query using the Aggregate Table ?

The simplest solution to check whether the query is using an aggregate table is to look at the query in the external database. If that is the case, then the aggregate table name should appear in the SQL query as the base table (the first table in the FROM clause). If it using an aggregate table, the aggregate table name should appears as the base table (the first table in FROM clause) in the SQL query. Additionally, for databases supporting tags such as Snowflake, the tag aggregate_table_name will be added to the query with the name of the aggregate table used. While building a project, to have more information about why a query is compatible or not with aggregate tables, it is possible to add additional logs by setting the logger com.activeviam.database.sql.internal.query.AggregateTableQueryOptimizer to FINE. The additional logs look like this: