使用WHERE IN子查询提高Update的性能

时间:2014-11-13 15:24:27

标签: sql sql-server tsql

我正在处理一个已经存在的sql语句(验证性能),我发现两个更新句子非常相似,正在使用WHERE IN(子查询)子句,我认为它们非常慢,我请求你的帮助找到两种句子的另一种方式,更快地完成工作。

这是“模拟”代码

    UPDATE Table1 SET IdUser = @NewUserId
    WHERE IdTask IN
    (
    SELECT T.IdTask
    FROM Business N
    JOIN Process P ON N.IdBusiness = P.CodBusiness
    JOIN Proyect Pr ON P.CodProyect = Pr.IdProyect
    JOIN Etap E ON E.CodProyect = Pr.IdProyect
    JOIN Task T ON T.CodEtap = E.IdEtap
    WHERE IdBusiness = @IdBusiness AND
    T.Flag = 0
    )

    UPDATE Table2 SET Flag = 1
    WHERE CodTask IN
    (
    SELECT T.IdTask
    FROM Business N
    JOIN Process P ON N.IdBusiness = P.CodBusiness
    JOIN Proyect Pr ON P.CodProyect = Pr.IdProyect
    JOIN Etap E ON E.CodProyect = Pr.IdProyect
    JOIN Task T ON T.CodEtap = E.IdEtap
    JOIN TaskAdvan TA ON TA.CodTask = T.IdTask
    WHERE IdBusiness = @IdBusiness AND
    T.Flag = 0
    )

非常感谢

2 个答案:

答案 0 :(得分:0)

如果您认为子查询的处理成本很高,则可以将中间结果存储在临时表或表变量中。然后,您可以在后续更新中使用它。

DECLARE @filter TABLE(IdTask INT PRIMARY KEY)
INSERT INTO @filter
SELECT T.IdTask
FROM Business N
JOIN Process P ON N.IdBusiness = P.CodBusiness
JOIN Proyect Pr ON P.CodProyect = Pr.IdProyect
JOIN Etap E ON E.CodProyect = Pr.IdProyect
JOIN Task T ON T.CodEtap = E.IdEtap
WHERE IdBusiness = @IdBusiness AND
T.Flag = 0

UPDATE t1 SET IdUser = @NewUserId
FROM Table1 t1 INNER JOIN @filter f ON f.IdTask = t1.IdTask

UPDATE t2 SET Flag = 1
FROM Table2 t2 INNER JOIN @filter f ON f.IdTask = t2.CodTask

答案 1 :(得分:0)

--UPDATE using Inner JOIN

    UPDATE  A
    SET     A.IdUser = @NewUserId
    FROM    Table1 AS A
            JOIN ( SELECT   T.IdTask
                   FROM     Business N
                            JOIN Process P
                            ON N.IdBusiness = P.CodBusiness
                            JOIN Proyect Pr
                            ON P.CodProyect = Pr.IdProyect
                            JOIN Etap E
                            ON E.CodProyect = Pr.IdProyect
                            JOIN Task T
                            ON T.CodEtap = E.IdEtap
                   WHERE    IdBusiness = @IdBusiness AND
                            T.Flag = 0
                 ) B
            ON A.IdTask = B.IdTask
------------------------------------------------------------------------
    UPDATE  A
    SET     A.Flag = 1
    FROM    Table2 AS A
            JOIN ( SELECT   T.IdTask
                   FROM     Business N
                            JOIN Process P
                            ON N.IdBusiness = P.CodBusiness
                            JOIN Proyect Pr
                            ON P.CodProyect = Pr.IdProyect
                            JOIN Etap E
                            ON E.CodProyect = Pr.IdProyect
                            JOIN Task T
                            ON T.CodEtap = E.IdEtap
                            JOIN TaskAdvan TA
                            ON TA.CodTask = T.IdTask
                   WHERE    IdBusiness = @IdBusiness AND
                            T.Flag = 0
                 ) AS B
            ON A.CodTask = B.IdTask