SQL优化内存问题

时间:2016-08-26 06:06:24

标签: sql optimization

我不是最有经验的SQL编写者,但是这里有。我有一个查询,它运行以显示表CusHAWB中的项目列表。可能是此表中的2000 - 3000条记录。我有一个单独的表JobPackLines,其中有另外2000-3000条记录。我需要运行并检查CUSHAWB并加入JobPacklines。 之后,我需要识别JobPackLines中不存在的JobPackLine记录。

在添加UNION之前查询运行正常,但之后它一直给我低内存错误:(。我想我需要帮助优化这个查询?

这是我的疑问:

Select 
CusMAWB.CM_MAWB AS Masterbill, 
CS_ConsignmentNum AS LineItem,
CS_HAWB AS HouseBill,
JL_RefNumber AS Number,
CASE CS_CustomsStatus 
    WHEN 'ACK' then 'Acknowledgement'
    WHEN 'IAR' Then 'Inspections/Audit Requirement'
    WHEN 'CLR' then 'ECI Accepted'
    WHEN 'CAN' then 'Cancelled'
    WHEN 'ERR' then 'ECI in Error'
    WHEN 'REF' then 'ECI Rejected'
    WHEN 'NSC' then 'Not sent to Customs'
    WHEN 'STC' then 'Sent to Customs'
    WHEN 'FEQ' then 'Formal Declaration Required'
    WHEN 'WOF' then 'Consignment Written Off'
    Else CS_CustomsStatus end  AS CustomsStatus,
    CASE    WHEN CS_CustomsStatus = 'WOF' AND JobPackLines.JL_RefNumber IS NOT NULL then 'Cleared'
    WHEN CS_CustomsStatus = 'WOF' AND JobPackLines.JL_RefNumber IS NULL then 'Missing' 
    WHEN CS_CustomsStatus  = 'FEQ' then 'Held' end AS OutturnStatus,
JobPackLines.JL_CustomAttrib2 AS SortLocation,
JobPackLines.JL_CustomAttrib4 AS ScanDate,
JobPackLines.JL_PackageCount AS Packs,
JobPackLines.JL_ActualWeight AS Weight,
JobPackLines.JL_Length AS Length,
JL_Width AS width,
JL_Height AS Height,
JL_ActualVolume AS Volume,
JL_CustomAttrib1 as TPID,
JL_CustomAttrib3 AS FreightFlowID,
JL_CustomFlag1 AS ManualSort,
'?' AS ManifestCheckBox,
CM_DepartureDate AS DepartureDate,
CASE WHEN JS_UniqueConsignRef IS NULL then 'No Shipment' WHEN JS_UniqueConsignRef =''then 'No Shipment' else JS_UniqueConsignref end AS Shipment,
CASE WHEN CS_CustomsStatus = 'WOF' then 1 else 0 end AS TotalClearedexpected,
CASE WHEN JobPackLines.JL_PK IS NOT NULL AND CS_CustomsStatus = 'WOF' then 1 else 0 end AS TotalClearedScanned,
CASE WHEN CS_CustomsStatus='FEQ' then 1 else 0 end AS TotalHeldExpected,
CASE WHEN JobPackLines.JL_PK IS NOT NULL AND CS_CustomsStatus='FEQ' then 1 else 0 end AS TotalHeldScanned,
CASE WHEN JL_PK IS NULL  then 1 else 0 end AS TotalMissingItems,
CASE WHEN JobPackLines.JL_PK IS NOT NULL AND JL_CustomFlag2='Y' then 1 else 0 end AS TotalNonManifested,
CASE WHEN JobPackLines.JL_CustomFlag1 ='Y' then 1 else 0 end AS TotalManualSort,
CM_MessageReference AS ECI

from CusMAWB 
LEFT JOIN CusHAWB on CS_CM=CM_PK AND CS_HAWB <> ''
LEFT JOIN Jobshipment on CS_JS=JS_PK
LEFT JOIN JobPackLines on JL_JS=JS_PK AND JL_RefNumber=CS_HAWB

Union

Select 
CusMAWB.CM_MAWB AS Masterbill, 
CS_ConsignmentNum AS LineItem,
'' AS HouseBill,
JL_RefNumber AS Number,
''  AS CustomsStatus,
'Missing' AS OutturnStatus,
JobPackLines.JL_CustomAttrib2 AS SortLocation,
JobPackLines.JL_CustomAttrib4 AS ScanDate,
JobPackLines.JL_PackageCount AS Packs,
JobPackLines.JL_ActualWeight AS Weight,
JobPackLines.JL_Length AS Length,
JL_Width AS width,
JL_Height AS Height,
JL_ActualVolume AS Volume,
JL_CustomAttrib1 as TPID,
JL_CustomAttrib3 AS FreightFlowID,
JL_CustomFlag1 AS ManualSort,
'?' AS ManifestCheckBox,
'' AS DepartureDate,
CASE WHEN JS_UniqueConsignRef IS NULL then 'No Shipment' WHEN JS_UniqueConsignRef =''then 'No Shipment' else JS_UniqueConsignref end AS Shipment,
0 AS TotalClearedexpected,
0 AS TotalClearedScanned,
0 AS TotalHeldExpected,
0 AS TotalHeldScanned,
1 AS TotalMissingItems,
CASE WHEN JobPackLines.JL_PK IS NOT NULL AND JL_CustomFlag2='Y' then 1 else 0 end AS TotalNonManifested,
CASE WHEN JobPackLines.JL_CustomFlag1 ='Y' then 1 else 0 end AS TotalManualSort,
CM_MessageReference AS ECI

from CusMAWB 
LEFT JOIN CusHAWB on CS_CM=CM_PK AND CS_HAWB <> ''
LEFT JOIN Jobshipment on CS_JS=JS_PK
LEFT JOIN JobPackLines on JL_JS=JS_PK AND JL_RefNumber<>CS_HAWB

0 个答案:

没有答案
相关问题