Scenario: We stage Change Data Capture (CDC) data in an Operational Data Store (ODS) layer table. This table includes metadata columns such as src_updated_ts, id_version, extraction_ts, and operation (with values representing insert, update, or delete operations). The source table has an ID column as its primary key.
Currently, when constructing our data warehouse, our job invokes a view for each ODS table to calculate the latest snapshot. This snapshot essentially aims to reconstruct the source table from the CDC rows. Our approach involves using the ROW_NUMBER() function with the following logic: partition by ID and order by src_updated_ts (in descending order), id_version (in descending order), and extraction_ts (in descending order). We then select the latest record for each ID.
Until now, we’ve been loading the warehouse once a day. However, we’re now planning to run the warehouse job every hour. Unfortunately, our current view-based method for calculating the latest snapshot is becoming prohibitively expensive and time-consuming. It requires scanning the entire ODS table for every view invocation, which is not feasible for frequent updates.
what am seeking help for: I want to materialize and calculate the data table’s current snapshot as i get records inserted into ODS table. I have tried to utilize materialized view feature but couldn’t use it as my query involves partition by or self join or sub-query.
What is the best way to achieve this in big query ?