I have this question:
For 2 tables:
1. events:
event_id int (autoincrement) --10B distinct values
event_ts datetime -- 10B
event_type int (1 = impression, 2 = click, 3 = purchase...) --20
product_id int --100K
client_id int --10M
client_type int --10
event_platform --100
2. products:
product_id int
product_name varchar(20)
product_type int -- 10000
I need to run this query:
select product_type, client_type, datetrunc('month',event_ts), count(distinct client_id), count(*)
from events join products using(product_id)
where event_ts > ...
and product_type in (100 values)
and event_platform in (3 values)
and event_type in (2,3)
group by product_type, client_type, datetrunc('month',event_ts)
having count(*) > 10000
What will be the optimal design for the 2 tables? (order by / partition / shading)
How would you rewrite this query?