I have the following functions:
FunctionA - returns Object ID and Detail ID
FunctionB - returns Detail ID and Detail Name
The following query is used to extract Object ID
, Detail ID
and Detail Name
:
SELECT FunctionA.ID
,FunctionA.DetailID
,FunctionB.DetailName
FROM FunctionA (...)
INNER JOIN FunctionB (...)
ON FunctionA.DetailID = FunctionB.DetailID
The screenshot below display its execution plan cost (it takes 32 seconds):
In the following query I have changed the query to use cross apply
instead inner join
and made the FunctionB
to return Detail Name
for specific Detail ID
:
SELECT FunctionA.ID
,FunctionA.DetailID
,FunctionB.DetailName
FROM FunctionA (...)
CROSS APPLY FunctionB (FunctionA.DetailID)
ON FunctionA.DetailID = FunctionB.DetailID
The screenshot below display its execution plan cost (it takes 3 seconds):
In the first case FunctionB
returns all pairs Detail ID
and Detail Name
and normally it takes a lot of time.
In the second case, FunctionB
is executed faster because it returns only Detail Name
for specific Detail ID
, but it is executed for each Object ID
.
Why the first case is so slower? Is SQL Server executing the FunctionB
in the second case for each row, or it is caching results and avoids execution of the function
with same parameter?
SQL-SERVER-2012
- check the questions tags – Noelyn