Table :
order_date. order_id. customer_id. product id
2020-01-01 O1. C1. P1
2020-01-01 O1. C1. P2
2020-01-01 O1. C1. P3
2020-01-10 O2. C1. P2
2020-01-10 O2. C1. P3
2020-01-10 O2. C1. P4
2020-01-10 O2. C1. P5
I used the code as below, but for huge volume of data (say 10M rows), it times out. Any better way to improve the performance of this query?
select a.order_date, a.customer_id, a.order_id, a.product_id, case when b.product_id = a.product_id then 'Y' else 'N' end as prev_purchase_sts
from table a
left join table b on a.customer_id = b.customer_id and a.order_date > b.order_date