当CROSS APPLY匹配所有行

时间:2018-01-26 15:07:48

标签: sql sql-server cross-apply outer-apply

示例:

UPDATE t
SET Amount = applied.MaxAmount
FROM @test t
CROSS APPLY
(
    SELECT MAX(t2.Amount) AS MaxAmount
    FROM @test t2
    WHERE t2.Id = t.ForeignId
) AS applied

VS

UPDATE t
SET Amount = applied.MaxAmount
FROM @test t
OUTER APPLY
(
    SELECT MAX(t2.Amount) AS MaxAmount
    FROM @test t2
    WHERE t2.Id = t.ForeignId
) AS applied

我的理解是这两个脚本将完成相同的事情,因为MAX()函数将始终返回结果,即使该结果为NULL。但是当我将它应用于大型数据集时,OUTER APPLY比CROSS APPLY慢得多。

我想知道为什么CROSS APPLY要快得多。它只是使用更有效的算法吗?

0 个答案:

没有答案