【发布时间】:2021-10-25 22:00:22
【问题描述】:
表:
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
我使用了如下代码,但是对于大量数据(比如 10M 行),它会超时。有没有更好的方法来提高此查询的性能?
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
【问题讨论】:
标签: sql amazon-redshift window-functions exists self-join