SQL-Server-First-Responder-Kit icon indicating copy to clipboard operation
SQL-Server-First-Responder-Kit copied to clipboard

Investigate issue with BlitzCache and Table-valued function joins

Open BlitzErik opened this issue 10 years ago • 1 comments

Script to repro:

USE [StackOverflow]
GO

CREATE FUNCTION [dbo].[BC_MSTVF_1] ( @uid INT )
RETURNS @Out TABLE (ID BIGINT, [BadgeCount] BIGINT )
    WITH SCHEMABINDING
AS
    BEGIN
        INSERT  INTO @Out
                (
                  [ID],
                  [BadgeCount]
                )
        SELECT  [b].[UserId], COUNT_BIG(*) AS [BadgeCount]
        FROM    [dbo].[Badges] AS [b]
        WHERE   [b].[UserId] = @uid
        GROUP BY [b].[UserId];
        RETURN;
    END;
GO

SELECT
    TOP 10000 *
FROM
    [dbo].[Users] AS [u]
    LEFT JOIN [dbo].[BC_MSTVF_1] (0) AS [bm] 
    ON [u].[Id] = [bm].[ID]

image

XML shows: <RelOp AvgRowSize="9" EstimateCPU="0.000100157" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row" EstimateRows="100" LogicalOp="Table-valued function" NodeId="1" Parallel="false" PhysicalOp="Table-valued function" EstimatedTotalSubtreeCost="0.000100157">

BlitzCache is looking for: relop.exist('/p:RelOp[contains(@LogicalOp, "Join")]/*/p:RelOp[(@LogicalOp[.="Table-valued function"])]') AS tvf_join

There are no other references to TVFs in the XML.

This doesn't seem to manifest at all with inline table valued functions.

USE [StackOverflow]
GO

CREATE FUNCTION [dbo].[BC_ITVF_1] ( @uid INT )
RETURNS TABLE
    WITH SCHEMABINDING
AS
RETURN
    SELECT  [b].[UserId], COUNT_BIG(*) AS [BadgeCount]
    FROM    [dbo].[Badges] AS [b]
    WHERE   [b].[UserId] = @uid
    GROUP BY [b].[UserId];
GO

SELECT
    TOP 10000 *
FROM
    [dbo].[Users] AS [u]
    LEFT JOIN [dbo].[BC_ITVF_1] (0) AS [bm] 
    ON [u].[Id] = [bm].[UserId]

FWIW, I've seen this work correctly with some background/system queries, but I'm not sure what's different there.Here's an example. Plan XML is attached.

image

Function Join.txt

BlitzErik avatar Jun 29 '16 00:06 BlitzErik

This isn't going anywhere.

BlitzErik avatar Nov 19 '16 17:11 BlitzErik