Usage of Tempdb and Tempdb Contention
what are the usage of tempdb? What is tempdb contention?
SQL DBA Learning Hub – Real-World Database Tutorials
what are the usage of tempdb? What is tempdb contention?
SQL Server has few system databases which was used to store the medata and information related to SQL server which will require to function the SQL server smoothly. Tempdb is one of the important System database.
Tempdb is a system database. It is used to store the temporary objects and intermediate results which created at the time of query executions.
When SQL services stops tempdb gets cleared and when SQL services starts again tempdb always starts as a clean copy.
Tempdb is used to store the user created temporary objects like local temp tables #temp and global temp tables ##temp, table-valued variables and temporary stored procedures.
It is used to perform operations like sorts, hashes, spools, worktables and intermediate result sets when an operation can not fit in a memory.
Data related row versions for features like Read-Committed Snapshot Isolation, Snapshot Isolation, online index rebuilds, MARS and some triggers.
Also, it acts as a temporary storage for certain metadata structures, cursors and some internal objects.
2. Identify the queries and sessions which are burning the tempdb space.
3. Take action on culprit.
– If you get long running or runaway query, reconsider killing it carefully.
– If maintainancr job or any other job is the cause, reschedule it outside of peak hours.
4. Once the cause is resolved shrink the tempdb files.
5. If there is emergency then restart the SQL services.
In SQL Server Tempdb contention occurs when multiple sessions and queries complete for access to shared allocation pages like PFS, GAM and SGAM causing latch waits and performance bottlenecks.
When you execute DMV like sys.dm_exec_requests you will see high waits on PAGEIOLATCH_*, PAGELATCH_* or LATCH_*. This occurs especially during heavy temp table or sort operations. Queries slow down under concurrency.
Below are the types of Contention
Sessions queue for PFS, GAM, SGAM pages during object creation in tempdb data files.
Overloaded access to tempdb system tables(Ex.sys.objects) from many temp table creations.
Add multiple equally sized tempdb data files(1 per logical CPU upto 8, then in multiples of 4) to spread load via round robbin allocation.
For SQL server 2019 and above versions, enable memory optimized tempdb metadata to eliminate metadata issues.
