Skip to main content

Use case

We have a dataset with orders and we want to aggregate data while having decent performance. Orders have a creation time, so we can use partitioning by time to optimize pre-aggregations build and refresh time. The problem is that the order’s status can change after a long period. In this case, we want to rebuild only partitions associated with this order. In the recipe below, we’ll learn how to use the refresh_key together with the FITER_PARAMS for partition separately.

Data modeling

Let’s explore the orders cube data that contains various information about orders, including number and status: In our case, each order has created_at and updated_at properties. The updated_at property is the last order update timestamp. To create a pre-aggregation with partitions, we need to specify the partition_granularity property. Partitions will be split monthly by the created_at dimension.
As you can see, we defined custom a refresh_key that will check for new values of the updated_at property. The refresh key is evaluated for each partition separately. For example, if we update orders from August and update their updated_at property, the current refresh key will update for all partitions. There is how it looks in the Cube logs:
Note that the query for two partitions is the same. It’s the reason why all partitions will be updated. How do we fix this and update only the partition for August? We can use the FILTER_PARAMS for that! Let’s update our pre-aggregation definition:
Cube will filter data by the created_at property and then apply the refresh key for the updated_at property. Here’s how it looks in the Cube logs:
Note that Cube checks the refresh key value using a date range over the created_at property. With this refresh key, only one partition will be updated.

Result

We have received orders from two partitions of a pre-aggregation and only one of them has been updated when an order changed its status:

Source code

Please feel free to check out the full source code or run it with the docker-compose up command. You’ll see the result, including queried data, in the console.