Mastering Tempdb Monica Rathbun, Consultant Denny Cherry and Associates Consulting
Mastering Tempdb
Monica Rathbun, Consultant
Denny Cherry and Associates
Consulting
Consultant
Denny Cherry & Associates Consulting
PASS Mid-Atlantic Regional Mentor
User Group Leader: Hampton Roads VA
SQL Saturday VA Beach Organizer
Monica Rathbun
/sqlespresso
@SQLEspresso
SQLEspresso.com
1. Drag & drop your photo onto the slide.
2. Resize & crop so the key part of the photo will show up here. You will want the picture to go to ALL THE WAY to the top, bottom, and left side. On the right, position it so it only just barely covers the red line.
IMPORTANT: Only resize photos using the CORNER SQUARE to maintain proportions (never have distorted photos in your slides). Use that to get the HEIGHT you need (so it touches the top and bottom of the slide. Use CROP(double click the photo and it will show up under “picture format” tab) to get the photo to the WIDTH you need.
Always Ask Questions
Interrupt me
This is a two-way conversation let’s learn from each other’s experiences
01WHAT IS IT
02CONFIGURE
03CONTENTION
04LOOKING
INSIDE
05SPILLS
06ALWAYSON
AGs
DEMOS
Understanding TEMPDB
What is it?
It’s a Global Resource
Used by Everyone
Works in a Round Robin fashion
Non-Durable
What is it?
Minimal Logging
Objects are frequently created and destroyed at VERY high concurrency
Used for data that doesn’t fit in memory
Needs to be accessed FAST
Remember These Things
It’s a Global Resource
Used By Everyone
Works in a Round Robin fashion
You Only
Have 1
Things
WILL
Disappear
Size
Matters
What’s in it?
Temporary user objects that are explicitly created
Internal objects created by the database engine
Worktables to store intermediate results
What’s in it?
Work files for hash join or hash aggregate operations
Intermediate sort results
Version stores
TEMPDB Configuration
Pin to High Performing
Disk
Multiple Files
1 file per CPU core up to 8
files *
Properly Sized Auto Growth
Put MDF and LDF on Same
Disk
Proper Configuration is KEY
TEMPDB Contention
Types
Object Allocation Contention
• Contention for metadata pages used to manage space allocation in data files
• PAGELATCH database id 2: waits on PFS and GAM Pages
Metadata Contention
• Pages that belong to systems object used to track table metadata
• PAGELATCH waits on pages that belong to system objects
Temp Table Cache Contention
• Contention for memory objects associated with Cache
• Either CMEMTHREAD waits or SOS_CACHESTORE
spinlock waits
Slide Credit Sourced from Pam Lahoud
Object Allocation Example
Uses “Special” Pages in Memory
• Thread 1
• Thread 2
• Thread 3
Physical Disk
• GAM, SGAM, PFS (The special pages)
• Determine “WHERE” to write the data
LATCH (light weight lock)
• Whenever a process needs to update one of these special page it takes a latch
Queuing Causes Waits/Delays
• Lots of processes happen to hit at the exact same time
• They all need to find a place to store some data
Trace Flag 1118/1117
Pre 2016
Heavy Contention in Metadata fixed
2016 CU 8 and 2017 CU 5
Proper Use of Table Variables
Proper Use of TEMP Tables
Use new inline index creation syntax
SQL Server 2014
Remember It has nothing to do with I/OIt’s Latch, Object or MetadataContention
Great Resource by Pam Lahoudhttps://blogs.msdn.microsoft.com/sql_server_team/tempdb-files-and-trace-flags-and-updates-oh-my/
Less Overhead than #Temp tables
Rule of thumb <100 rows
No Statistics on Variables
Trace Flag 2453 allows table variables to trigger recompile
DECLARE @MyFakeTable TABLE(Field1 VARCHAR(10) NULL,Field2 INT NOT NULL)
Do not explicitly DROP temp tables at the end of a stored procedure
Do not ALTER temp tables
Do not TRUNCATE temp tables
The more temp tables that need to be deleted, the more likely you are to hit the contention.
CREATE TABLE #MyFakeTable (Field1 VARCHAR(10) NULL,Field2 INT NOT NULL)
1. Drag & drop your photo onto the slide.
2. Resize & crop so the key part of the photo will show up here. You will want the picture to go to ALL THE WAY to the top, bottom, and left side. On the right, position it so it only just barely covers the red line.
IMPORTANT: Only resize photos using the CORNER SQUARE to maintain proportions (never have distorted photos in your slides). Use that to get the HEIGHT you need (so it touches the top and bottom of the slide. Use CROP(double click the photo and it will show up under “picture format” tab) to get the photo to the WIDTH you need.
TEMP Table Cache Improvements
Concurrent PFS Updates
Memory Optimized TempDB Metadata
TEMPDB Space Usage
What’s taking space?
sys.dm_db_file_space_usage
sys.dm_db_session_space_usage
sys.dm_db_task_space_usage
What’s in the Version
Store?
sys.dm_tran_active_snapshot_database_transactions
sys.dm_tran_version_store
ALLOW_SNAPSHOT_ISOLATION READ_COMMITTED_SNAPSHOT -
IDENTIFY & TUNE
Great Resource by Kendra Littlehttps://littlekendra.com/2009/08/27/whos-using-all-that-space-in-tempdb-and-whats-their-plan/
IDENTIFY &
TUNE
ISSUE CHECKPOINT
DBCC SHRINKFILE
SHRINK?
DONE
DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE
DBCC FREESYSTEMCACHE
DBCC FREESESSIONCACHE
DBCC SHRINKFILE
DBCC SHRINKFILE
DONE
SHRINK?
These operations remove all
caches, which will impact server performance. Don’t do this
stuff unless necessary. Be sure to database scope when
possible.
Spills to TEMPDB
The spill data to TempDB warning means that the query was not granted enough memory to finish the operation and spilled over into the TempDB to complete the operation and had to go to DISK.
Based on Statistics
THE FIXUPDATE YOUR STATISTICS
UNDER ESTIMATESSpills to TEMPDB – causing significant performance degradation
HOW ALWAYS ON AVAILABILITY GROUPS USE
TEMPDB
What about AlwaysOn
AGs?
Pertains to READ ONLY Replicas ONLY
Use SnapShot Isolation by default i.e. Version Store
Any query run uses the version store
Long running queries can fill up TEMPDB with Version Store data
Statistics are created in TEMPDB
TEMPDB Contention & Config by Pam Lahoudhttps://blogs.msdn.microsoft.com/sql_server_team/tempdb-files-and-trace-flags-and-updates-oh-my/
TEMPDB Usage by Kendra Littlehttps://littlekendra.com/2009/08/27/whos-using-all-that-space-in-tempdb-and-whats-their-plan/
Always on Statistics by Rajendra Guptahttps://www.sqlshack.com/sql-server-statistics-in-always-on-availability-groups/
TEMPDB by Microsofthttps://docs.microsoft.com/en-us/sql/relational-databases/databases/tempdb-database?view=sql-server-ver15
Bob Ward Internals of TEMPDB https://www.youtube.com/watch?v=SvseGMobe2w
What is using your TEMPDB?
Monica Rathbun
@SQLEspresso
sqlespresso.com
/in/sqlespresso