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

  • Home
  • SEARCH
  • 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 215531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:28:45+00:00 2026-05-11T18:28:45+00:00

I have a query that looks like SELECT P.Column1, P.Column2, P.Column3, … ( SELECT

  • 0

I have a query that looks like

SELECT
 P.Column1,
 P.Column2,
 P.Column3,
 ...
 (
   SELECT
       A.ColumnX,
       A.ColumnY,
       ...
   FROM
      dbo.TableReturningFunc1(@StaticParam1, @StaticParam2) AS A
   WHERE
      A.Key = P.Key
   FOR XML AUTO, TYPE  
 ),
 (
   SELECT
       B.ColumnX,
       B.ColumnY,
       ...
   FROM
      dbo.TableReturningFunc2(@StaticParam1, @StaticParam2) AS B
   WHERE
      B.Key = P.Key
   FOR XML AUTO, TYPE  
 )
FROM
(
   <joined tables here>
) AS P
FOR XML AUTO,ROOT('ROOT') 

P has ~ 5000 rows
A and B ~ 4000 rows each

This query has a runtime performance of ~10+ minutes.

Changing it to this however:

SELECT
 P.Column1,
 P.Column2,
 P.Column3,
 ...
INTO #P

SELECT
 A.ColumnX,
 A.ColumnY,
 ...
INTO #A     
FROM
 dbo.TableReturningFunc1(@StaticParam1, @StaticParam2) AS A

SELECT
 B.ColumnX,
 B.ColumnY,
 ...
INTO #B     
FROM
 dbo.TableReturningFunc2(@StaticParam1, @StaticParam2) AS B


SELECT
 P.Column1,
 P.Column2,
 P.Column3,
 ...
 (
   SELECT
       A.ColumnX,
       A.ColumnY,
       ...
   FROM
      #A AS A
   WHERE
      A.Key = P.Key
   FOR XML AUTO, TYPE  
 ),
 (
   SELECT
       B.ColumnX,
       B.ColumnY,
       ...
   FROM
      #B AS B
   WHERE
      B.Key = P.Key
   FOR XML AUTO, TYPE  
 )
FROM #P AS P
FOR XML AUTO,ROOT('ROOT')      

Has a performance of ~4 seconds.

This makes not a lot of sense, as it would seem the cost to insert into a temp table and then do the join should be higher by default. My inclination is that SQL is doing the wrong type of “join” with the subquery, but maybe I’ve missed it, there’s no way to specify the join type to use with correlated subqueries.

Is there a way to achieve this without using #temp tables/@table variables via indexes and/or hints?

EDIT: Note that dbo.TableReturningFunc1 and dbo.TableReturningFunc2 are inline TVF’s, not multi-statement, or they are “parameterized” view statements.

  • 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-05-11T18:28:45+00:00Added an answer on May 11, 2026 at 6:28 pm

    Your procedures are being reevaluated for each row in P.

    What you do with the temp tables is in fact caching the resultset generated by the stored procedures, thus removing the need to reevaluate.

    Inserting into a temp table is fast because it does not generate redo / rollback.

    Joins are also fast, since having a stable resultset allows possibility to create a temporary index with an Eager Spool or a Worktable

    You can reuse the procedures without temp tables, using CTE‘s, but for this to be efficient, SQL Server needs to materialize the results of CTE.

    You may try to force it do this with using an ORDER BY inside a subquery:

    WITH    f1 AS
            (
            SELECT  TOP 1000000000
                    A.ColumnX,
                    A.ColumnY
            FROM    dbo.TableReturningFunc1(@StaticParam1, @StaticParam2) AS A
            ORDER BY
                    A.key
            ),
            f2 AS
            (
            SELECT  TOP 1000000000
                    B.ColumnX,
                    B.ColumnY,
            FROM    dbo.TableReturningFunc2(@StaticParam1, @StaticParam2) AS B  
            ORDER BY
                    B.Key
            )
    SELECT  …
    

    , which may result in Eager Spool generated by the optimizer.

    However, this is far from being guaranteed.

    The guaranteed way is to add an OPTION (USE PLAN) to your query and wrap the correspondind CTE into the Spool clause.

    See this entry in my blog on how to do that:

    • Generating XML in subqueries

    This is hard to maintain, since you will need to rewrite your plan each time you rewrite the query, but this works well and is quite efficient.

    Using the temp tables will be much easier, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 221k
  • Answers 221k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Whilst the click handler is bound to the anchor, the… May 13, 2026 at 12:08 am
  • Editorial Team
    Editorial Team added an answer I see two things wrong with the code you posted:… May 13, 2026 at 12:08 am
  • Editorial Team
    Editorial Team added an answer Are you sure that ${ORALOGIN} and ${LOGFILE} are valid ENV… May 13, 2026 at 12:08 am

Related Questions

I have a query that looks like SELECT P.Column1, P.Column2, P.Column3, ... ( SELECT
In SQL one can write a query that searches for a name of a
I'm working on adding globalization to my product cataloge and I have made it
I'm seeing performance problems with retrieving multiple instances of objects that have many relationships
I've got a strange problem with SQL Server 2000, and I just can't think

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.