TSQL Is there a way to skip executing functions in an INNER JOIN section?

I have the following SQL statement:

FUNCTION F1
INNER JOIN TABLE T1
INNER JOIN FUNCTION F2 
INNER JOIN FUNCTION F3
INNER JOIN FUNCTION F4
...
INNER JOIN TABLE T2

In some situations, when it FUNCTION F1does not return any records, the wholes statement is executed for 12seconds.

Execution time FUNCTION F1(only) 4seconds. If I leave attached only FUNCTION F1, and the tables exectuin time again 4seconds.

So, SQL Server uses the rest of the functions that increase the execution time of the statement.

Why doesn't SQL Server skip execution because there is a clause INNER JOINand the first function returns nothing? Is there any way to fix this behavior?

+3
source share
1 answer

SQL- , . , .

- :

select *
into #f1
from function f1;

if not exists (select 1 from #f1)
begin
    -- stop processing here
end;

select *
into #f2
from function f2;

if not exists (select 1 from #f2)
begin
    -- stop processing here
end;

select *
into #f3
from function f3;

if not exists (select 1 from #f3)
begin
    -- stop processing here
end;

. . .

FUNCTION #F1
INNER JOIN TABLE T1
INNER JOIN #F2 
INNER JOIN #F3
INNER JOIN #F4
...
INNER JOIN TABLE T2

, , .

0

All Articles