Wonder TempDB

Dubi Lebel

Dad, chef, gardener and
DBA
Dubi.lebel@gmail.com
Who am I?
 Dubi- Or Not To Be
 To do is to be - Descartes
 To be is to do - Voltaire
 Do be do be do - Frank Sinatra
...
Temporary Agenda
 It all about TemDB
 For what?
 Where?
 How to?
 best practice!




11/2/2009          Dubi Or Not T...
TempDB
 Only one TempDB per instance.
 Operations within TempDB are minimally logged.
 data in TempDB does not persist ...
What is TempDB
BOL:
      The TempDB system database is a global resource that is
      available to all users connected t...
BOL: TempDB hold the following :
 Temporary user objects that are explicitly
  created, such as: global or local
    tem...
BOL: TempDB hold the following :
 Internal objects that are created by the SQL Server
  Database Engine, for example:
   ...
BOL: TempDB hold the following :
 Row versions that are generated by data
  modification transactions in a database that ...
BOL: TempDB hold the following :
 Row versions that are generated by data
  modification transactions for features, such ...
Is that all?




11/2/2009   Dubi Or Not To Be
                                10
TempDB also hold the following:
 DBCC CHECK
 Service Broker
 Database Mail (Service Broker)
 Event notifications (Serv...
event log:
Source: MSSQLSERVER
 Event ID: 17052
 Description: The log file for database 'TempDB' is full.
 Back up the tra...
error message
Server: Msg 8624, Level 16, State 1
 Internal SQL Server error

 or
Server: Msg 1101, Level 17, State 10, Li...
How to plan TempDB Capacity ?
 Check that auto-grow is on or off for TempDB
 Execute individual queries , workload trace...
How to Monitor TempDB Use?
 sys.dm_db_file_space_usage - disk space that is used
 sys.dm_db_session_space_usage and
  sy...
Monitoring TempDB Disk Space 1
SELECT
 SUM(unallocated_extent_page_count) AS [free pages],
 (SUM(unallocated_extent_page_c...
11/2/2009   Dubi Or Not To Be
                                17
Monitoring TempDB Disk Space 2
SELECT SUM(version_store_reserved_page_count)
     AS [version store pages used],
     (SUM...
11/2/2009   Dubi Or Not To Be
                                19
Monitoring TempDB Disk Space 3
SELECT transaction_id
FROM
  sys.dm_tran_active_snapshot_database_transactions
ORDER BY ela...
11/2/2009   Dubi Or Not To Be
                                21
Monitoring TempDB Disk Space 4
SELECT SUM(internal_object_reserved_page_count)
     AS [internal object pages used],
   (S...
11/2/2009   Dubi Or Not To Be
                                23
Monitoring TempDB Disk Space 5
SELECT SUM(user_object_reserved_page_count)
     AS [user object pages used],
 (SUM(user_ob...
11/2/2009   Dubi Or Not To Be
                                25
Monitoring TempDB Disk Space 6
SELECT SUM(size)*1.0/128 AS [size in MB]
FROM TempDB.sys.database_files

  returns the tota...
11/2/2009   Dubi Or Not To Be
                                27
SELECT
  SUM (user_object_reserved_page_count)*8 as
     usr_obj_kb,
  SUM (internal_object_reserved_page_count)*8 as
    ...
11/2/2009   Dubi Or Not To Be
                                29
which Transact-SQL statements are
the top consumers of tempdb space
 SELECT t1.session_id, t1.request_id,
     t1.task_al...
Perfmon counters
 Access Methods::Worktables Created/sec
    temporary objects and are used to store results for query
 ...
Perfmon counters
 Temp Tables Creation Rate (SQL Server 2005)
    The number of temporary table or variables created/sec...
How to add space to TempDB?
 ALTER DATABASE, the data file will return to the new
  size every time you restart the insta...
Hardware
 Generally, RAID 1+0 provides better write performance
  than any other RAID level providing data protection,
  ...
File Placement
 On a high-performance IO subsystem
 Preferably on a separate spindle(s)
 IO subsystem requirements not ...
Number of Files
 create as many files as needed to maximize disk
  bandwidth.
 One of the best practice is to create one...
equal sizing
The equal sizing of data files is critical because the
  proportional fill algorithm is based on the size of ...
Moving TempDB from system disk
Alter database TempDB modify file
  (name = tempdev,
  filename = „DEV:MyPathTempDB.mdf')

...
What about the log file?
 Most operations in tempdb are not logged.
 Operations on user objects are logged.
 Sort opera...
Moving TempDB from system disk
 Be careful when you move the TempDB database
  because if the TempDB database cannot be c...
What about Shrinking files?
 Auto shrink is not allowed for tempdb.
 How to shrink the TempDB database in SQL Server
  h...
What about auto update stats?
 disabling the auto update stats
 Usually objects created in the TempDB are fairly small
 ...
Indexing temporary table
 Indexing temporary large temp tables greatly increases
  TempDB performance
 use the normal CR...
Restrictions
 Adding filegroups.

 Backing up or restoring the database.

 Changing collation. The default collation is...
Restrictions
 Participating in database mirroring.

 Removing the primary filegroup, primary data file, or log file.

 ...
 Working with tempdb in SQL Server 2005
    http://technet.microsoft.com/en-us/library/cc966545.aspx
   Capacity Plannin...
END or AND ?

D.O.N.T.B


11/2/2009   Dubi Or Not To Be
                                47
Upcoming SlideShare
Loading in …5
×

Nothing Is Not More Permanent Than The Temporary

1,858 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,858
On SlideShare
0
From Embeds
0
Number of Embeds
20
Actions
Shares
0
Downloads
23
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Nothing Is Not More Permanent Than The Temporary

  1. 1. Wonder TempDB Dubi Lebel Dad, chef, gardener and DBA Dubi.lebel@gmail.com
  2. 2. Who am I?  Dubi- Or Not To Be  To do is to be - Descartes  To be is to do - Voltaire  Do be do be do - Frank Sinatra  Yaba Daba Do - Fred Flintstone 11/2/2009 Dubi Or Not To Be 2
  3. 3. Temporary Agenda  It all about TemDB  For what?  Where?  How to?  best practice! 11/2/2009 Dubi Or Not To Be 3
  4. 4. TempDB  Only one TempDB per instance.  Operations within TempDB are minimally logged.  data in TempDB does not persist after SQL Server shuts down.  Only one file group in TempDB is allowed for data and one file group for logs.  By default auto grow is enabled.  When the server restarts, the TempDB file size is reset to the configured value (the default is 8 MB). 11/2/2009 Dubi Or Not To Be 4
  5. 5. What is TempDB BOL: The TempDB system database is a global resource that is available to all users connected to the instance of SQL Server and is used to hold the following: 11/2/2009 Dubi Or Not To Be 5
  6. 6. BOL: TempDB hold the following :  Temporary user objects that are explicitly created, such as: global or local  temporary tables,  temporary stored procedures,  table variables,  Cursors (Key-set-driven and static cursors ). 11/2/2009 Dubi Or Not To Be 6
  7. 7. BOL: TempDB hold the following :  Internal objects that are created by the SQL Server Database Engine, for example:  work tables to store intermediate results for:  spools  sorting.  Hash match.  Index (create with SORT_IN_TempDB ) 11/2/2009 Dubi Or Not To Be 7
  8. 8. BOL: TempDB hold the following :  Row versions that are generated by data modification transactions in a database that uses read- committed  using row versioning isolation  snapshot isolation transactions. 11/2/2009 Dubi Or Not To Be 8
  9. 9. BOL: TempDB hold the following :  Row versions that are generated by data modification transactions for features, such as:  online index operations,  Multiple Active Result Sets (MARS),  AFTER triggers. 11/2/2009 Dubi Or Not To Be 9
  10. 10. Is that all? 11/2/2009 Dubi Or Not To Be 10
  11. 11. TempDB also hold the following:  DBCC CHECK  Service Broker  Database Mail (Service Broker)  Event notifications (Service Broker)  Query notifications (Service Broker)  XML  Common table expression queries (with cte(col,col) as select..)  Large object (LOB) data type variables and parameters 11/2/2009 Dubi Or Not To Be 11
  12. 12. event log: Source: MSSQLSERVER Event ID: 17052 Description: The log file for database 'TempDB' is full. Back up the transaction log for the database to free up some log space 11/2/2009 Dubi Or Not To Be 12
  13. 13. error message Server: Msg 8624, Level 16, State 1 Internal SQL Server error or Server: Msg 1101, Level 17, State 10, Line 1 Could not allocate new page for database 'TempDB'. There are no more pages available in filegroup DEFAULT. Space can be created by dropping objects, adding additional files, or allowing file growth. 11/2/2009 Dubi Or Not To Be 13
  14. 14. How to plan TempDB Capacity ?  Check that auto-grow is on or off for TempDB  Execute individual queries , workload trace files , index maintenance and monitor TempDB space.  Sum the space-use values from the previous steps to predict your total workload usage.  adjust this value. And adjust again. 11/2/2009 Dubi Or Not To Be 14
  15. 15. How to Monitor TempDB Use?  sys.dm_db_file_space_usage - disk space that is used  sys.dm_db_session_space_usage and sys.dm_db_task_space_usage - page allocation or deallocation activity 11/2/2009 Dubi Or Not To Be 15
  16. 16. Monitoring TempDB Disk Space 1 SELECT SUM(unallocated_extent_page_count) AS [free pages], (SUM(unallocated_extent_page_count)*1.0/128) AS [free space in MB] FROM sys.dm_db_file_space_usage; returns the total number of free pages and total free space in megabytes (MB) available in all files in TempDB 11/2/2009 Dubi Or Not To Be 16
  17. 17. 11/2/2009 Dubi Or Not To Be 17
  18. 18. Monitoring TempDB Disk Space 2 SELECT SUM(version_store_reserved_page_count) AS [version store pages used], (SUM(version_store_reserved_page_count)*1.0/128) AS [version store space in MB] FROM sys.dm_db_file_space_usage; returns the total number of pages used by the version store and the total space in MB used by the version store in TempDB 11/2/2009 Dubi Or Not To Be 18
  19. 19. 11/2/2009 Dubi Or Not To Be 19
  20. 20. Monitoring TempDB Disk Space 3 SELECT transaction_id FROM sys.dm_tran_active_snapshot_database_transactions ORDER BY elapsed_time_seconds DESC; If the version store is using a lot of space in TempDB, you must determine what is the longest running transaction. 11/2/2009 Dubi Or Not To Be 20
  21. 21. 11/2/2009 Dubi Or Not To Be 21
  22. 22. Monitoring TempDB Disk Space 4 SELECT SUM(internal_object_reserved_page_count) AS [internal object pages used], (SUM(internal_object_reserved_page_count)*1.0/128) AS [internal object space in MB] FROM sys.dm_db_file_space_usage; returns the total number of pages used by internal objects and the total space in MB used by internal objects in TempDB 11/2/2009 Dubi Or Not To Be 22
  23. 23. 11/2/2009 Dubi Or Not To Be 23
  24. 24. Monitoring TempDB Disk Space 5 SELECT SUM(user_object_reserved_page_count) AS [user object pages used], (SUM(user_object_reserved_page_count)*1.0/128) AS [user object space in MB] FROM sys.dm_db_file_space_usage; returns the total number of pages used by user objects and the total space used by user objects in TempDB. 11/2/2009 Dubi Or Not To Be 24
  25. 25. 11/2/2009 Dubi Or Not To Be 25
  26. 26. Monitoring TempDB Disk Space 6 SELECT SUM(size)*1.0/128 AS [size in MB] FROM TempDB.sys.database_files returns the total amount of disk space used by all files in TempDB. 11/2/2009 Dubi Or Not To Be 26
  27. 27. 11/2/2009 Dubi Or Not To Be 27
  28. 28. SELECT SUM (user_object_reserved_page_count)*8 as usr_obj_kb, SUM (internal_object_reserved_page_count)*8 as internal_obj_kb, SUM (unallocated_extent_page_count)*8 as freespace_kb, SUM (mixed_extent_page_count)*8 as mixedextent_kb FROM sys.dm_db_file_space_usag 11/2/2009 Dubi Or Not To Be 28
  29. 29. 11/2/2009 Dubi Or Not To Be 29
  30. 30. which Transact-SQL statements are the top consumers of tempdb space  SELECT t1.session_id, t1.request_id, t1.task_alloc, t1.task_dealloc, t2.sql_handle, t2.statement_start_offset, t2.statement_end_offset, t2.plan_handle FROM (Select session_id, request_id, SUM(internal_objects_alloc_page_count) AS task_alloc, SUM (internal_objects_dealloc_page_count) AS task_dealloc FROM sys.dm_db_task_space_usage GROUP BY session_id, request_id) AS t1, sys.dm_exec_requests AS t2 WHERE t1.session_id = t2.session_id AND (t1.request_id = t2.request_id) ORDER BY t1.task_alloc DESC 11/2/2009 Dubi Or Not To Be 30
  31. 31. Perfmon counters  Access Methods::Worktables Created/sec  temporary objects and are used to store results for query spool, LOB variables, and cursors.  Typically this number is < 200  Access Methods::Workfiles Created/sec  temporary results for hash joins and hash aggregates  Access Methods: Worktables From Cache Ratio 11/2/2009 Dubi Or Not To Be 31
  32. 32. Perfmon counters  Temp Tables Creation Rate (SQL Server 2005)  The number of temporary table or variables created/sec.  Temp Tables For Destruction (SQL Server 2005)  The number of temporary tables or variables waiting to be destroyed by cleanup system thread. 11/2/2009 Dubi Or Not To Be 32
  33. 33. How to add space to TempDB?  ALTER DATABASE, the data file will return to the new size every time you restart the instance of SQL Server.  Set the recovery model of TempDB to SIMPLE.  Turn on auto-grow, but be careful, % can be dangers! based it on the speed of the I/O subsystem  BUT it is a good idea to turn off the auto-grow feature for the TempDB data files 11/2/2009 Dubi Or Not To Be 33
  34. 34. Hardware  Generally, RAID 1+0 provides better write performance than any other RAID level providing data protection, including RAID 5.  Striping recommended  Separate from user databases  SQL Server 2008  CHECKSUM for new installs (Earlier versions – NONE, Not changeable)  On upgrade NONE still default  Recommend setting CHECKSUM after upgrade 11/2/2009 Dubi Or Not To Be 34
  35. 35. File Placement  On a high-performance IO subsystem  Preferably on a separate spindle(s)  IO subsystem requirements not as strict  Durability not required (KB917047)  RAM Disk or IO subsystem with cache  multiple files on the same physical spindle or multiple devices? 35
  36. 36. Number of Files  create as many files as needed to maximize disk bandwidth.  One of the best practice is to create one data file for each (virtual) CPU on the server  KB 328551 -  Trace flag 1118 switches allocations in TempDB from single-page at a time for the first 8 pages  create the files with equal sizing 11/2/2009 Dubi Or Not To Be 36
  37. 37. equal sizing The equal sizing of data files is critical because the proportional fill algorithm is based on the size of the files. If data files are created with unequal sizes, the proportional fill algorithm tries to use the largest file more for GAM allocations instead of spreading the allocations between all the files, thereby defeating the purpose of creating multiple data files. 11/2/2009 Dubi Or Not To Be 37
  38. 38. Moving TempDB from system disk Alter database TempDB modify file (name = tempdev, filename = „DEV:MyPathTempDB.mdf') Alter database TempDB modify file (name = templog, filename = „DEV:MyPathtemplog.ldf') Restart server 11/2/2009 Dubi Or Not To Be 38
  39. 39. What about the log file?  Most operations in tempdb are not logged.  Operations on user objects are logged.  Sort operations in tempdb also require logging.  Estimating the log space:  similar to estimating the log file size for other databases.  Multi log files? 11/2/2009 Dubi Or Not To Be 39
  40. 40. Moving TempDB from system disk  Be careful when you move the TempDB database because if the TempDB database cannot be created, SQL Server will not start. If the TempDB database cannot be created, start SQL Server by using the (-f) startup parameter and move the TempDB database to a valid location. 11/2/2009 Dubi Or Not To Be 40
  41. 41. What about Shrinking files?  Auto shrink is not allowed for tempdb.  How to shrink the TempDB database in SQL Server http://support.microsoft.com/kb/307487 It is not recommended! 11/2/2009 Dubi Or Not To Be 41
  42. 42. What about auto update stats?  disabling the auto update stats  Usually objects created in the TempDB are fairly small and, as such, the statistics will not reach the threshold that causes the statistics to update automatically.  However, you'll need to manually update statistics if you occasionally use large temporary objects.  What about “auto create statistics” 11/2/2009 Dubi Or Not To Be 42
  43. 43. Indexing temporary table  Indexing temporary large temp tables greatly increases TempDB performance  use the normal CREATE INDEX command after the table has been created.  table variables cannot have indexes created on them. But can have a primary key defined upon creation using the DECLARE @variable TABLE command. 11/2/2009 Dubi Or Not To Be 43
  44. 44. Restrictions  Adding filegroups.  Backing up or restoring the database.  Changing collation. The default collation is the server collation.  Changing the database owner. TempDB is owned by dbo.  Creating a database snapshot.  Dropping the database.  Dropping the guest user from the database.  Enabling change data capture. 11/2/2009 Dubi Or Not To Be 44
  45. 45. Restrictions  Participating in database mirroring.  Removing the primary filegroup, primary data file, or log file.  Renaming the database or primary filegroup.  Running DBCC CHECKALLOC.  Running DBCC CHECKCATALOG.  Setting the database to OFFLINE.  Setting the database or primary filegroup to READ_ONLY. 11/2/2009 Dubi Or Not To Be 45
  46. 46.  Working with tempdb in SQL Server 2005 http://technet.microsoft.com/en-us/library/cc966545.aspx  Capacity Planning for TempDB http://msdn.microsoft.com/en-us/library/ms345368.aspx  TempDB Database http://msdn.microsoft.com/en-us/library/ms190768.aspx  Concurrency enhancements for the TempDB database http://support.microsoft.com/kb/328551  How to shrink the TempDB database in SQL Server http://support.microsoft.com/kb/307487 11/2/2009 Dubi Or Not To Be 46
  47. 47. END or AND ? D.O.N.T.B 11/2/2009 Dubi Or Not To Be 47

×