Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7967503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:56:09+00:00 2026-06-04T06:56:09+00:00

I have several stored procedures that use an outer apply. The query inside the

  • 0

I have several stored procedures that use an outer apply. The query inside the outer apply is always the same, so I could build a common table valued function which gives me the obvious benefit of code re-use, but I’m wondering if there are performance implications either way. Is there a hit I take if I call a function?

For example:

SELECT
    m.[ID],
    m.[MyField],
    o.[OtherField]
FROM
    [MyTable] m
OUTER Apply
(
    fMyFunction(m.[ID])
)

VS

SELECT
    mt.[ID],
    mt.[MyField],
    o.[OtherField]
FROM
    [MyTable] mt
OUTER Apply
(
    SELECT TOP 1
        ot.[OtherField]
    FROM
        [OtherTable] ot 
    WHERE
        ot.[ID] = m.[ID]
) o
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:56:10+00:00Added an answer on June 4, 2026 at 6:56 am

    It depends of function type:

    1. If the function is an inline table-valued function then this function will be considered to be a "parameterized" view and SQL Server can do some optimization work.

    2. If the function is multi-step table-valued function then is hard for SQL Server to optimize the statement and the output from SET STATISTICS IO will be misleading.

    For the next test I used the AdventureWorks2008 (you can download this database from CodePlex). In this sample database you may find an inline table-valued function named [Sales].[ufnGetCheapestProduct]:

    ALTER FUNCTION [Sales].[ufnGetCheapestProduct](@ProductID INT)
    RETURNS TABLE
    AS
    RETURN
        SELECT   dt.ProductID
                ,dt.UnitPrice
        FROM
        (
            SELECT   d.SalesOrderDetailID
                    ,d.UnitPrice
                    ,d.ProductID  
                    ,ROW_NUMBER() OVER(PARTITION BY d.ProductID ORDER BY d.UnitPrice ASC, d.SalesOrderDetailID) RowNumber
            FROM    Sales.SalesOrderDetail d
            WHERE   d.ProductID = @ProductID
        ) dt
        WHERE   dt.RowNumber = 1
    

    I created a new function named [Sales].[ufnGetCheapestProductMultiStep]. This function is a multi-step table-valued function:

    CREATE FUNCTION [Sales].[ufnGetCheapestProductMultiStep](@ProductID INT)
    RETURNS @Results TABLE (ProductID INT PRIMARY KEY, UnitPrice MONEY NOT NULL)
    AS
    BEGIN
        INSERT  @Results(ProductID, UnitPrice)
        SELECT   dt.ProductID
                ,dt.UnitPrice
        FROM
        (
            SELECT   d.SalesOrderDetailID
                    ,d.UnitPrice
                    ,d.ProductID  
                    ,ROW_NUMBER() OVER(PARTITION BY d.ProductID ORDER BY d.UnitPrice ASC, d.SalesOrderDetailID) RowNumber
            FROM    Sales.SalesOrderDetail d
            WHERE   d.ProductID = @ProductID
        ) dt
        WHERE   dt.RowNumber = 1;
    
        RETURN;
    END
    

    Now, we can run the next tests:

    --Test 1
    SELECT  p.ProductID, p.Name, oa1.*
    FROM    Production.Product p
    OUTER APPLY 
    (
        SELECT   dt.ProductID
                ,dt.UnitPrice
        FROM
        (
            SELECT   d.SalesOrderDetailID
                    ,d.UnitPrice
                    ,d.ProductID  
                    ,ROW_NUMBER() OVER(PARTITION BY d.ProductID ORDER BY d.UnitPrice ASC, d.SalesOrderDetailID) RowNumber
            FROM    Sales.SalesOrderDetail d
            WHERE   d.ProductID = p.ProductID
        ) dt
        WHERE   dt.RowNumber = 1
    ) oa1
    
    --Test 2
    SELECT  p.ProductID, p.Name, oa2.*
    FROM    Production.Product p
    OUTER APPLY [Sales].[ufnGetCheapestProduct](p.ProductID) oa2
    
    --Test 3
    SELECT  p.ProductID, p.Name, oa3.*
    FROM    Production.Product p
    OUTER APPLY [Sales].[ufnGetCheapestProductMultiStep](p.ProductID) oa3
    

    And this is the output from SQL Profiler:
    enter image description here

    Conclusion: you can see that using a query or an inline table-valued function with OUTER APPLY will give you the same performance (logical reads). Plus: the multi-step table-valued functions are (usually) more expensive.

    Note: I do not recommend using SET STATISTICS IO to measure the IO for scalar and multi-step table valued functions because the results can be wrong. For example, for these tests the output from SET STATISTICS IO ON will be:

    --Test 1
    Table 'SalesOrderDetail'. Scan count 504, logical reads 1513, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    Table 'Product'. Scan count 1, logical reads 5, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    
    --Test 2
    Table 'SalesOrderDetail'. Scan count 504, logical reads 1513, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    Table 'Product'. Scan count 1, logical reads 5, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    
    --Test 3
    Table '#064EAD61'. Scan count 504, logical reads 1008 /*WRONG*/, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    Table 'Product'. Scan count 1, logical reads 5, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several stored procedures that all use the same set of parameters. Is
I have a rather huge query that is needed in several stored procedures, and
so I have written several stored procedures that act on individual rows of data
There appears to be several stored procedures that were renamed with sp_rename, causing the
I have been running up a stored procedure that runs several separate update and
Say I have a stored procedure that returns data from a SELECT query. I
I have a c# application that interfaces with the database only through stored procedures.
I have an ETL process that involves a stored procedure that makes heavy use
I'm finding that there are several points in the stored procedures that I'm creating
We have a helper class that we use to call stored procs on SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.