例子:
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
对比
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 这么快。它只是使用更有效的算法吗?