Oracle链接服务器查询花费的时间太长,无法执行大量记录。查询中涉及许多联接,其中某些表具有超过10万条记录。 我尝试过放置索引,创建临时表并将它们包括在联接中而不是物理表中,但是没有一个起作用
SELECT 'BSC' as Site_Code,Item as Part_Number,BUYFROMBP as Supplier_Code,Company AS CPO_CODE, WHSE as Shop_floor_whse,IP as Installation_Plan,LineNumb as line_number,ForecastDATE,CONVERT(DATE,GETDATE())
AS load_date,RecShipDate,
DelDateOpen,DelDateClose,PureForecastDate,NonNullLine,Qty
FROM OPENQUERY(MINMAX_LS_ERPPROD,'SELECT i.company,
f.ITEM,
i.ITEMDESC,
i.PRODUCTCLASS,
i.BUYFROMBP,
m3.caldate RecShipDate,
m5.caldate DelDateOpen,
m6.caldate DelDateClose,
trunc(COALESCE(a.planstartdate,f.forecastdate)) as PureForecastDate,
(CASE I.PRODUCTCLASS WHEN ''MINMAX'' THEN trunc(COALESCE(a.planstartdate,f.forecastdate)+ CASE WHEN i.company in (629,637,638,634,636,639,642) THEN 3/24 ELSE 0 END )
WHEN ''CBO-RP'' THEN trunc(COALESCE(a.planstartdate,f.forecastdate)+ CASE WHEN i.company in (629,637,638,634,636,639,642) THEN 3/24 ELSE 0 END )
WHEN ''CBO-MM'' THEN trunc(COALESCE(a.planstartdate,f.forecastdate)+ CASE WHEN i.company in (629,637,638,634,636,639,642) THEN 3/24 ELSE 0 END )
WHEN ''CBO-DP'' THEN trunc(COALESCE(a.planstartdate,f.forecastdate)+ CASE WHEN i.company in (629,637,638,634,636,639,642) THEN 3/24 ELSE 0 END )
ELSE m6.caldate
END) AS ForecastDATE ,
a.LINENUMB,
COALESCE(a.linenumb, 0) as NonNullLine,
CASE I.PRODUCTCLASS WHEN ''CBO-IP'' THEN COALESCE(a.ASSYORDOP, '' '')
else '' ''
end IP,
f.WHSE,
sum(coalesce(a.prodengqty,f.forecastqty)) Qty
FROM C629.ITEMDATAGENERAL i INNER JOIN C629.ITEMFORECASTDETAIL f ON i.ITEM = f.ITEM
INNER JOIN C629.ITEMBUYFROMBP ibp ON i.item = ibp.item and i.buyfrombp = ibp.buyfrombp
INNER JOIN C629.ITEMWHSEDATA iw on i.item = iw.item and i.whse = iw.whse
LEFT JOIN C629.ASCOPENORDERDETAIL a ON (f.ORDERNUMB = a.WHSEORD) AND (f.ITEM = a.ITEM) and trunc(f.FORECASTDATE) = trunc(a.PLANSTARTDATE)
INNER join C629.mdaycal m on trunc(COALESCE(a.planstartdate,f.forecastdate)+ CASE WHEN i.company in (629,637,638,634,636,639,642) THEN 3/24 ELSE 0 END) = m.caldate
INNER join C629.mdaycal m4 on m.mfgdate=m4.caldate
INNER join C629.mdaycal m2 on m.mdaylong = m2.mdaylong + iw.safetytimewhse + iw.outbndleadtimeiw + iw.inbndleadtimeiw + (ibp.calcleadtime-ibp.bpsafetytime - case when ibp.planfullsupplytimedesc = ''yes'' then ibp.fullsupplytime else ibp.supplytime end - ibp.internalprocesstime)-1
INNER join C629.mdaycal m3 on m2.mdaylong = m3.mdaylong - iw.inbndleadtimeiw+1
inner join C629.mdaycal m5 on m4.mdaylong = m5.mdaylong + iw.safetytimewhse + iw.outbndleadtimeiw + iw.inbndleadtimeiw - 1
inner join C629.mdaycal m6 on m5.mdaylong = m6.mdaylong - iw.inbndleadtimeiw+1
WHERE (((i.PRODUCTCLASS)=''MINMAX'' OR (i.PRODUCTCLASS) Like ''CBO%'')) and (ibp.plancluster is null OR ibp.plancluster = '' '')
GROUP BY i.company, f.ITEM, I.PRODUCTCLASS,i.ITEMDESC, i.BUYFROMBP, m3.caldate,m5.caldate,m6.caldate,
trunc(COALESCE(a.planstartdate,f.forecastdate)),
(CASE I.PRODUCTCLASS WHEN ''MINMAX'' THEN trunc(COALESCE(a.planstartdate,f.forecastdate)+ CASE WHEN i.company in (629,637,638,634,636,639,642) THEN 3/24 ELSE 0 END )
WHEN ''CBO-RP'' THEN trunc(COALESCE(a.planstartdate,f.forecastdate)+ CASE WHEN i.company in (629,637,638,634,636,639,642) THEN 3/24 ELSE 0 END )
WHEN ''CBO-MM'' THEN trunc(COALESCE(a.planstartdate,f.forecastdate)+ CASE WHEN i.company in (629,637,638,634,636,639,642) THEN 3/24 ELSE 0 END )
WHEN ''CBO-DP'' THEN trunc(COALESCE(a.planstartdate,f.forecastdate)+ CASE WHEN i.company in (629,637,638,634,636,639,642) THEN 3/24 ELSE 0 END )
ELSE m6.caldate
END) ,
a.LINENUMB,coalesce(a.linenumb, 0), (CASE I.PRODUCTCLASS WHEN ''CBO-IP'' THEN COALESCE(a.ASSYORDOP, '' '')
else '' ''
end), f.WHSE
order by i.company, f.item, m3.caldate asc')
查询运行了很长时间,然后由于对象错误而失败。实际上,当我删除以下联接时,查询运行正常-
INNER join C629.mdaycal m4 on m.mfgdate=m4.caldate
INNER join C629.mdaycal m2 on m.mdaylong = m2.mdaylong + iw.safetytimewhse + iw.outbndleadtimeiw + iw.inbndleadtimeiw + (ibp.calcleadtime-ibp.bpsafetytime - case when ibp.planfullsupplytimedesc = ''yes'' then ibp.fullsupplytime else ibp.supplytime end - ibp.internalprocesstime)-1
INNER join C629.mdaycal m3 on m2.mdaylong = m3.mdaylong - iw.inbndleadtimeiw+1
inner join C629.mdaycal m5 on m4.mdaylong = m5.mdaylong + iw.safetytimewhse + iw.outbndleadtimeiw + iw.inbndleadtimeiw - 1
inner join C629.mdaycal m6 on m5.mdaylong = m6.mdaylong - iw.inbndleadtimeiw+1
这意味着我必须以某种方式微调此逻辑,或者可能是其他原因,但是在删除这些联接后,查询将在2分钟内完成,否则需要40-50分钟或更长时间