2 of 3
deleted 1 character in body
ypercubeᵀᴹ
  • 97.9k
  • 13
  • 214
  • 305

I read that this is not good practice because function is called 'zilion' times and it have bad impact on performance.

While CROSS APPLY can be useful in some cases, I don't expect any difference in performance between calling the function in WHERE or CROSS APPLY in the specific case. If the table has a million rows (and columns C and D possibly a million different values), a million times the function will be called. How can it be other wise?

I tried to rewrite it with CROSS APPLY.

Here's how:

SELECT
    t.A,
    t.B,
    ca.Fc,
    ca.Fd,
    dbo.Func(t.E) AS Fe
    t.F,
FROM abcdef AS t
  CROSS APPLY 
    ( SELECT
          dbo.Func(t.C) AS Fc, 
          dbo.Func(t.D) AS Fd
    ) AS ca
WHERE 0 = ca.Fc + ca.Fd ;

or:

SELECT
...
FROM abcdef AS t
  CROSS APPLY 
    ( SELECT
          dbo.Func(t.C) AS Fc, 
          dbo.Func(t.D) AS Fd
      FROM (SELECT NULL) AS dummy
      WHERE 0 = dbo.Func(t.C) + dbo.Func(t.d) 
    ) AS ca ;

Again, I don't think will have any effects on efficiency.

ypercubeᵀᴹ
  • 97.9k
  • 13
  • 214
  • 305