0

我有以下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))

整个存储过程挂起(超时)......我已经正确地索引了我的表,我什至Inactive在我的链接表中的字段上有一个索引......如果我在上面使用同一行并说:

AND (l.Inactive=0)

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

4

1 回答 1

0

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

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

于 2013-09-18T12:58:08.257 回答