Hi all!
I am not sure how you all will respond to this, so let me throw it out there.
/*
* Disclaimer!!!
* This is not a real query being executed, but a simplified version for general vision.
* Executing it with any other tool may produce a different result.
*/
select distinct
[SubQuery5].[OrderHed_OrderNum] as [OrderHed_OrderNum],
[SubQuery5].[OrderHed_PONum] as [OrderHed_PONum],
(iif(right(SubQuery6.UD03_sq6_Key3,3)='---', left(right(SubQuery6.UD03_sq6_Key3,6),3) + '-' + SubQuery6.UD03_sq6_ShortChar01, left(right(SubQuery6.UD03_sq6_Key3,7),3) + right(SubQuery6.UD03_sq6_Key3,3) + '-' + SubQuery6.UD03_sq6_ShortChar01)) as [Calculated_MyXRev],
[SubQuery5].[Calculated_OurXRev] as [Calculated_OurXRev],
[SubQuery5].[OrderDtl_PartNum] as [OrderDtl_PartNum],
[SubQuery5].[OrderDtl_OrderNum] as [OrderDtl_OrderNum],
[SubQuery5].[OrderDtl_OrderLine] as [OrderDtl_OrderLine],
[SubQuery5].[Calculated_FirstRel] as [Calculated_FirstRel],
[SubQuery5].[OrderDtl_UnitPrice] as [OrderDtl_UnitPrice],
[SubQuery5].[OrderRel_ReqDate] as [OrderRel_ReqDate],
[SubQuery6].[Calculated_MinDate] as [Calculated_MinDate],
[SubQuery5].[Calculated_Shipped] as [Calculated_Shipped],
[SubQuery5].[Calculated_OurRemainQty] as [Calculated_OurRemainQty],
(SubQuery6.UD03_sq6_Number01 -SubQuery6.UD03_sq6_Number03) as [Calculated_CustFinalQty],
(convert(varchar,SubQuery5.OrderHed_OrderNum) + '-' + convert(varchar,SubQuery5.OrderDtl_OrderLine) + '-' +convert(varchar, SubQuery5.Calculated_FirstRel)) as [Calculated_OrderLnRel],
(0) as [Calculated_CloseRelease],
(0) as [Calculated_UseCustDate],
(iif(convert(varchar, SubQuery6.Calculated_MinDate , 101) is null, iif((SubQuery5.Calculated_Shipped >= 1 or (SubQuery10.Calculated_FirstJob) != '' ), 'Close Me', 'Delete Me'), 'Use Date')) as [Calculated_Suggestion],
[SubQuery10].[Calculated_FirstJob] as [Calculated_FirstJob]
from (select
[SubQuery4].[OrderHed_Company] as [OrderHed_Company],
[SubQuery4].[OrderHed_OrderNum] as [OrderHed_OrderNum],
[SubQuery4].[OrderHed_PONum] as [OrderHed_PONum],
[SubQuery4].[Calculated_OurXRev] as [Calculated_OurXRev],
[SubQuery4].[OrderDtl_PartNum] as [OrderDtl_PartNum],
[SubQuery4].[OrderDtl_OrderNum] as [OrderDtl_OrderNum],
[SubQuery4].[OrderDtl_OrderLine] as [OrderDtl_OrderLine],
[SubQuery4].[OrderDtl_UnitPrice] as [OrderDtl_UnitPrice],
[SubQuery4].[OrderDtl_RevisionNum] as [OrderDtl_RevisionNum],
[SubQuery4].[OrderRel_ReqDate] as [OrderRel_ReqDate],
[SubQuery4].[Calculated_OurRemainQty] as [Calculated_OurRemainQty],
[SubQuery4].[OrderRel_SellingReqQty] as [OrderRel_SellingReqQty],
[SubQuery4].[OrderRel_OurStockShippedQty] as [OrderRel_OurStockShippedQty],
(iif(right(UD03.Key3,3)='---', left(right(UD03.Key3,6),3) + '-' + UD03.ShortChar01, left(right(UD03.Key3,7),3) + right(UD03.Key3,3) + '-' + UD03.ShortChar01)) as [Calculated_MyXRev],
(UD03.Number01-UD03.Number03) as [Calculated_CustFinalQty],
[SubQuery4].[Calculated_Shipped] as [Calculated_Shipped],
(min( SubQuery4.OrderRel_OrderRelNum)) as [Calculated_FirstRel]
from (select
[OrderHed].[Company] as [OrderHed_Company],
[OrderHed].[PONum] as [OrderHed_PONum],
[OrderDtl].[PartNum] as [OrderDtl_PartNum],
[OrderDtl].[OrderNum] as [OrderDtl_OrderNum],
[OrderDtl].[OrderLine] as [OrderDtl_OrderLine],
[OrderDtl].[RevisionNum] as [OrderDtl_RevisionNum],
(replace(OrderDtl.XRevisionNum,' ','' )) as [Calculated_OurXRev],
[OrderRel].[OrderRelNum] as [OrderRel_OrderRelNum],
[OrderRel].[OurReqQty] as [OrderRel_OurReqQty],
[OrderRel].[ReqDate] as [OrderRel_ReqDate],
[OrderDtl].[UnitPrice] as [OrderDtl_UnitPrice],
[OrderRel].[SellingReqQty] as [OrderRel_SellingReqQty],
[OrderRel].[OurStockShippedQty] as [OrderRel_OurStockShippedQty],
(OrderRel.SellingReqQty - OrderRel.OurStockShippedQty - OrderRel.OurJobShippedQty) as [Calculated_OurRemainQty],
[OrderHed].[CustNum] as [OrderHed_CustNum],
[OrderHed].[OpenOrder] as [OrderHed_OpenOrder],
[OrderDtl].[OpenLine] as [OrderDtl_OpenLine],
[OrderRel].[OpenRelease] as [OrderRel_OpenRelease],
[OrderHed].[OrderNum] as [OrderHed_OrderNum],
(OrderRel.OurStockShippedQty + OrderRel.OurJobShippedQty) as [Calculated_Shipped]
from Erp.OrderHed as OrderHed
inner join Erp.OrderDtl as OrderDtl on
OrderHed.Company = OrderDtl.Company
and OrderHed.OrderNum = OrderDtl.OrderNum
inner join Erp.OrderRel as OrderRel on
OrderDtl.Company = OrderRel.Company
and OrderDtl.OrderNum = OrderRel.OrderNum
and OrderDtl.OrderLine = OrderRel.OrderLine
and ( OrderRel.OpenRelease = true )
where (OrderHed.CustNum = 1)) as SubQuery4
left outer join Ice.UD03 as UD03 on
SubQuery4.OrderHed_Company = UD03.Company
and SubQuery4.OrderHed_PONum = UD03.Key2
and SubQuery4.Calculated_OurXRev = MyXRev
and SubQuery4.OrderDtl_PartNum = UD03.Key4
and SubQuery4.OrderDtl_UnitPrice = UD03.Number02
and SubQuery4.OrderRel_ReqDate = UD03.Date01
and (SubQuery4.Calculated_OurRemainQty = CustFinalQty
or SubQuery4.OrderRel_SellingReqQty = UD03.Number01)
group by [SubQuery4].[OrderHed_Company],
[SubQuery4].[OrderHed_OrderNum],
[SubQuery4].[OrderHed_PONum],
[SubQuery4].[Calculated_OurXRev],
[SubQuery4].[OrderDtl_PartNum],
[SubQuery4].[OrderDtl_OrderNum],
[SubQuery4].[OrderDtl_OrderLine],
[SubQuery4].[OrderDtl_UnitPrice],
[SubQuery4].[OrderDtl_RevisionNum],
[SubQuery4].[OrderRel_ReqDate],
[SubQuery4].[Calculated_OurRemainQty],
[SubQuery4].[OrderRel_SellingReqQty],
[SubQuery4].[OrderRel_OurStockShippedQty],
(iif(right(UD03.Key3,3)='---', left(right(UD03.Key3,6),3) + '-' + UD03.ShortChar01, left(right(UD03.Key3,7),3) + right(UD03.Key3,3) + '-' + UD03.ShortChar01)),
(UD03.Number01-UD03.Number03),
[SubQuery4].[Calculated_Shipped]) as SubQuery5
left outer join (select distinct
[UD03_sq6].[Company] as [UD03_sq6_Company],
(iif(right(UD03_sq6.Key3,3)='---', left(right(UD03_sq6.Key3,6),3) + '-' + UD03_sq6.ShortChar01, left(right(UD03_sq6.Key3,7),3) + right(UD03_sq6.Key3,3) + '-' + UD03_sq6.ShortChar01)) as [Calculated_MyXRev],
(Number01-Number03) as [Calculated_CustFinalQty],
(min(UD03_sq6.Date01)) as [Calculated_MinDate],
[UD03_sq6].[Key2] as [UD03_sq6_Key2],
[UD03_sq6].[Key4] as [UD03_sq6_Key4],
[UD03_sq6].[Number01] as [UD03_sq6_Number01],
[UD03_sq6].[Number02] as [UD03_sq6_Number02],
[UD03_sq6].[Key3] as [UD03_sq6_Key3],
[UD03_sq6].[ShortChar01] as [UD03_sq6_ShortChar01],
[UD03_sq6].[Number03] as [UD03_sq6_Number03]
from (select
[OrderHed1].[Company] as [OrderHed1_Company],
[OrderHed1].[PONum] as [OrderHed1_PONum],
[OrderDtl1].[PartNum] as [OrderDtl1_PartNum],
[OrderDtl1].[OrderNum] as [OrderDtl1_OrderNum],
[OrderDtl1].[OrderLine] as [OrderDtl1_OrderLine],
(replace(OrderDtl1.XRevisionNum,' ','')) as [Calculated_OurXRev],
[OrderRel1].[OrderRelNum] as [OrderRel1_OrderRelNum],
[OrderRel1].[OurReqQty] as [OrderRel1_OurReqQty],
[OrderRel1].[ReqDate] as [OrderRel1_ReqDate],
[OrderDtl1].[UnitPrice] as [OrderDtl1_UnitPrice],
[OrderRel1].[SellingReqQty] as [OrderRel1_SellingReqQty],
[OrderRel1].[OurStockShippedQty] as [OrderRel1_OurStockShippedQty],
(OrderRel1.SellingReqQty - OrderRel1.OurStockShippedQty) as [Calculated_OurRemainQty]
from Erp.OrderHed as OrderHed1
inner join Erp.OrderDtl as OrderDtl1 on
OrderHed1.Company = OrderDtl1.Company
and OrderHed1.OrderNum = OrderDtl1.OrderNum
inner join Erp.OrderRel as OrderRel1 on
OrderDtl1.Company = OrderRel1.Company
and OrderDtl1.OrderNum = OrderRel1.OrderNum
and OrderDtl1.OrderLine = OrderRel1.OrderLine) as SubQuery7
right outer join Ice.UD03 as UD03_sq6 on
UD03_sq6.Company = SubQuery7.OrderHed1_Company
and UD03_sq6.Key2 = SubQuery7.OrderHed1_PONum
and MyXRev = SubQuery7.Calculated_OurXRev
and UD03_sq6.Key4 = SubQuery7.OrderDtl1_PartNum
and UD03_sq6.Number02 = SubQuery7.OrderDtl1_UnitPrice
and UD03_sq6.Date01 = SubQuery7.OrderRel1_ReqDate
and (CustFinalQty = SubQuery7.Calculated_OurRemainQty
or UD03_sq6.Number01 = SubQuery7.OrderRel1_SellingReqQty)
left outer join (select
[OrderHed3].[Company] as [OrderHed3_Company],
[OrderHed3].[PONum] as [OrderHed3_PONum],
[OrderHed3].[OrderNum] as [OrderHed3_OrderNum],
[OrderDtl3].[OrderLine] as [OrderDtl3_OrderLine],
[OrderDtl3].[PartNum] as [OrderDtl3_PartNum],
[OrderDtl3].[UnitPrice] as [OrderDtl3_UnitPrice],
(replace(OrderDtl3.XRevisionNum, ' ', '')) as [Calculated_OurXRev]
from Erp.OrderHed as OrderHed3
inner join Erp.OrderDtl as OrderDtl3 on
OrderHed3.Company = OrderDtl3.Company
and OrderHed3.OrderNum = OrderDtl3.OrderNum) as SubQuery9 on
UD03_sq6.Company = SubQuery9.OrderHed3_Company
and UD03_sq6.Key2 = SubQuery9.OrderHed3_PONum
and MyXRev = SubQuery9.Calculated_OurXRev
and UD03_sq6.Key4 = SubQuery9.OrderDtl3_PartNum
and UD03_sq6.Number02 = SubQuery9.OrderDtl3_UnitPrice
where (SubQuery7.OrderHed1_PONum is null and SubQuery9.OrderHed3_PONum is not null)
group by [UD03_sq6].[Company],
(iif(right(UD03_sq6.Key3,3)='---', left(right(UD03_sq6.Key3,6),3) + '-' + UD03_sq6.ShortChar01, left(right(UD03_sq6.Key3,7),3) + right(UD03_sq6.Key3,3) + '-' + UD03_sq6.ShortChar01)),
(Number01-Number03),
[UD03_sq6].[Key2],
[UD03_sq6].[Key4],
[UD03_sq6].[Number01],
[UD03_sq6].[Number02],
[UD03_sq6].[Key3],
[UD03_sq6].[ShortChar01],
[UD03_sq6].[Number03]) as SubQuery6 on
SubQuery5.OrderHed_Company = SubQuery6.UD03_sq6_Company
and SubQuery5.OrderHed_PONum = SubQuery6.UD03_sq6_Key2
and SubQuery5.Calculated_OurXRev = SubQuery6.Calculated_MyXRev
and SubQuery5.OrderDtl_PartNum = SubQuery6.UD03_sq6_Key4
and SubQuery5.OrderDtl_UnitPrice = SubQuery6.UD03_sq6_Number02
and (SubQuery5.Calculated_OurRemainQty = SubQuery6.Calculated_CustFinalQty
or SubQuery5.OrderRel_SellingReqQty = SubQuery6.UD03_sq6_Number01)
left outer join (select
[JobHead].[Company] as [JobHead_Company],
[JobHead].[PartNum] as [JobHead_PartNum],
[JobHead].[RevisionNum] as [JobHead_RevisionNum],
(min(JobHead.JobNum)) as [Calculated_FirstJob]
from Erp.JobHead as JobHead
where (JobHead.JobClosed = false)
group by [JobHead].[Company],
[JobHead].[PartNum],
[JobHead].[RevisionNum]) as SubQuery10 on
SubQuery5.OrderHed_Company = SubQuery10.JobHead_Company
and SubQuery5.OrderDtl_PartNum = SubQuery10.JobHead_PartNum
and SubQuery5.OrderDtl_RevisionNum = SubQuery10.JobHead_RevisionNum
order by SubQuery5.OrderDtl_PartNum, SubQuery5.OrderDtl_OrderNum, SubQuery5.OrderDtl_OrderLine, SubQuery5.Calculated_FirstRel
This monstrosity of an updatable BAQ is one of the ways we deal with a customer file containing order information. The overarching goal of this particular BAQ is to compare the customer’s requested orders (held in UD03), with the order/releases we have open in Epicor (from OrderHed,Dtl,Rel). The resulting list should show our open releases that can not be matched to the customer’s requested orders based on a host of criteria. Along with this, the BAQ calculates a suggested action for the release. Either close the release, or, if the PO, Line (Xrev), Part, and Quantity match, then we can just change the date on the release to a date that the customer requests.
With the data I have now, this BAQ returns about 2100 rows in 5.4 to 6.7 seconds. I would like to get that down to a second or less if possible. I have done a few things to improve the performance of this BAQ. First, I made sure to include Company as the first join in all joins. I also moved most of my join fields in UD03 into key fields to utilize indexing. I looked into changing the outer joins for inner joins, but that is where I got stuck. I am also joining on a few calculated fields. Could that also impact performance?
Without completely doing my job for me, can you make any suggestions based on the SQL and requirements I gave? How would you go about replacing outer joins with inner? Would that even help my efficiency?
Thanks for your time!
Nate
See the related topic below for more info: