SQL - 使用子查询提取最近日期的记录时查询超时

时间:2018-06-06 22:12:27

标签: sql oracle

我正在尝试在一个非常大的数据集中提取最近日期(COMPUTE_DAY)的值 - 这似乎是一个经常被问到的问题,最常见的解决方案是在同一个表上使用子查询。不幸的是,每次尝试时我的查询都会超时。该表分为两列,REGION和COMPUTE_DAY,主键为REGION,COMPUTE_DAY和PLAN_UUID。有什么办法可以让这个查询更有效率吗?

SELECT /*+ use_hash(ipp,ipp2) */ 
ipp.COMPUTE_DAY,
ipp.ITEM,
ipp.MANUFACTURER,
ipp.ORDER_DATE,
ipp.CARTON,
sum(ipp.TARGET_INVENTORY) as 1,
sum(ipp.CURRENT_INVENTORY) as 2,
sum(ipp.DEMAND) as 3,
sum(ipp.ORDERS) as 4,
sum(ipp.SHIPMENTS) as 5,
sum(ipp.QUANTITY) as 6,

FROM 
table ipp

WHERE
ipp.REGION = 1
AND ipp.COMPUTE_DAY = (select max(ipp2.COMPUTE_DAY) from O_IP_PLANS ipp2 where ipp2.REGION_ID = 1 AND ipp2.COMPUTE_DAY BETWEEN TO_DATE('{RUN_DATE_YYYY/MM/DD}','YYYY/MM/DD')-7 AND TO_DATE('{RUN_DATE_YYYY/MM/DD}','YYYY/MM/DD') AND ipp2.PLAN_UUID = ipp.PLAN_UUID) 
AND ipp.GROUP_ID = 121
AND ipp.IOG = 1
AND ipp.INTENT = 'YES'

GROUP BY ipp.COMPUTE_DAY,
ipp.ITEM,
ipp.MANUFACTURER,
ipp.ORDER_DATE,
ipp.CARTON;

0 个答案:

没有答案