在 WHERE 子句中重用字段值

Reuse field values in WHERE clause

如何优化以下 T-SQL 的性能,以便 heavyFunctionCall 函数只被调用一次。

在 table 变量、温度 table 秒、CTE 或其他变量中寻找最快的选项?

SQL:

select dbo.heavyFunctionCall(a, b, c) 
from T 
where dbo.heavyFunctionCall(a, b, c) > 10

这样做只会 运行 你的函数每行一次而不是两次:

SELECT * 
FROM (
  SELECT dbo.heavyFunctionCall(a, b, c) AS x
  FROM T) a
WHERE x > 10
declare proc tst (@x int)  -- set @x whatever you want.
                           -- the execution plan will be the same.
as 
begin
SELECT * 
FROM (
  SELECT dbo.heavyFunctionCall(a, b, c) AS result
  FROM T) resultx
WHERE result > @x
end

也许是这样:

select hFC.result
from T 
cross apply ( select dbo.heavyFunctionCall(T.a, T.b, T.c) result ) hFC
where hFC.result > 10