<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlserver.ro/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>How-To Articles</title><link>http://sqlserver.ro/forums/19/ShowForum.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Just Temporary</title><link>http://sqlserver.ro/forums/thread/92.aspx</link><pubDate>Sat, 16 Sep 2006 10:47:03 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:92</guid><dc:creator>xmldeveloper</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/92.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=19&amp;PostID=92</wfw:commentRss><description>&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;Almost everything around us is temporary starting with your job and ending with your beer supply. You may argue that a job may last for a life time but even life is temporary. In SQL Server temporary means &lt;B&gt;tempdb&lt;/B&gt;, a database where all ephemeral things happen. As you may know by now, SQL Server 2005 uses &lt;B&gt;tempdb&lt;/B&gt; more than its predecessors, a lot more. The goal of this article is to tell you why.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;SQL Server 2005 has far more features than previous versions of SQL Server, and yes, this is one of the reasons for increased &lt;B&gt;tempdb&lt;/B&gt; usage. More features mean a greater need of temporary objects and obviously of &lt;B&gt;tempdb&lt;/B&gt; space. Some of the new features that may use &lt;B&gt;tempdb&lt;/B&gt; are Service Broker, Database Mail, CTEs (Common Table BLOCKED EXPRESSION, Event Notifications or MARS (Multiple Active Result Sets). &lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;The second reason for an extensive use of &lt;B&gt;tempdb&lt;/B&gt; is that some of the features, that do not use &lt;B&gt;tempdb&lt;/B&gt; in earlier versions of SQL Server, use &lt;B&gt;tempdb&lt;/B&gt; in SQL Server 2005. For example the &lt;B&gt;deleted&lt;/B&gt; and &lt;B&gt;inserted&lt;/B&gt; tables, used by AFTER triggers, are created in &lt;B&gt;tempdb&lt;/B&gt; (in earlier versions the transaction log is used for the same purpose).&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;The third and the last reason I will mention here, is that some of the features that used &lt;B&gt;tempdb&lt;/B&gt; in earlier versions of SQL Server will require more &lt;B&gt;tempdb&lt;/B&gt; space in SQL Server 2005. For example the DBCC CHECKDB statement, will use more &lt;B&gt;tempdb&lt;/B&gt; space due to more comprehensive logical checks, more complex structures to be checked and some new checks to be performed for the new features in SQL Server 2005.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;To get a better image of features that may require &lt;B&gt;tempdb&lt;/B&gt; disk space (and because I like lists) I will give you a list with &lt;B&gt;tempdb&lt;/B&gt; “offenders”:&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;Row versioning – row versions are held in the &lt;B&gt;tempdb&lt;/B&gt; version store. Row versioning supports features such as online index operations, Multiple Active Result Sets (MARS), and AFTER triggers. They are also used&amp;nbsp; by databases that use read-committed snapshot isolation or snapshot isolation levels.&lt;/FONT&gt; &lt;/P&gt;
&lt;LI&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;Bulkload operations with triggers enabled - row versioning is used for this feature.&lt;/FONT&gt; &lt;/P&gt;
&lt;LI&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;Keyset-driven cursors and static cursors use work tables built in &lt;B&gt;tempdb&lt;/B&gt;.&lt;BR&gt;Common-table-expression (CTEs) queries - work tables are created in &lt;B&gt;tempdb&lt;/B&gt; for spool operations when CTEs queries are executed.&lt;/FONT&gt; &lt;/P&gt;
&lt;LI&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;Service Broker – uses &lt;B&gt;tempdb&lt;/B&gt; for various reasons such as caching and preserving dialog context. Some of the features that rely on Service Broker like Database Mail, Event Notifications and Query Notifications can also use &lt;B&gt;tempdb&lt;/B&gt;.&lt;/FONT&gt; &lt;/P&gt;
&lt;LI&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;DBCC CHECKDB statement (already mentioned) uses &lt;B&gt;tempdb&lt;/B&gt; for work tables.&lt;/FONT&gt; &lt;/P&gt;
&lt;LI&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;Indexes – creating and rebuilding indexes can use &lt;B&gt;tempdb&lt;/B&gt; when you set the SORT_IN_&lt;B&gt;TEMPDB&lt;/B&gt; option to ON. Concurrent user modification (updates or deletes) during online index operations will require &lt;B&gt;tempdb&lt;/B&gt; space.&lt;/FONT&gt; &lt;/P&gt;
&lt;LI&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;Large object (LOB) data type variables and parameters of types like varchar(max), nvarchar(max), varbinary(max) text, ntext, image, and xml and string functions operating on LOBs may use &lt;B&gt;tempdb&lt;/B&gt;.&lt;/FONT&gt; &lt;/P&gt;
&lt;LI&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;Multiple Active Result Sets (MARS) can use &lt;B&gt;tempdb&lt;/B&gt;&lt;/FONT&gt; &lt;/P&gt;
&lt;LI&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;Queries that contain DML statements (INSERT, UPDATE, DELETE) and use internal objects for storing intermediate results (in case of hash joins, hash aggregates, or sorting). &lt;/FONT&gt;&lt;/P&gt;
&lt;LI&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;Temporary tables and table variables&lt;/FONT&gt; &lt;/P&gt;
&lt;LI&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;Tables returned by table-valued functions&lt;/FONT&gt; &lt;/P&gt;
&lt;LI&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;User-defined tables and indexes&lt;/FONT&gt; &lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;Why should you care about &lt;B&gt;tempdb&lt;/B&gt; disk space? Because it can become one of your greatest performance pains if the disk space becomes insufficient. I will not give you the cure (the cure being beyond the purpose of this article) but I will give you some resources at the end of the article, resources that do an excellent job in teaching you how to deal with &lt;B&gt;tempdb&lt;/B&gt; usage. However I want to mention three of the new Dynamic Management Views and Functions that can help you monitor the &lt;B&gt;tempdb&lt;/B&gt; disk space usage:&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;The &lt;B&gt;sys.dm_db_file_space_usage&lt;/B&gt; dynamic management view allows you to monitor the disk space used for each of the &lt;B&gt;tempdb&lt;/B&gt; files.&lt;/FONT&gt; 
&lt;LI class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;The &lt;B&gt;sys.dm_db_session_space_usage&lt;/B&gt; dynamic management view will display the page allocation or deallocation activity in &lt;B&gt;tempdb&lt;/B&gt; at the session level.&amp;nbsp; &lt;/FONT&gt;
&lt;LI class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;The &lt;B&gt;sys.dm_db_task_space_usage&lt;/B&gt; dynamic management view will do the same thing as the previous DMV (but at the task level).&lt;/FONT&gt; &lt;/LI&gt;&lt;/UL&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;To get a feel of how you can use them, I will give you an example:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;B&gt;&lt;SPAN&gt;&lt;FONT size=2&gt;Example:&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR&gt;DECLARE @i int&lt;BR&gt;SET @i = 0&lt;BR&gt;CREATE TABLE #T (c char(8000))&lt;/SPAN&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;B&gt;&lt;SPAN&gt;WHILE @i &amp;lt; 100&lt;BR&gt;BEGIN&lt;BR&gt;INSERT INTO #T SELECT ('Test')&lt;BR&gt;SET @i = @i + 1&lt;BR&gt;END&lt;/SPAN&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;B&gt;&lt;SPAN&gt;SELECT user_objects_alloc_page_count&lt;BR&gt;FROM sys.dm_db_session_space_usage&lt;BR&gt;WHERE session_id = @@SPID;&lt;/SPAN&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;B&gt;&lt;SPAN&gt;--Results&lt;BR&gt;user_objects_alloc_page_count &lt;BR&gt;-----------------------------&lt;BR&gt;104&lt;/SPAN&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;In this simple example, I’ve created a temporary table and then I used the &lt;B&gt;sys.dm_db_session_space_usage&lt;/B&gt; dynamic management view to check the number of pages reserved for user objects in &lt;B&gt;tempdb&lt;/B&gt;. I filtered the result for my session using the &lt;B&gt;@@SPID&lt;/B&gt; function. You can use the same DMV to build more complex queries and get the pages reserved for internal objects or for row versioning. &lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;I will end my article here, reminding you again that the SQL Server 2005 uses &lt;B&gt;tempdb&lt;/B&gt; for more things than SQL Server 2000 does, so please consider this when you upgrade or build your applications using SQL Server 2005.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;You have been warned!&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial size=2&gt;Thank you!&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;&lt;B&gt;Additional Resources:&lt;BR&gt;&lt;/B&gt;Books Online – “Troubleshooting Insufficient Disk Space in &lt;B&gt;tempdb&lt;/B&gt;” &lt;/FONT&gt;&lt;/FONT&gt;&lt;A href="http://msdn2.microsoft.com/en-US/library/ms176029(SQL.90).aspx"&gt;&lt;FONT face=Arial size=2&gt;http://msdn2.microsoft.com/en-US/library/ms176029(SQL.90).aspx&lt;/FONT&gt;&lt;/A&gt;&lt;BR&gt;&lt;FONT face=Arial size=2&gt;Books Online – “Capacity Planning for &lt;B&gt;tempdb&lt;/B&gt;” &lt;/FONT&gt;&lt;A href="http://msdn2.microsoft.com/en-US/library/ms345368(SQL.90).aspx"&gt;&lt;FONT face=Arial size=2&gt;http://msdn2.microsoft.com/en-US/library/ms345368(SQL.90).aspx&lt;/FONT&gt;&lt;/A&gt;&lt;BR&gt;&lt;FONT face=Arial size=2&gt;“Troubleshooting Performance Problems in SQL Server 2005” &lt;/FONT&gt;&lt;A href="http://www.microsoft.com/technet/prodtechnol/sql/2005/tsprfprb.mspx#ESVAE"&gt;&lt;FONT face=Arial size=2&gt;http://www.microsoft.com/technet/prodtechnol/sql/2005/tsprfprb.mspx#ESVAE&lt;/FONT&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT size=1&gt;Published in Culminis Compass&lt;/FONT&gt;&lt;/P&gt;</description></item></channel></rss>