减少SQL查询执行时间

时间:2015-05-07 04:52:55

标签: sql-server sql-server-2005 crystal-reports

我的情况是我们的Web应用程序根据查询结果生成一个Crystal Report,执行时间大约需要1小时。

我知道这很疯狂但是,任何人都可以建议在以下巨型查询中进行任何优化或更改。

感谢。

注意:我缩短了一些类似的查询。

CREATE procedure [dbo].[sp_ExportRevenueMenifest_27FEB] 
   (@vessel varchar(10), @voyage varchar(10),
    @call varchar(10), @GLC varchar(10),
    @Port varchar(10), @str varchar(200)) 
AS 
   create table #OCF (PortCode varchar(10), 
                      PortName varchar(40),
                      BLNumber varchar(20), RS decimal(18,4),
                      RS1 decimal(18,4), Consignee varchar(500),
                      NoPackages varchar(50), Vessel varchar(10),
                      Vayage varchar(10), Call char(2),
                      Vessel_Name varchar(50),
                      Sailed_Date varchar(20), ExRate decimal(8,4)
                     )
insert into #OCF 
SELECT     a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtUSD) AS PrepaidAmtUSD,SUM(B.COLLECTAmtUSD) AS COLLECTAmtUSD, a.Consignee, a.NoPackages, a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
                      ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND 
                      a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
                      Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
                      Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND (B.ConfigCode = 'OCF')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name, 
                      ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate, B.ConfigCode


create table #DDC (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #DDC 
SELECT     a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtUSD) AS PrepaidAmtUSD,SUM(B.COLLECTAmtUSD) AS COLLECTAmtUSD, a.Consignee, a.NoPackages, a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
                      ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND 
                      a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
                      Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
                      Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND (B.ConfigCode = 'DDC')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name, 
                      ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate, B.ConfigCode

create table #FUM (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #FUM
SELECT     a.PortofDischarge, ....., a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
                      ....INNER JOIN
                      Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND (B.ConfigCode = 'FUM')
GROUP BY Port_Master.Port_Name,.... B.ConfigCode


create table #PSS (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #PSS
SELECT     a.PortofDischarge, .... a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
                      ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND 
                      a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
                      Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
                      Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND (B.ConfigCode = 'PSS')
GROUP BY Port_Master.Port_Name, ....., B.ConfigCode


create table #BAF (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #BAF
SELECT     a.PortofDischarge, .... a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
                      ...... ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND (B.ConfigCode = 'BAF')
GROUP BY Port_Master.Port_Name, ..... B.ConfigCode


create table #HD (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #HD
SELECT     a.PortofDischarge, ..... a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN

                      BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
                      ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND 
                      ...... ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND (B.ConfigCode = 'HD')
GROUP BY Port_Master.Port_Name, ..... B.ConfigCode

create table #WRS (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #WRS
SELECT     a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtUSD) AS PrepaidAmtUSD,SUM(B.COLLECTAmtUSD) AS COLLECTAmtUSD, a.Consignee, a.NoPackages, a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
                      ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND 
                      a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
                      Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
                      Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND (B.ConfigCode = 'WRS')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name, 
                      ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate, B.ConfigCode


create table #DIV (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #DIV
SELECT     a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtUSD) AS PrepaidAmtUSD,SUM(B.COLLECTAmtUSD) AS COLLECTAmtUSD, a.Consignee, a.NoPackages, a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
                      ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND 
                      a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
                      Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
                      Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND (B.ConfigCode = 'DIV')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name, 
                      ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate, B.ConfigCode

create table #PAN (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #PAN
SELECT     a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtUSD) AS PrepaidAmtUSD,SUM(B.COLLECTAmtUSD) AS COLLECTAmtUSD, a.Consignee, a.NoPackages, a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
                      ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND 
                      a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
                      Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
                      Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND (B.ConfigCode = 'PAN')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name, 
                      ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate, B.ConfigCode
create table #CAF (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #CAF
SELECT     a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtUSD) AS PrepaidAmtUSD,SUM(B.COLLECTAmtUSD) AS COLLECTAmtUSD, a.Consignee, a.NoPackages, a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
                      ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND 
                      a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
                      Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
                      Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND (B.ConfigCode = 'CAF')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name, 
                      ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate, B.ConfigCode

create table #CDR (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #CDR
SELECT     a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtUSD) AS PrepaidAmtUSD,SUM(B.COLLECTAmtUSD) AS COLLECTAmtUSD, a.Consignee, a.NoPackages, a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      ..... ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND (B.ConfigCode = 'CDR')
GROUP BY Port_Master.Port_Name, .... B.ConfigCode

create table #THC (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18),RS1 decimal(18),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #THC
SELECT     a.PortofDischarge, ......, a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
                      ......
WHERE     (a.ImportExport = 2) AND (B.ConfigCode = 'THC')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name, 
                      ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate, B.ConfigCode

create table #INL (PortCode varchar(10),....,ExRate decimal(8,4))
insert into #INL
SELECT     a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtRupee) AS PrepaidAmtRupee,SUM(B.CollectAmtRupee) AS collectAmtRupee, a.Consignee, a.NoPackages, a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      ......
WHERE     (a.ImportExport = 2) AND (B.ConfigCode = 'INL')
GROUP BY Port_Master.Port_Name, ......, B.ConfigCode

create table #ARB (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #ARB
SELECT     a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtRupee) AS PrepaidAmtRupee,SUM(B.CollectAmtRupee) AS collectAmtRupee, a.Consignee, a.NoPackages, a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
                      ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND 
                      a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
                      Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
                      Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND (B.ConfigCode = 'ARB')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name, 
                      ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate, B.ConfigCode


create table #DOC (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #DOC
SELECT     a.PortofDischarge, .... a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
                      ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND 
                      .... ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND (B.ConfigCode = 'DOC')
GROUP BY Port_Master.Port_Name, ...., B.ConfigCode


create table #THCUSD (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #THCUSD 
SELECT     a.PortofDischarge, .... a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
                      ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND 
                      .... ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND (b.ConfigCode='THC')
GROUP BY Port_Master.Port_Name,...., a.ExchangeRate


create table #INLUSD (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #INLUSD 
SELECT     a.PortofDischarge, .... a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
                      ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND 
                      .....ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND (b.ConfigCode='INL')
GROUP BY Port_Master.Port_Name,.... a.ExchangeRate

create table #ARBUSD (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #ARBUSD 
SELECT     a.PortofDischarge, ....., a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      .....
                      Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND (b.ConfigCode='ARB')
GROUP BY Port_Master.Port_Name,.... a.ExchangeRate


create table #OTH (PortCode varchar(10), PortName varchar(40),BLNumber varchar(20), RS decimal(18,4),RS1 decimal(18,4),Consignee varchar(500),NoPackages varchar(50),Vessel varchar(10),Vayage varchar(10),Call char(2),Vessel_Name varchar(50),Sailed_Date varchar(20),ExRate decimal(8,4))
insert into #OTH
SELECT     a.PortofDischarge, Port_Master.Port_Name, a.BLNumber, SUM(B.PrepaidAmtUSD) AS PrepaidAmtUSD,SUM(B.COLLECTAmtUSD) AS COLLECTAmtUSD, a.Consignee, a.NoPackages, a.Vessel, 
                      a.Voyage, a.Call, Vessel_Master.Vessel_Name, ExportVoyage_Master.Sailed_Date, a.ExchangeRate
FROM         BLMaster a INNER JOIN
                      BL_ChargesDetails B ON a.BLID = B.BLID INNER JOIN
                      ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND 
                      a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
                      Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code INNER JOIN
                      Port_Master ON a.PortofDischarge = Port_Master.Port_Code
WHERE     (a.ImportExport = 2) AND b.ConfigCode NOT IN('OCF','FUM','BAF','YAS','CAF','HD','DIV','PSS','CAF','PSS','PAN','WRS','CDR','THC','INL','DDC','ARB','DOC','STX')
GROUP BY Port_Master.Port_Name, a.BLNumber, a.Consignee, a.NoPackages, a.Vessel, a.Voyage, a.Call, Vessel_Master.Vessel_Name, 
                      ExportVoyage_Master.Sailed_Date, a.PortofDischarge, a.ExchangeRate



DECLARE @Query varchar(8000)
SET @Query = ''




SELECT @Query ='SELECT     Port_Master.Port_Name,Vessel_Master.Vessel_Name, Vessel_Master.Vessel_Code, ExportVoyage_Master.Voyage_Code, ExportVoyage_Master.Sailed_Date, 
                      Vessel_Master.Nationality, ExportVoyage_Master.Master, a.Portofloading,a.PortofDischarge,a.FinalDestination, ExportVoyage_Master.Call,ExportVoyage_Master.Berth_Date,a.BLNumber,a.Shipper, a.Consignee,a.port,
                      a.MarksNo, a.GoodsDescription, a.NoPackages, BL_ClubbedContainer.ContainerNo,   
                      BL_ClubbedContainer.TypeSize, A.ExchangeRate, a.BrokerCode,
                     isnull(OC.RS,0)OCF,isnull(FU.RS,0)FUM,isnull(BA.RS,0)BAF,isnull(CA.RS,0)CAF,isnull(HD.RS,0)HD,isnull(DI.RS,0)DIV,isnull(PS.RS,0)PSS,isnull(PA.RS,0)PAN,isnull(WR.RS,0)WRS,isnull(CD.RS,0)CDR,isnull(TH.RS,0)THC,isnull(DD.RS,0)DDC,isnull(INL.RS,0)INL,isnull(AR.RS,0)ARB,isnull(DO.RS,0)DOC,isnull(OT.RS,0)OTH,isnull(THUS.RS,0)THCUSD,isnull(INUS.RS,0)INLUSD,isnull(ARUS.RS,0)ARBUSD,isnull(OC.RS1,0)OCF1,isnull(FU.RS1,0)FUM1,isnull(BA.RS1,0)BAF1,isnull(CA.RS1,0)CAF1,isnull(HD.RS1,0)HD1,isnull(DI.RS1,0)DIV1,isnull(PS.RS1,0) PSS1,isnull(PA.RS1,0) PAN1,isnull(WR.RS1,0) WRS1,isnull(CD.RS1,0)CDR1,isnull(TH.RS1,0)THC1,isnull(DD.RS1,0)DDC1,isnull(INL.RS1,0)INL1,isnull(AR.RS1,0)ARB1,isnull(DO.RS1,0) DOC1,isnull(OT.RS1,0)OTH1,isnull(THUS.RS1,0)THCUSD1,isnull(INUS.RS1,0)INLUSD1,isnull(ARUS.RS1,0)ARBUSD1
FROM         BLMaster a LEFT OUTER JOIN

                     BL_ClubbedContainer ON a.BLNumber = BL_ClubbedContainer.BLNumber INNER JOIN
                      ExportVoyage_Master ON a.Vessel = ExportVoyage_Master.Vessel_Code AND a.Voyage = ExportVoyage_Master.Voyage_Code AND 
                      a.Port = ExportVoyage_Master.Port AND a.Call = ExportVoyage_Master.Call INNER JOIN
                      Vessel_Master ON ExportVoyage_Master.Vessel_Code = Vessel_Master.Vessel_Code inner join port_master on a.FinalDestination=Port_Master.Port_Code
left outer join #OCF OC on OC.blnumber=a.blnumber 
left outer join #DDC DD on DD.blnumber=a.blnumber 
left outer join #FUM FU on FU.blnumber=a.blnumber 
left outer join #BAF BA on BA.blnumber=a.blnumber 
left outer join #HD HD on HD.blnumber=a.blnumber 
left outer join #DIV DI on DI.blnumber=a.blnumber 
left outer join #WRS WR on WR.blnumber=a.blnumber 
left outer join #CAF CA on CA.blnumber=a.blnumber 
left outer join #PSS PS on PS.blnumber=a.blnumber 
left outer join #PAN PA on PA.blnumber=a.blnumber 
left outer join #CDR CD on CD.blnumber=a.blnumber 
left outer join #THC TH on TH.blnumber=a.blnumber 
left outer join #INL INL on INL.blnumber=a.blnumber 
left outer join #ARB AR on AR.blnumber=a.blnumber 
left outer join #DOC DO on DO.blnumber=a.blnumber 
left outer join #OTH OT on OT.blnumber=a.blnumber 
left outer join #THCUSD THUS on THUS.blnumber=a.blnumber 
left outer join #INLUSD INUS on INUS.blnumber=a.blnumber 
left outer join #ARBUSD ARUS on ARUS.blnumber=a.blnumber 
WHERE     (a.ImportExport = 2) '+ @str + ' order by a.Portofdischarge,a.blnumber asc'
EXEC (@Query)

2 个答案:

答案 0 :(得分:0)

谢谢你们的努力和时间来研究我的问题。

该查询用于从BLMaster中提取所有记录,而不使用传递给存储过程的参数进行过滤。

P.S。感谢我的供应商。

WHERE (a.ImportExport = 2) AND (B.ConfigCode = 'OCF')
/*  Added below two lines to all WHERE clause solved my issue  */
AND a.Vessel = @vessel
AND a.Voyage = @voyage

表BLMaster有大约12万个记录,因为这需要很长时间。

答案 1 :(得分:0)

根据我的经验  1)尝试在使用前将参数放入局部变量。

create procedure [dbo].[usp_test](@Id varchar(20))
as
begin
  select * from Test
  where Id = @Id
end

alter procedure [dbo].[usp_test](@Id varchar(20))
as
begin
  declare @local_id varchar(20) = @Id

  select * from Test
  where Id = @local_id
end

2)使用重新编译提示。这将获得更适合基于参数值的查询的新查询计划。

exec dbo.usp_test 1 with recompile

参考:https://stackoverflow.com/a/30092833/1554116