Can Anyone help me with this?? Running SQL Server 2000 -- I have this query, a fairly simple one...but hits several tables
and has several case statements and LEFT OUTER JOINS. I know this is not the best way to go about this, because it takes over 45 second to run for just one PO_NUMBER. Not Good. But I just don't know how to get around all this. Can anyone show me how I might make this a little better / quicker? Any ideas?
Thanks in advance.
Crystal QB
SELECT DISTINCT
PO.PO_NUMBER,
PO.COMPANY,
PO.PO_RELEASE,
PO.PO_CODE,
PO.PO_DATE,
PO.PO_REVISION,
PO.TERM_CODE,
PO.PO_USER_FLD_5+' '+PO.PO_USER_FLD_3 as REQ_NBR,
CASE
WHEN PO.FREIGHT_TERMS = 'P'
THEN 'PREPAID'
WHEN PO.FREIGHT_TERMS = 'C'
THEN 'COLLECT'
WHEN PO.FREIGHT_TERMS = 'A'
THEN 'PPD & ADD'
ELSE PO.FREIGHT_TERMS
END FREIGHT_TERMS,
PO.SHIP_VIA,
CASE
WHEN PO.FOB_CODE = 'SP'
THEN 'SHIPPING POINT'
WHEN PO.FOB_CODE = 'DS'
THEN 'DESTINATION'
ELSE PO.FOB_CODE
END SHIP_TERMS,
PO.DFLT_DL_DATE,
CASE
WHEN PO.CURRENCY_CODE = ' '
THEN 'USD'
ELSE PO.CURRENCY_CODE
END CURRENCY,
BUYER.[NAME] as BUYER_NAME,
BUYER.PHONE_PREFIX AS Buyer_PhnPrefx,
BUYER.PHONE AS Buyer_Phone,
POL.LINE_NBR,
POL.ITEM,
POL.[DESCRIPTION],
POL.ENT_BUY_UOM,
POL.ENT_UNIT_CST,
POL.ITEM_TYPE,
POL.QUANTITY,
POL.CXL_QTY,
POL.REC_QTY,
POL.EARLY_DL_DATE,
POL.BUYER_CODE,
POL.TAX_CODE,
POL.EXTENDED_AMT,
rtrim(ltrim(POL.PO_USER_FLD_4)) + ' ' + rtrim(ltrim(POL.PO_USER_FLD_6)) as POLineUserCodes,
CASE
WHEN POL.USER_DATE_3 > '12/31/1753'
THEN POL.USER_DATE_3
END OnHireDate,
CASE
WHEN POL.USER_DATE_3 > '12/31/1753'
THEN POL.USER_DATE_4
END OffHireDate,
ICL.PO_NAME as LNbr_ShipTo_Name,
ICL.PO_ADDR1 as LNbr_ShipTo_Addr1,
ICL.PO_ADDR2 as LNbr_ShipTo_Addr2,
ICL.PO_ADDR3 as LNbr_ShipTo_Addr3,
ICL.PO_ADDR4 as LNbr_ShipTo_Addr4,
ICL.PO_CITY_ADDR5 as LNbr_ShipTo_City,
ICL.PO_STATE_PROV as LNbr_ShipTo_State,
ICL.PO_POSTAL_CD as LNbr_ShipTo_Zip,
ICL.PO_COUNTRY as LNbr_ShipTo_Country,
MMDIST.SYSTEM_CD,
MMDIST.DOC_TYPE,
MMDIST.COMPONENT_SEQ,
MMDIST.AOC_CODE,
MMDIST.ACCT_UNIT,
MMDIST.ACCOUNT,
MMDIST.DIST_COMPANY,
MMDIST.ACTIVITY,
MMDIST.ACCT_CATEGORY,
CASE
WHEN SUBSTRING(L_HICO.OBJECT,6,1) = 'A'
THEN SUBSTRING(L_HICO.OBJECT,(L_HICO.HEADSIZE+1),LEN(L_HICO.OBJECT))
END IMComment
FROM
dbo.PURCHORDER PO
LEFT OUTER JOIN
dbo.POLINE POL
ON PO.COMPANY = POL.COMPANY
AND PO.PO_NUMBER = POL.PO_NUMBER
AND PO.PO_RELEASE = POL.PO_RELEASE
AND PO.PO_CODE = POL.PO_CODE
LEFT OUTER JOIN
dbo.ICLOCATION ICL
ON POL.COMPANY = ICL.COMPANY
AND POL.LOCATION = ICL.LOCATION
AND POL.LOCATION <> PO.LOCATION
LEFT OUTER JOIN
dbo.APVENMAST VENMASTR
ON PO.VENDOR = VENMASTR.VENDOR
LEFT OUTER JOIN
dbo.BUYER BUYER
ON PO.BUYER_CODE = BUYER.BUYER_CODE
AND POL.PROCURE_GROUP = BUYER.PROCURE_GROUP
LEFT OUTER JOIN
dbo.MMDIST MMDIST
ON POL.PO_NUMBER = MMDIST.DOC_NUMBER
AND POL.COMPANY = MMDIST.COMPANY
AND POL.PO_RELEASE = MMDIST.DOC_NBR_NUM
AND POL.PO_CODE = MMDIST.PO_CODE
AND POL.LINE_NBR = MMDIST.LINE_NBR
AND POL.LOCATION = MMDIST.LOCATION
LEFT OUTER JOIN
dbo.ITCOMMENT ITCOMMENT
ON POL.ITEM = ITCOMMENT.ITEM
LEFT OUTER JOIN
dbo.L_HICO L_HICO
ON POL.L_INDEX = L_HICO.L_INDEX
WHERE
rtrim(ltrim(PO.PO_NUMBER)) = '100207489'
and has several case statements and LEFT OUTER JOINS. I know this is not the best way to go about this, because it takes over 45 second to run for just one PO_NUMBER. Not Good. But I just don't know how to get around all this. Can anyone show me how I might make this a little better / quicker? Any ideas?
Thanks in advance.
Crystal QB
SELECT DISTINCT
PO.PO_NUMBER,
PO.COMPANY,
PO.PO_RELEASE,
PO.PO_CODE,
PO.PO_DATE,
PO.PO_REVISION,
PO.TERM_CODE,
PO.PO_USER_FLD_5+' '+PO.PO_USER_FLD_3 as REQ_NBR,
CASE
WHEN PO.FREIGHT_TERMS = 'P'
THEN 'PREPAID'
WHEN PO.FREIGHT_TERMS = 'C'
THEN 'COLLECT'
WHEN PO.FREIGHT_TERMS = 'A'
THEN 'PPD & ADD'
ELSE PO.FREIGHT_TERMS
END FREIGHT_TERMS,
PO.SHIP_VIA,
CASE
WHEN PO.FOB_CODE = 'SP'
THEN 'SHIPPING POINT'
WHEN PO.FOB_CODE = 'DS'
THEN 'DESTINATION'
ELSE PO.FOB_CODE
END SHIP_TERMS,
PO.DFLT_DL_DATE,
CASE
WHEN PO.CURRENCY_CODE = ' '
THEN 'USD'
ELSE PO.CURRENCY_CODE
END CURRENCY,
BUYER.[NAME] as BUYER_NAME,
BUYER.PHONE_PREFIX AS Buyer_PhnPrefx,
BUYER.PHONE AS Buyer_Phone,
POL.LINE_NBR,
POL.ITEM,
POL.[DESCRIPTION],
POL.ENT_BUY_UOM,
POL.ENT_UNIT_CST,
POL.ITEM_TYPE,
POL.QUANTITY,
POL.CXL_QTY,
POL.REC_QTY,
POL.EARLY_DL_DATE,
POL.BUYER_CODE,
POL.TAX_CODE,
POL.EXTENDED_AMT,
rtrim(ltrim(POL.PO_USER_FLD_4)) + ' ' + rtrim(ltrim(POL.PO_USER_FLD_6)) as POLineUserCodes,
CASE
WHEN POL.USER_DATE_3 > '12/31/1753'
THEN POL.USER_DATE_3
END OnHireDate,
CASE
WHEN POL.USER_DATE_3 > '12/31/1753'
THEN POL.USER_DATE_4
END OffHireDate,
ICL.PO_NAME as LNbr_ShipTo_Name,
ICL.PO_ADDR1 as LNbr_ShipTo_Addr1,
ICL.PO_ADDR2 as LNbr_ShipTo_Addr2,
ICL.PO_ADDR3 as LNbr_ShipTo_Addr3,
ICL.PO_ADDR4 as LNbr_ShipTo_Addr4,
ICL.PO_CITY_ADDR5 as LNbr_ShipTo_City,
ICL.PO_STATE_PROV as LNbr_ShipTo_State,
ICL.PO_POSTAL_CD as LNbr_ShipTo_Zip,
ICL.PO_COUNTRY as LNbr_ShipTo_Country,
MMDIST.SYSTEM_CD,
MMDIST.DOC_TYPE,
MMDIST.COMPONENT_SEQ,
MMDIST.AOC_CODE,
MMDIST.ACCT_UNIT,
MMDIST.ACCOUNT,
MMDIST.DIST_COMPANY,
MMDIST.ACTIVITY,
MMDIST.ACCT_CATEGORY,
CASE
WHEN SUBSTRING(L_HICO.OBJECT,6,1) = 'A'
THEN SUBSTRING(L_HICO.OBJECT,(L_HICO.HEADSIZE+1),LEN(L_HICO.OBJECT))
END IMComment
FROM
dbo.PURCHORDER PO
LEFT OUTER JOIN
dbo.POLINE POL
ON PO.COMPANY = POL.COMPANY
AND PO.PO_NUMBER = POL.PO_NUMBER
AND PO.PO_RELEASE = POL.PO_RELEASE
AND PO.PO_CODE = POL.PO_CODE
LEFT OUTER JOIN
dbo.ICLOCATION ICL
ON POL.COMPANY = ICL.COMPANY
AND POL.LOCATION = ICL.LOCATION
AND POL.LOCATION <> PO.LOCATION
LEFT OUTER JOIN
dbo.APVENMAST VENMASTR
ON PO.VENDOR = VENMASTR.VENDOR
LEFT OUTER JOIN
dbo.BUYER BUYER
ON PO.BUYER_CODE = BUYER.BUYER_CODE
AND POL.PROCURE_GROUP = BUYER.PROCURE_GROUP
LEFT OUTER JOIN
dbo.MMDIST MMDIST
ON POL.PO_NUMBER = MMDIST.DOC_NUMBER
AND POL.COMPANY = MMDIST.COMPANY
AND POL.PO_RELEASE = MMDIST.DOC_NBR_NUM
AND POL.PO_CODE = MMDIST.PO_CODE
AND POL.LINE_NBR = MMDIST.LINE_NBR
AND POL.LOCATION = MMDIST.LOCATION
LEFT OUTER JOIN
dbo.ITCOMMENT ITCOMMENT
ON POL.ITEM = ITCOMMENT.ITEM
LEFT OUTER JOIN
dbo.L_HICO L_HICO
ON POL.L_INDEX = L_HICO.L_INDEX
WHERE
rtrim(ltrim(PO.PO_NUMBER)) = '100207489'