Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your procedures are being reevaluated for each row in <code>P</code>.</p> <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.</p> <p>Inserting into a temp table is fast because it does not generate <code>redo</code> / <code>rollback</code>.</p> <p>Joins are also fast, since having a stable resultset allows possibility to create a temporary index with an <code>Eager Spool</code> or a <code>Worktable</code></p> <p>You can reuse the procedures without temp tables, using <code>CTE</code>'s, but for this to be efficient, <code>SQL Server</code> needs to materialize the results of <code>CTE</code>.</p> <p>You may <em>try</em> to force it do this with using an <code>ORDER BY</code> inside a subquery:</p> <pre><code>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 … </code></pre> <p>, which may result in <code>Eager Spool</code> generated by the optimizer.</p> <p>However, this is far from being guaranteed.</p> <p>The guaranteed way is to add an <code>OPTION (USE PLAN)</code> to your query and wrap the correspondind <code>CTE</code> into the <code>Spool</code> clause.</p> <p>See this entry in my blog on how to do that:</p> <ul> <li><a href="http://explainextended.com/2009/05/28/generating-xml-in-subqueries/" rel="noreferrer"><strong>Generating XML in subqueries</strong></a></li> </ul> <p>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.</p> <p>Using the temp tables will be much easier, though.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload