Well I have a series of sps that are running a data warehousing solution that we have developed in house. While on the most part it runs pretty good, there is one stored procedure that runs really slow. It takes about 30 minutes on average to execute. I know exactly where the bottle neck is, I just don't know how to fix it.
Basically what the stored procedure does is create a series of variable temp tables and insert into those tables. no problem there. it then joins the temp tables and inserts into another temp table, twice (the second one is a little different).
this is the statement that joins the tables and inserts into the temp table, and is the part that takes forever. any help would be greatly appreciated.
; with Requested as
(
select distinct
PONUMBER as PONumber,
min(REQSTDBY) as RequestedBy
from dw.POP10110
where REQSTDBY <>''
group by PONUMBER
)
insert into @tblTableA
(
PONumber,
ReceiptNumber,
ReceiptLineNumber,
VendorID,
POType,
QuantityShipped,
QuantityInvoiced,
ItemNumber,
ItemDescription,
UofM,
UnitCost,
ExtendedCost,
SiteID,
ProjectNumber,
AccountID,
RequestedBy,
GLPostDate,
VendorName,
CostCategoryID
)
select
a.PONUMBER,
a.POPRCTNM,
a.RCPTLNNM,
a.VENDORID,
a.POPTYPE,
a.QTYSHPPD,
a.QTYINVCD,
b.ITEMNMBR,
b.ITEMDESC,
b.UOFM,
b.UNITCOST,
b.EXTDCOST,
b.LOCNCODE,
b.ProjNum,
case i.CostCategoryID
when 'MISC' then isnull(i.AccountID,'N/A')
else
case j.CostCategoryID
when 'MISC' then
isnull(j.AccountID,'N/A')
else
isnull(c.PurchaseAccount,'N/A')
end
end as AccountID,
d.RequestedBy,
coalesce(e.GLPOSTDT, f.GLPOSTDT, '') as GLPostDate,
coalesce(e.VENDNAME, f.VENDNAME, '') as VENDNAME,
case i.CostCategoryID when 'MISC' then i.CostCategoryID else
case j.CostCategoryID when 'MISC' then j.CostCategoryID else coalesce(g.CostCategoryID, h.CostCategoryID, '') end
end as CostCategoryID
from dw.POP10500 a
inner join dw.POP30310 b
on a.PONUMBER=b.PONUMBER
and a.POPRCTNM=b.POPRCTNM
and a.RCPTLNNM=b.RCPTLNNM
left outer join @gl00100 c
on b.INVINDX=c.ActID
left outer join Requested d
on b.PONUMBER = d.PONumber
left outer join dw.POP30300 e
on b.POPRCTNM=e.POPRCTNM
left outer join dw.POP10300 f
on b.POPRCTNM=f.POPRCTNM
left outer join @pop31310 g
on b.POPRCTNM=g.ReceiptNumber
left outer join @pop11310 h
on b.POPRCTNM=h.ReceiptNumber
left outer join @pop30390 i
on a.POPRCTNM=i.ReceiptNumber
left outer join @pop10390 j
on a.POPRCTNM=j.ReceiptNumber
As far as i am aware the appropriate indexes are in place.