<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www2.sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Kalen Delaney : temp tables</title><link>http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/temp+tables/default.aspx</link><description>Tags: temp tables</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Geek City: Space Used By Worktables</title><link>http://www2.sqlblog.com/blogs/kalen_delaney/archive/2008/11/26/space-used-by-worktables.aspx</link><pubDate>Wed, 26 Nov 2008 17:08:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10129</guid><dc:creator>Kalen Delaney</dc:creator><slash:comments>6</slash:comments><comments>http://www2.sqlblog.com/blogs/kalen_delaney/comments/10129.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/kalen_delaney/commentrss.aspx?PostID=10129</wfw:commentRss><description>&lt;P&gt;Today, a reader asked me the following:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;"How can I find the amount of space occupied by a worktable?. Using SET STATISTICS IO ON, I can only see the number of reads using the worktable, not the amount of space taken."&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What is a worktable? 
&lt;P&gt;I always like to think of it as a temp table that SQL Server builds without being asked. While preparing to write this post, I decided to see if I could find a formal definition. Books Online for SQL Server 2005 gives the following definition in the topic "Worktables": 
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Worktables are generated for certain GROUP BY, ORDER BY, or UNION queries. For example, if an ORDER BY clause references columns that are not covered by any indexes, the relational engine may need to generate a worktable to sort the result set into the order requested. Worktables are also sometimes used as spools that temporarily hold the result of executing a part of a query plan.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then I went to one of my favorite whitepapers, &lt;A href="http://www.microsoft.com/technet/prodtechnol/sql/2005/workingwithtempdb.mspx" target=_blank&gt;"Working with tempdb in SQL Server 2005",&lt;/A&gt;&amp;nbsp; which I strongly suggest you take a look at, if you're at all interested in keeping track of your &lt;EM&gt;tempdb&lt;/EM&gt; database.&amp;nbsp; It had a slightly different definition:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Work tables are temporary objects and are used to store results for query spool, LOB variables, and cursors.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So there is some overlap, in that both definitions mention spools. 
&lt;P&gt;Prior to SQL Server 2005, the best we could do was watch the STATISTICS IO value, and look at the page reads for any worktables created in the query, but, as my reader mentions, those values show us the number of reads, not the total size of the tables . There were/are some Performance Monitor counters that let us see how many worktables were created, but they don't mention the size. 
&lt;P&gt;SQL Server 2005 provides us a couple of DMVs that can be helpful. 
&lt;P&gt;The first, &lt;EM&gt;sys.dm_db_file_space_usage&lt;/EM&gt;, has a name that seems like it will provide information about all your databases, but it turns out it just provides information for tempdb. I usually use this view to keep track of the version store space, but it also tells me how much space is used for user objects (explicit temp tables) and internal objects (which include worktables). 
&lt;P&gt;The second, &lt;EM&gt;sys.dm_db_session_space_usage&lt;/EM&gt;, reports information for each session, so you can filter it by the session_id you are interested in . For you current session, you can look at @@spid: 
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;SELECT * FROM sys.dm_db_session_space_usage&lt;BR&gt;WHERE session_id = @@spid;&lt;/FONT&gt; 
&lt;P&gt;During testing, the above can be useful, to look at the values before you run a test, and then look at the values afterwards, and compute the difference. This still doesn't give you the exact size of your worktables, but it can give you some ideas. In fact, the above mentioned whitepaper states that there is no way to get the number of pages used by any specific internal object in tempdb. 
&lt;P&gt;The whitepaper gives you code to create a table called &lt;EM&gt;tempdb_space_usage&lt;/EM&gt; and a stored procedure called &lt;EM&gt;sp_sampleTempDbSpaceUsage&lt;/EM&gt; to populate the table. It also provides half a dozen queries to examine the data collected. 
&lt;P&gt;You should be able to get a much better handle on what is using your tempdb space by following the guidelines in the whitepaper and running some of the provided queries. 
&lt;P&gt;Have fun! 
&lt;P&gt;&lt;FONT color=#ff00ff size=4&gt;~Kalen&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=10129" width="1" height="1"&gt;</description><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/DMVs/default.aspx">DMVs</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/internals/default.aspx">internals</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/storage/default.aspx">storage</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/temp+tables/default.aspx">temp tables</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/tempdb/default.aspx">tempdb</category></item><item><title>Did You Know? Estimated vs Actual Plans</title><link>http://www2.sqlblog.com/blogs/kalen_delaney/archive/2007/07/30/did-you-know-estimated-vs-actual-plans.aspx</link><pubDate>Mon, 30 Jul 2007 18:41:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:1999</guid><dc:creator>Kalen Delaney</dc:creator><slash:comments>5</slash:comments><comments>http://www2.sqlblog.com/blogs/kalen_delaney/comments/1999.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/kalen_delaney/commentrss.aspx?PostID=1999</wfw:commentRss><description>&lt;P&gt;In my previous post, I mentioned that it is important to understand the difference between estimated and actual query plans, so I decided to go into a few more details regarding the differences. &lt;/P&gt;
&lt;P&gt;Optimization takes place&amp;nbsp;before execution, so in one sense, any query plan is an estimated plan. But when you request an actual plan, SQL Server will actually execute each statement as it displays the plan, so&amp;nbsp;the plan&amp;nbsp;for subsequent statements can change after the earlier statements are executed.&amp;nbsp; In addition, SQL Server adds additional information to the actual plan, after the execution takes place. As mentioned last time, the actual plan includes the number of processors used to execute the query. It will also include the number of rows returned from each step of the plan as it is executed, and the number of times each step is executed.&amp;nbsp; If you are executing a plan that has already been cached and is now being reused, the actual plan will include both the parameter values the plan was originally compiled with and the parameter values used during execution. &lt;/P&gt;
&lt;P&gt;You can request that SQL Server display ESTIMATED plans without executing the query with any of the following options:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;SET SHOWPLAN_TEXT ON &lt;/P&gt;
&lt;P&gt;SET SHOWPLAN_ALL ON&lt;/P&gt;
&lt;P&gt;SET SHOWPLAN_XML ON&lt;/P&gt;
&lt;P&gt;For graphical estimated plans, you can use the Query | Display Estimated Execution Plan menu option. This can be invoked with a toolbar button, or with Cntl-L. &lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can request that SQL Server display actual plans with any of the following options:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;SET STATISTICS PROFILE ON &lt;/P&gt;
&lt;P&gt;SET STATISTICS XML ON &lt;/P&gt;
&lt;P&gt;For graphical actual plans, you can use the Query | Include Actual Execution Plan menu option. This can be invoked with a toolbar button, or with Cntl-K. &lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I'll show you 3 examples of ways in which the actual plan can be useful. They require that you have the AdventureWorks database installed.&lt;/P&gt;
&lt;P&gt;Example 1:&lt;/P&gt;
&lt;P&gt;First, create a stored procedure called 'testtemp' that will build a temporary table.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;USE AdventureWorks&lt;BR&gt;GO&lt;BR&gt;IF EXISTS (SELECT 1 FROM sys.procedures&lt;BR&gt;WHERE name = 'testtemp')&lt;BR&gt;DROP PROC testtemp&lt;BR&gt;GO&lt;BR&gt;CREATE PROC testtemp&lt;BR&gt;(@p int)&lt;BR&gt;AS&lt;BR&gt;SELECT * INTO #t&lt;BR&gt;FROM Sales.SalesOrderDetail &lt;BR&gt;CREATE INDEX t_index ON #t (ProductID)&lt;BR&gt;SELECT * FROM #t&lt;BR&gt;WHERE ProductID &amp;lt; @p&lt;BR&gt;RETURN &lt;BR&gt;GO&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Try to look at the estimated plan for the procedure, and you will get an error because the plan for the SELECT cannot be generated when the temp table #t does not exist: 
&lt;BLOCKQUOTE&gt;
&lt;P&gt;-- Estimated plan &lt;BR&gt;SET SHOWPLAN_TEXT ON&amp;nbsp;&lt;BR&gt;GO&lt;BR&gt;EXEC testtemp 896&lt;BR&gt;-- error is generated&lt;BR&gt;GO&lt;BR&gt;SET SHOWPLAN_TEXT&amp;nbsp;OFF&amp;nbsp;&lt;BR&gt;GO &lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Now look at the actual plan and you will see plans for the temp table creation and for the SELECT from the temp table.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;SET STATISTICS PROFILE ON&amp;nbsp;&lt;BR&gt;GO&lt;BR&gt;EXEC testtemp 896&lt;BR&gt;GO&lt;BR&gt;SET STATISTICS PROFILE OFF&amp;nbsp;&lt;BR&gt;GO &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Example 2:&lt;/P&gt;
&lt;P&gt;One of the main reasons that the actual plan may different from the estimated plan is because of data changes to your data. In general (and assuming you have the option 'auto update statistics' enabled) if more than 20% of the data in a table changes, the optimizer will detect stale statistics and update them automatically. The updated statistics will then trigger a recompile. &lt;/P&gt;
&lt;P&gt;This example makes a copy of Sales.SalesOrderDetail in the AdventureWorks database and builds an index on the table.&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;SELECT * INTO NewOrders &lt;BR&gt;FROM Sales.SalesOrderDetail &lt;BR&gt;GO 
&lt;P&gt;CREATE INDEX IX_NewOrders_ProductID on NewOrders(ProductID) &lt;BR&gt;GO 
&lt;P&gt;SET SHOWPLAN_ALL ON -- Estimated Plan &lt;BR&gt;GO 
&lt;P&gt;-- The estimated plan shows us that a seek is done on the SELECT from NewOrders because at optimization time &lt;BR&gt;--&amp;nbsp;there are only a few rows that have a ProductID value of 897. 
&lt;P&gt;BEGIN TRAN &lt;BR&gt;UPDATE NewOrders &lt;BR&gt;SET ProductID = 897 &lt;BR&gt;WHERE ProductID between 800 and 900 
&lt;P&gt;SELECT OrderQty, CarrierTrackingNumber&lt;BR&gt;FROM NewOrders &lt;BR&gt;WHERE ProductID = 897 &lt;BR&gt;ROLLBACK TRAN &lt;BR&gt;GO 
&lt;P&gt;SET SHOWPLAN_ALL OFF &lt;BR&gt;GO 
&lt;P&gt;&amp;nbsp;&lt;/P&gt;-- The actual plan shows us that a table scan is done on the SELECT from NewOrders because after the update is executed, &lt;BR&gt;--&amp;nbsp; statistics are updated and the query is recompiled. Now there are lots of rows&amp;nbsp;that have a ProductID value of 897. 
&lt;P&gt;SET STATISTICS PROFILE ON -- Actual Plan &lt;BR&gt;GO 
&lt;P&gt;BEGIN TRAN &lt;BR&gt;UPDATE NewOrders &lt;BR&gt;SET ProductID = 897 &lt;BR&gt;WHERE ProductID between 800 and 900 &lt;/P&gt;
&lt;P&gt;SELECT OrderQty, CarrierTrackingNumber &lt;BR&gt;FROM NewOrders &lt;BR&gt;WHERE ProductID = 897 &lt;BR&gt;ROLLBACK TRAN &lt;BR&gt;GO &lt;/P&gt;
&lt;P&gt;SET STATISTICS PROFILE OFF &lt;BR&gt;GO &lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Example 3: 
&lt;P&gt;For this example, you can create a stored procedure that uses a parameter to determine which rows from the Sales.SalesOrderDetail table to select. 
&lt;BLOCKQUOTE&gt;
&lt;P&gt;USE AdventureWorks&lt;BR&gt;GO&lt;BR&gt;IF EXISTS (SELECT 1 FROM sys.procedures&lt;BR&gt;WHERE name = 'GetProducts')&lt;BR&gt;DROP PROC GetProducts&lt;BR&gt;GO&lt;BR&gt;CREATE PROC GetProducts&lt;BR&gt;(@p int)&lt;BR&gt;AS&lt;BR&gt;SELECT * &lt;BR&gt;FROM Sales.SalesOrderDetail &lt;BR&gt;WHERE ProductID = @p&lt;BR&gt;RETURN &lt;BR&gt;GO&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The first time you run the procedure, a plan will be built based on the first parameter. The second time you run the procedure, the original plan will be used, and the statistics should show that SQL Server is performing more reads than there are pages in the table. (There are 1238 pages in the table.) 
&lt;BLOCKQUOTE&gt;
&lt;P&gt;SET STATISTICS IO ON&lt;BR&gt;GO&lt;BR&gt;EXEC GetProducts 710&amp;nbsp; -- using nc index takes 145 logical reads&lt;BR&gt;GO&lt;BR&gt;EXEC GetProducts 707&amp;nbsp; -- using nc index takes 9458 logical reads. &lt;BR&gt;GO &lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can look at the actual plan in XML to see the parameters used for compile and execute.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;SET STATISTICS XML ON&lt;BR&gt;GO&lt;BR&gt;EXEC GetProducts 707 &lt;BR&gt;GO&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Near the bottom of the XML document, you should see the following:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&amp;lt;ParameterList&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;ColumnReference Column="@p" ParameterCompiledValue="(710)" ParameterRuntimeValue="(707)" /&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;lt;/ParameterList&amp;gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Seeing the different values used for compilation and execution can lead you to suspect an issue with parameter sniffing. There are several ways around this problem, but that's the topic for another day. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have fun!&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#ff00ff&gt;&lt;STRONG&gt;~Kalen&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=1999" width="1" height="1"&gt;</description><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/parameter+sniffing/default.aspx">parameter sniffing</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/showplan/default.aspx">showplan</category><category domain="http://www2.sqlblog.com/blogs/kalen_delaney/archive/tags/temp+tables/default.aspx">temp tables</category></item></channel></rss>