2

How do I optimize the performance of the following T-SQL so heavyFunctionCall function will get called just once.

Looking for the fastest option among table variables, temp tables, CTEsor something else?

SQL:

select dbo.heavyFunctionCall(a, b, c) 
from T 
where dbo.heavyFunctionCall(a, b, c) > 10
gotqn
  • 42,737
  • 46
  • 157
  • 243
user1514042
  • 1,899
  • 7
  • 31
  • 57

3 Answers3

3

Doing this would only run your function once on every row instead of twice:

SELECT * 
FROM (
  SELECT dbo.heavyFunctionCall(a, b, c) AS x
  FROM T) a
WHERE x > 10
Dave Sexton
  • 10,768
  • 3
  • 42
  • 56
1
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
Dudi Konfino
  • 1,126
  • 2
  • 13
  • 24
1

maybe this:

select hFC.result
from T 
cross apply ( select dbo.heavyFunctionCall(T.a, T.b, T.c) result ) hFC
where hFC.result > 10
A ツ
  • 1,267
  • 2
  • 9
  • 14