在where子句中使用case语句时优化查询

时间:2013-02-15 06:41:57

标签: sql sql-server-2008

我有以下sql查询,当我运行没有 where子句时使用case statements时,它在1秒内运行完美,但是当我运行这个时它花了太长时间使用where clause

case statements

所以问题是如何优化这个where clause以便它完美运行。

   declare @JailCode int =141 
            declare @PrisonerType nvarchar(4)='All'  
            declare @Location nvarchar(4)='All'    
            declare @DATE1 smalldatetime='01/01/2009'  
            declare @DATE2 smalldatetime='12/31/2009'       
            declare @Caste nvarchar(10)='All'    
            declare @Religion nvarchar(10) ='All'   
            declare @Occupation int=0  
            declare @Qualification int=0  
            declare @Tendency nvarchar(10)='All'    
            declare @Risk nvarchar(10)='All'    
            declare @Annual nvarchar(10)='All'    
            declare @Marital nvarchar(10)='All'         
            declare @Sex nvarchar(10)='All'    
            declare @Nationality int=0  
            declare @Offence int=0  




    SELECT PT.P_Name as Prisoner_Type,      
    PE.PID_No,PE.Jail_Code,PE.Entry_Year,Convert(varchar(10),PE.Admission_Date,103) Admission_Date,PE.JID_No,Convert(varchar(10),PE.Release_Date,103) Release_Date ,  
    PM.Prisoner_Name, SUBSTRING(PM.Father_Name, 0, 17)as Father_Name, PM.Mother_Name,PM.Spouse_Name,PM.Caste_Code,
    PM.Religion_Code,PM.Occupation_Code,PM.Qualification,PM.Tendency_Type,PM.Risk_Type,PM.Annual_Income_Code,PM.Marital_Status_Code,PM.Id_Mark,
    PM.Sex_Code,PM.Nationality_Code,PM.Off_Code,PM.Present_Add, Age as age ,PM.Permt_Add, LS.Location  AS Location 
    ,NT.Nation_Name , JT.Jail_Address
    --,PrisonerPicture.Photo
    ,OT.Offence_Name,QT.Qualification as quali ,IT.Annual_Income_Range,CT.Caste_Name
    ,RT.Religion_Name,MT.Marital_Status_Name,OFT.Occ_Name AS Occupation
    , STUFF((    SELECT ', ' + cc.Act_Sec AS [text()]

    FROM CourtCases cc
    WHERE
    cc.PID_NO = PE.PID_NO and (',' + Replace(pe.Case_No,'$',',') +',' LIKE '%,' + CONVERT(VARCHAR, cc.Case_No) + ',%') group by cc.Act_Sec
    FOR XML PATH('')     ), 1, 1, '' )

    AS [Act_Sec]
    , STUFF((    SELECT ', ' + Convert(varchar(10),CC.Fir_Date,103) AS [text()]
    FROM CourtCases cc
    WHERE
    cc.PID_NO = PE.PID_NO and (',' + Replace(pe.Case_No,'$',',') +',' LIKE '%,' + CONVERT(VARCHAR, cc.Case_No) + ',%') group by  Convert(varchar(10),CC.Fir_Date,103) 
    FOR XML PATH('')     ), 1, 1, '' )

    AS [Fir_Date]

    ,STUFF((    SELECT ', ' + FIR_No AS [text()]
    FROM CourtCases cc
    WHERE
    cc.PID_NO = PE.PID_NO and (',' + Replace(pe.Case_No,'$',',') +',' LIKE '%,' + CONVERT(VARCHAR, cc.Case_No) + ',%') group by FIR_No
    FOR XML PATH('')     ), 1, 1, '' )

    AS [FIR_No]

    ,  STUFF((    SELECT ', ' + ct.Court_Name AS [text()]

    FROM CourtCases cc inner join Court_Tab as ct 
    ON cc.Court_Code=convert(nvarchar(50),ct.Court_Code)
    WHERE
    cc.PID_NO = PE.PID_NO and (',' + Replace(pe.Case_No,'$',',') +',' LIKE '%,' + CONVERT(VARCHAR, cc.Case_No) + ',%') group by  ct.Court_Name
    FOR XML PATH('')     ), 1, 1, '' )
    AS [Court_Name]

    , STUFF((    SELECT ', ' + pt.PS_Name AS [text()]                      
    FROM CourtCases cc inner join PS_Tab as pt 
    ON convert(nvarchar(50),cc.PS_Code)=convert(nvarchar(50),pt.PS_Code)
    WHERE
    cc.PID_NO = PE.PID_NO and (',' + Replace(pe.Case_No,'$',',') +',' LIKE '%,' + CONVERT(VARCHAR, cc.Case_No) + ',%') group by pt.PS_Name
    FOR XML PATH('')     ), 1, 1, '' )

    AS [PS_Name]

    FROM PrisonMaster AS PM 
    INNER  JOIN   PrisonerEntry     AS PE        ON PE.PID_No = PM.PID_No  AND   PE.Jail_Code=@JailCode

    INNER   JOIN  Nation_Tab        AS NT        ON PM.Nationality_Code = NT.Nation_Code
    INNER JOIN  Sex_Tab         AS S         ON PM.Sex_Code = S.Sex_Code

    INNER  JOIN  Jail_Tab       AS JT        ON PE.Jail_Code = JT.Jail_Code
    INNER   JOIN  Prisoner_Tab   AS PT        ON PE.Prisoner_Type=PT.P_Type
    INNER  JOIN  Loc_Status     AS LS        ON PE.Location=LS.Loc_Status
    left JOIN   Offence_Tab AS    OT        ON OT.Offence_Code=PM.Occupation_Code
    left JOIN Qualification_Tab AS QT       ON QT.Q_Code=PM.Qualification
    left JOIN Income_Tab AS IT               ON IT.Annual_Income_Code=PM.Annual_Income_Code
    left JOIN Caste_Tab AS CT                ON CT.Caste_Code=PM.Caste_Code
    LEFT JOIN Religion_Tab AS RT             ON RT.Religion_Code=PM.Religion_Code
    LEFT JOIN Marital_Tab AS MT              ON MT.Marital_Status_Code=PM.Marital_Status_Code
    LEFT JOIN Occupation_Tab AS OFT          ON     OFT.Occ_Code=PM.Occupation_Code
    WHERE   

    PE.Admission_Date BETWEEN  @DATE1  AND  @DATE2 

    AND isnull(PE.Prisoner_Type,0) =CASE WHEN @PrisonerType!='All' THEN @PrisonerType WHEN @PrisonerType ='All' THEN isnull(PE.Prisoner_Type,0) END

    AND isnull(PE.Location,0)=CASE WHEN @Location!='All' THEN @Location WHEN @Location ='All' THEN isnull(PE.Location,0) END

    AND isnull(PM.Caste_Code,0)=CASE WHEN @Caste!='All' THEN @Caste WHEN @Caste ='All' THEN isnull(PM.Caste_Code,0) END

    AND isnull(PM.Religion_Code,0)=CASE WHEN @Religion!='All' THEN @Religion WHEN @Religion ='All' THEN isnull(PM.Religion_Code,0) END

    AND isnull(PM.Occupation_Code,0)=CASE WHEN @Occupation!=0 THEN @Occupation WHEN @Occupation =0 THEN isnull(PM.Occupation_Code,0) END

    AND isnull(PM.Qualification,0)=CASE WHEN @Qualification!=0 THEN @Qualification WHEN @Qualification =0 THEN isnull(PM.Qualification,0) END

    AND isnull(PM.Tendency_Type,0)=CASE WHEN @Tendency!='All' THEN @Tendency WHEN @Tendency ='All' THEN isnull(PM.Tendency_Type,0) END

    AND isnull(PM.Risk_Type,0)=CASE WHEN @Risk!='All' THEN @Risk WHEN @Risk ='All' THEN isnull(PM.Risk_Type,0) END

    AND isnull(PM.Annual_Income_Code,0)=CASE WHEN @Annual!='All' THEN @Annual WHEN @Annual ='All' THEN isnull(PM.Annual_Income_Code,0) END

    AND isnull(PM.Marital_Status_Code,0)=CASE WHEN @Marital!='All' THEN @Marital WHEN @Marital ='All' THEN isnull(PM.Marital_Status_Code,0) END

    AND isnull(PM.Sex_Code,0)=CASE WHEN @Sex!='All' THEN @Sex WHEN @Sex ='All' THEN isnull(PM.Sex_Code,0) END

    AND isnull(PM.Nationality_Code,0)=CASE WHEN @Nationality!=0 THEN @Nationality WHEN @Nationality =0 THEN isnull(PM.Nationality_Code,0) END

    AND isnull(PM.Off_Code,0)=CASE WHEN @Offence!=0 THEN @Offence WHEN @Offence =0 THEN isnull(PM.Off_Code,0) END

    ORDER BY  PE.PID_No

我刚从动态sql的同事那里得到了一个解决方案,但我不想使用那个。我不喜欢将查询写为字符串,然后执行它..如果我必须这样做,那么为什么我使用存储过程?并且只有动态sql是我可以使用的选项或其他任何东西都可以完成。

0 个答案:

没有答案