sql查询超时... case / if / what to do

时间:2013-09-18 12:35:55

标签: sql-server tsql

我有以下sql server sproc:

PROCEDURE [dbo].[GetSoftwareProgramsGrid]
    @SoftwareTitle varchar(1000)='All',
    @CategoryID varchar(100)='All',
    @ManufacturerID varchar(50)='All',
    @ModelID int=0, -- 0 means all
    @AssetID int=0, -- 0 means all
    @AssetStatus int=0, --0 is active, 1 is inactive, and 2 is all
    @Status int=0, --0 is active, 1 is inactive, and 2 is all
    @Type varchar(100)='All',
    @Site varchar(100)='All',
    @Department varchar(100)='All',
    @Manager varchar(100)='All',
    @Employee varchar(100)='All',
    @SortExpression varchar(100)='Software',
    @SortOrder int=0
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

SELECT
*
FROM
(
    SELECT DISTINCT
        Program AS Software
    FROM
        AssetProgram ap
    LEFT JOIN
        AssetAssignment aa
    ON
        aa.AssetID = ap.AssetID
    LEFT JOIN
        [MyLinkedServer].MyDB.dbo.Login l
    ON
        l.LoginID = aa.LoginID
    LEFT JOIN
        Asset a
    ON
        a.AssetID = ap.AssetID
    INNER JOIN Model m
    ON
        a.ModelID = m.ModelID
    INNER JOIN
        Category c
    ON
        c.CategoryID = m.CategoryID
    INNER JOIN Manufacturer ma 
    ON
        ma.ManufacturerID = m.ManufacturerID
    WHERE
        (
            --Software filters
            (ap.Program = @SoftwareTitle OR @SoftwareTitle='All')

            --Asset filters
            AND (c.CategoryID = @CategoryID OR @CategoryID='All')   --filter category
            AND (ma.ManufacturerID = @ManufacturerID OR @ManufacturerID='All') --filter manufacturer
            AND (m.ModelID = @ModelID OR @ModelID = 0)  --filter model
            AND (a.AssetID = @AssetID OR @AssetID = 0)  --filter by asset name (the actual asset id)
            AND (((a.Inactive=@AssetStatus) OR (@AssetStatus=2)))
            AND (aa.Inactive=0) 
            AND (ap.Inactive=0)

            --Employee filters
            /*AND ((l.Inactive=@Status) OR (@Status=2)) --status of employee 2 is all, 1 is inactive, and 0 is active

            AND (@Type='All' OR (@Type='Contractor' AND l.IsContractor=1) OR (@Type='Regular' AND l.IsContractor=0))    --contractor or regular employee
            AND (@Site='All' OR @Site=l.ClientID)   --the site
            AND (@Department='All' OR @Department=l.FunctionalGroupID)  --the department
            AND ((l.Manager = @Manager OR l.FullName=@Manager) OR @Manager='All')   --the manager
            AND (l.FullName = @Employee OR @Employee='All') --the employee
            */
        )) ttt
ORDER BY    
            CASE WHEN @SortExpression='Software' AND @SortOrder=0 THEN Software END ASC,
            CASE WHEN @SortExpression='Software' AND @SortOrder=1 THEN Software END DESC

由于我们的设置,此查询必须包含链接服务器。只要我注释掉我的员工参数,查询运行正常并且很快,即本节:

--Employee filters
                /*AND ((l.Inactive=@Status) OR (@Status=2)) --status of employee 2 is all, 1 is inactive, and 0 is active

                AND (@Type='All' OR (@Type='Contractor' AND l.IsContractor=1) OR (@Type='Regular' AND l.IsContractor=0))    --contractor or regular employee
                AND (@Site='All' OR @Site=l.ClientID)   --the site
                AND (@Department='All' OR @Department=l.FunctionalGroupID)  --the department
                AND ((l.Manager = @Manager OR l.FullName=@Manager) OR @Manager='All')   --the manager
                AND (l.FullName = @Employee OR @Employee='All') --the employee
                */

我甚至将该部分的第一行带入,例如只有这一行:

AND ((l.Inactive=@Status) OR (@Status=2))

整个sproc挂起(超时)....我已经正确索引了我的表格,我甚至在我的链接表中的Inactive字段上有一个索引...如果我在上面使用相同的行然后说:

AND (l.Inactive=0)

它运行正常,因此OR条件导致它(布尔值)。但是,我需要这个条件作为传递需要满足的参数。我有什么其他选择,我是否必须IF BEGIN...使用所有这些参数?看起来很麻烦...对于任何人的信息,AssetProgram表总共有5万行,所以这并不算太多。

1 个答案:

答案 0 :(得分:0)

是的,你的where子句是疯了,特别是所有的“OR”

尝试根据@Status变量将“用户”加载到临时表(或var表)中(如果为1,则仅加载非活动状态,如果为0,则仅加载活动状态,依此类推......)并在连接上使用临时表,这将大大减少比较次数。

相关问题