产品
product_id product_serial_number product_status
1 X123 PENDING
1 X123 PROCESSED
2 X345 PENDING
3 X678 PENDING
4 Y890 PENDING
4 Y890 PROCESSED
上表显示了产品的状态及其历史。我需要生成一个报告,其输出如下所示:
product_id status
1 UPDATE
2 NEW
3 NEW
4 UPDATE
即,如果一个产品之前已经被处理过(例如产品1 和4),它的状态是UPDATE,否则它的状态是NEW。
我提出了这个查询,但我对它的表现不满意:
select product_id, 'UPDATE'
from products p1
where product_id in (select product_id from products p2 where p2.product_status='PROCESSED' and p2.product_status='ARCHIVED')
Union
select product_id, 'NEW'
from products p1
where product_id not in (select product_id from products p2 where p2.product_status='PROCESSED' and p2.product_status='ARCHIVED')
另一种可行的方法是将表连接到自身:
select p1.product_id, decode(p2.product_id, null, 'NEW','UPDATE')
from products p1, products p2
where p1.product_id=p2.product_id(+)
and p1.product_serial_number=p2.serial_number(+)
and p2.product_status(+) = 'PROCESSED'
当任一查询针对大型数据集运行时,性能都不是很好。我如何改进(甚至完全改变)上述查询以获得最佳性能?