geek sync: understanding and monitoring tempdb
TRANSCRIPT
Understanding and Monitoring Tempdb
June 3, 2015
About Me – Vicky Harp• Product Manager with Idera• Working with SQL Server
since 2001• Working at Idera since 2004
• Twitter: @vickyharp• Email: [email protected]
What is tempdb?
master model
msdb tempdb
How temporary is it?• Recreated on server restart
– Reverts to last configured size– Populated with contents of model database
• Does not run recovery– Everything in tempdb is, well, temporary!
Why monitor tempdb?Temp tables
Table variables
Cursors
Worktables
DBCC CHECKDB
Version Store
Online Index Rebuilds
After Triggers
Tempdb Issues
File Topology Contention
Version Store
Session Space Usage
File Space and Topology• Defaults
– One 8 MB data file on the C: drive– One 1 MB log file on the C: drive– 10% autogrow
File Space and Topology• Best practices
– One 8 MB data file on the C: drive– One 1 MB log file on the C: drive– 10% autogrow
File Space and Topology• Best practices
– Multiple pre-sized data files on a non-system drive
– One pre-sized log file on a non-system drive– Fixed size autogrow as a last resort
What Size Files?• DBCC CHECKDB with ESTIMATEONLY• Index size of largest index being rebuilt• Monitor, monitor, monitor!
File Space and Topology• What to check
– File location– File count– Available space
• How often– Every 5 seconds forever
File Space and Topology• What to check
– File location– File count– Available space
• How often– After server restart– Up to hourly for space available
Monitoring Files• tempdb.sys.dm_db_file_space_usage• tempdb.sys.database_files
DEMO
SQL Server Allocation 101Database
Filegroup
File
Extent
Page
Record
SQL Server Allocation 101Database
Filegroup
File
Extent
Page
Record
Pages and Records
Page Header
Row Data
96 bytes
8096bytes
8192 bytes
Pages and ExtentsPage 1
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8
Extent
8 KB
8 KB
8 KB
8 KB
8 KB
8 KB
8 KB
8 KB
64 KB
Mixed and Dedicated ExtentsTab A
Tab A
Tab A
Tab B
Tab B
Tab B
Tab C
Tab C
Tab A
Tab C
Tab B
8 KB
8 KB
8 KB
8 KB
8 KB
8 KB
8 KB
8 KB
64 KB 64 KB
64 KB
GAM IntervalsGAM Interval (4 GB)
1 2 63998 63999…
GAMPage Allocation state of each extent (1-63999)
SGAMPage Free pages in mixed extents (1-63999)
PFS IntervalsPFS Interval (64 MB)
1 2 3 8085 8086 8087…
PFSPage Free space in heap and LOB pages
Allocation Process
File
GAMIntervals
PFSIntervals
GAMPage
SGAMPage
PFSPage
(Extent 39)Page 312
Page ID 2:1:312
File 1DB 2
Allocation Page Contention
File
1
GAMPage
SGAMPage
PFSPage
2
3
4
4 3 2 1
Allocation Page Contention
File
1 GAM SGAM PFS
2
3
4 4
3
2
1
File
File
File
GAM SGAM PFS
GAM SGAM PFS
GAM SGAM PFS
Proportional Fill
File 4File 3File 2File 1
Allocation 1 Allocation 2 Allocation 3 Allocation 4
Proportional Fill
File 4File 3File 2File 1
Allocation 1Allocation 2
Allocation 3 Allocation 4
File Configuration• Multiple tempdb data files• All files the same size• Presize files to prevent autogrow• Log size 2x size of largest file• Autogrow set to fixed size
Number of Files• Create 1 file for every 2 or 4 logical
CPUs, to a maximum of 8• Monitor for contention• If contention exists, add 4 more files• Repeat until a max of 1 file per
logical CPU
Trace Flags and SQL 2016• TF1117
– Grow all files in a filegroup equally
• TF1118– Do not use mixed extents
• Default behavior for SQL 2016
Contention• What to check
– Waits on allocation objects in tempdb
• How often– Continuously while configuring tempdb– Weekly spot checks thereafter
Monitoring Contention• sys.dm_os_waiting_tasks• wait_type like 'PAGE%LATCH_%'• PageID
– PFS – 1 or multiple of 8088– GAM – 2 or multiple of 511232– SGAM – 3 or (PageID – 1) is multiple of 511232
DEMO
Blocking
CATDOG
Write Read
Read
Snapshot Isolation
CATFISH
(Ver 1)
Write Read
Read
CATDOG
(Ver 2)
CATFISH
(Ver 1)
Tempdb Version
Store
Snapshot IsolationRead
Read
CATFISH
(Ver 1)
Tempdb Version
Store
CATDOG
(Ver 2)
CATFISH
(Ver 3)
Version Store• What to check
– Version store size– Long running version store transactions– Version generation and cleanup rate
• How often– Continuously with snapshot isolation workload– Otherwise, check with space available check
Monitoring Version Store• sys.dm_db_file_space_usage
– version_store_reserved_page_count
• sys.dm_tran_active_snapshot_database_transactions• sys.dm_os_performance_counters
– Version Generation rate (KB/s)– Version Cleanup rate (KB/s)
DEMO
Session Space UsageTemp tables
Table variables
Cursors
Worktables
DBCC CHECKDB
Version Store
Online Index Rebuilds
After Triggers
Tempdb SpillsOperation RAM
Tempdb
Disk
Session Space Usage• What to check
– Heavy use of tempdb by user sessions
• How often– Continuously if tuning workload– Otherwise, daily spot checks
Monitor Session Space Used• tempdb.sys.dm_db_session_space_usage • tempdb.sys.dm_db_task_space_usage
DEMO
Tempdb Issues
File Topology Contention
Version Store
Session Space Usage