I've this table in the main database
create table sales (
`updated_date_key` INT(10) UNSIGNED NOT NULL,
`store_id` BIGINT UNSIGNED NOT NULL,
`product_id` BIGINT UNSIGNED NOT NULL,
`category` INT(10) UNSIGNED NOT NULL,
`price` FLOAT NOT NULL,
`sold` BOOL NOT NULL,
PRIMARY KEY (`store_id`, `product_id`),
)
When there is any change in the records, a set of events are created which is consumed in a different database (analytics database). Below we have a set of events created on two different dates. The event created on 20221012 (YYYYMMDD) can be considered as the initial state of the table. This event is consumed in a different database (analytics database).
event on : 20221012 (Three products were created)
{ updated_date_key : 20221012, store_id : 1, product_id: 123, category: 0, price: 500.12, sold: 0}, { updated_date_key : 20221012, store_id : 1, product_id: 456, category: 0, price: 100, sold: 0}, { updated_date_key : 20221012, store_id : 2, product_id: 678, category: 2, price: 200, sold: 0}
event on : 20221020 (Two products are sold)
{ updated_date_key : 20221020, store_id : 1, product_id: 123, category: 0, price: 500.12, sold: 1}, { updated_date_key : 20221020, store_id : 1, product_id: 456, category: 0, price: 100, sold: 1}
Expected outcome:
expected outcome is a snapshot that gives the count of sold and unsold products for a given store and category on a particular day.
date_key, store_id, category unsold_products, sold_products
20221012 1 0 2 0
20221012 2 2 1 0
20221013 1 0 2 0
20221013 2 2 1 0
20221014 1 0 2 0
20221014 2 2 1 0
...............
20221020 1 0 0 2
20221020 2 2 1 0
Question:
One of the solution is to store all the revisions at the analytics database and then create a snapshot based on that. But storing all the revisions can create a huge table and we have resource constraints. Is it possible to create snapshot without storing the revisions in the analytics database purely based on the events that we receive? Is it possible to store only the pre-aggregated results of the events like count of sold, unsold item based on event and then create snapshot based on this pre-aggregated revision table? Is there more efficient way of creating snapshot based on revision table?