11

I have this query on SQL server, a merge replication query:

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:

  1. The query above
  2. The function call to fn_MSgeneration_downloadonly
  3. The function call to fn_MSArticle_has_downloadonly_property

The actual query plan includes only this information:

  1. 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 ON

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.

17

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 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:

SSMS pre-execution plan

The same XML viewed in SQL Sentry Plan Explorer shows the nested nature of the calls more clearly:

PE pre-execution plan

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;

SSMS post-execution

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:

Trace 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:

Function plans

The parent query plan is:

Parent plan

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.

  • @PaulWhite is there any good reason that trigger plans are not shown when requesting the estimated execution plan? That seems like a useful missing feature. I might create a connect item for it. – usr Apr 6 '15 at 10:56
  • @usr - Maybe because the actual cached plan that is selected can vary dependant on the actual number of rows as described here? technet.microsoft.com/en-us/library/… – Martin Smith Apr 6 '15 at 12:41
  • @MartinSmith that might be a reason. Recently a connect item for execution plans of check and fk constraints was marked as completed so I was hopeful they would do the same thing with triggers. – usr Apr 6 '15 at 13:24
  • @usr - This one here? 3 months? That must be a record turn around for a new feature request! – Martin Smith Apr 6 '15 at 19:01
  • @MartinSmith yes, that one. It was "fixed" a 1-2 ago. I really hope I don't have to query the query store. I had hoped to click a button in SSMS. Actually, I was surprised to see any change at all to a part of the engine that wasn't touched in years. But maybe there was none. – usr Apr 6 '15 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.