patternsqlModerate
Estimated versus actual query plan with function calls
Viewed 0 times
callsactualversuswithqueryfunctionplanestimated
Problem
I have this query on SQL server, a merge replication query:
The estimated query plan includes information about 3 queries:
The actual query plan includes only this information:
Nothing about the functions. Why is the function information missing in the actual plan?
I tried these options:
Which created an actual plan, but it was missing parts 2 and 3 same as when I used the actual query plan option in Management Studio.
If for instance I was to use Profiler to capture the information about the function calls what events would I select?
Didn't find an answer specifically related to the query plans, but I profiled SP:StmtStarting and SP:StmtCompleted and it showed the function calls.
SELECT DISTINCT
b.tablenick,
b.rowguid,
c.generation,
sys.fn_MSgeneration_downloadonly
(
c.generation,
c.tablenick
)
FROM #belong b
LEFT OUTER JOIN dbo.MSmerge_contents c ON
c.tablenick = b.tablenick
AND c.rowguid = b.rowguid;The estimated query plan includes information about 3 queries:
- The query above
- The function call to fn_MSgeneration_downloadonly
- The function call to fn_MSArticle_has_downloadonly_property
The actual query plan includes only this information:
- The query above
Nothing about the functions. Why is the function information missing in the actual plan?
I tried these options:
SET STATISTICS PROFILE ON
SET STATISTICS XML ONWhich created an actual plan, but it was missing parts 2 and 3 same as when I used the actual query plan option in Management Studio.
If for instance I was to use Profiler to capture the information about the function calls what events would I select?
Didn't find an answer specifically related to the query plans, but I profiled SP:StmtStarting and SP:StmtCompleted and it showed the function calls.
Solution
And nothing about the functions. Why is the function information
missing in the actual plan?
This is by design, for performance reasons.
Functions that contain
In the context of your question, this would result in full
Example
Consider the T-SQL scalar function below, created in the AdventureWorks sample database, which returns the name of a product given its ID:
Pre-execution plan
A pre-execution plan (estimated plan in SSMS) shows plan information for the parent statement and nested function calls:
SSMS output:
The same XML viewed in SQL Sentry Plan Explorer shows the nested nature of the calls more clearly:
Post-execution output
SSMS shows details for only the main query when post-execution plan output is requested:
The performance impact of doing otherwise can be shown using the Showplan XML Statistics Profile Event Class in SQL Server Profiler, using a query that calls the function multiple times (once per input row):
Profiler output:
There are five separate post-execution plans for the function executions, and one for the parent query. The five function plans look like this in the profiler lower pane:
The parent query plan is:
Executing the query without the
The situation for triggers is reversed. These do not show any pre-execution plan information, but do include a post-execution plan. This reflects the set-based nature of triggers; each is fired once for all rows affected, rather than once per row.
missing in the actual plan?
This is by design, for performance reasons.
Functions that contain
BEGIN and END in the definition create a new T-SQL stack frame for each input row. Put another way, the function body is executed separately for each input row. This single fact explains most performance problems associated with T-SQL scalar and multi-statement functions (note that in-line table valued functions do not use the BEGIN...END syntax).In the context of your question, this would result in full
SHOWPLAN output for each row. XML plan output is quite verbose and expensive to produce, so producing full output for every row would be a bad idea in general terms.Example
Consider the T-SQL scalar function below, created in the AdventureWorks sample database, which returns the name of a product given its ID:
CREATE FUNCTION dbo.DumbNameLookup
(
@ProductID integer
)
RETURNS dbo.Name
AS
BEGIN
RETURN
(
SELECT
p.Name
FROM Production.Product AS p
WHERE
p.ProductID = @ProductID
);
END;Pre-execution plan
A pre-execution plan (estimated plan in SSMS) shows plan information for the parent statement and nested function calls:
-- Pre-execution plan shows main query and nested function call
SET SHOWPLAN_XML ON;
GO
SELECT dbo.DumbNameLookup(1);
GO
SET SHOWPLAN_XML OFF;SSMS output:
The same XML viewed in SQL Sentry Plan Explorer shows the nested nature of the calls more clearly:
Post-execution output
SSMS shows details for only the main query when post-execution plan output is requested:
-- Post-execution plan shows main query only
SET STATISTICS XML ON;
SELECT dbo.DumbNameLookup(1);
SET STATISTICS XML OFF;The performance impact of doing otherwise can be shown using the Showplan XML Statistics Profile Event Class in SQL Server Profiler, using a query that calls the function multiple times (once per input row):
SELECT TOP (5)
p.ProductID,
dbo.DumbNameLookup(p.ProductID)
FROM Production.Product AS p;Profiler output:
There are five separate post-execution plans for the function executions, and one for the parent query. The five function plans look like this in the profiler lower pane:
The parent query plan is:
Executing the query without the
TOP (5) clause results in a full execution plan for each of the 504 rows in the Product table. You can probably see how this would quickly get out of hand with larger tables.The situation for triggers is reversed. These do not show any pre-execution plan information, but do include a post-execution plan. This reflects the set-based nature of triggers; each is fired once for all rows affected, rather than once per row.
Code Snippets
CREATE FUNCTION dbo.DumbNameLookup
(
@ProductID integer
)
RETURNS dbo.Name
AS
BEGIN
RETURN
(
SELECT
p.Name
FROM Production.Product AS p
WHERE
p.ProductID = @ProductID
);
END;-- Pre-execution plan shows main query and nested function call
SET SHOWPLAN_XML ON;
GO
SELECT dbo.DumbNameLookup(1);
GO
SET SHOWPLAN_XML OFF;-- Post-execution plan shows main query only
SET STATISTICS XML ON;
SELECT dbo.DumbNameLookup(1);
SET STATISTICS XML OFF;SELECT TOP (5)
p.ProductID,
dbo.DumbNameLookup(p.ProductID)
FROM Production.Product AS p;Context
StackExchange Database Administrators Q#29742, answer score: 17
Revisions (0)
No revisions yet.