managing your tempdb
DESCRIPTION
By Choirul Amri.TRANSCRIPT
Managing Your Tempdb
Choirul Amri
Senior Consultant, MCS
http://www.choirulamri.org
@mchoirul
SQL Server User Group Indonesia
Jakarta, February 28, 2013
Session Takeaway
• What is tempdb, by the way?
• What objects are stored
• How tempdb works
• How to monitor tempdb
• Tempdb best practices
Are you familiar with this?
• Slow query performance
• Runs out of disk space for system database (tempdb)
What is tempdb?
• System database
• Used to store temporary data in SQL Server
• What’s in it:
– User objects
– Internal objects
– Version store
Tempdb is often overlooked performance bottleneck
What objects are stored?• User objects
– Your #temp table and table variable
– Internal Objects
• Internal Objects
– Work files and work tables such as cursor, GROUP BY, and sorting operation
– Maintenance operation such as index rebuild and DBCC CHECKDB
• Version Stores
– Snapshot isolation or read-committed snapshot isolation
– Online index rebuild
• Complete list:
– http://technet.microsoft.com/en-US/library/ms345368(v=SQL.90).aspx
– http://technet.microsoft.com/en-us/library/cc966545.aspx
DBA has limited control over tempdb size, developer has!
How tempdb works
• It’s cleared every time the server restarted
• Default: 8MB with 10% autogrowth
• 1 SQL instance = 1 tempdb
• Simple recovery model
• It can be multiple files (MDF)
• Balancing between MDF: SQL Sever will choose the biggest file or the one with more free space
• May not be backed-up, restored, attached, detached
Monitoring tempdb
• Allocation bottleneck
– Balancing between files
• IO bottleneck
– Disk contention problem
• Space utilization
– Internal, user object, version store
Allocation bottleneck
• Check for PAGELATCH and PAGEIOLACTH wait
• Check for this type of page:
– PFS: 1
– GAM: 2
– SGAM: 3
Checking allocation bottleneck
demo
IO Bottleneck
• Use DMV query to get top expensive IO
• Check perfmon to see if tempdb disks are bottleneck
• check sys.dm_io_virtual_file_stats and sys.dm_io_pending_io_requests
Checking IO bottleneck
demo
Tempdb capacity planning
• There is no exact formula to forecast tempdb
• Monitor tempdb size regularly to forecast the trend, and use it as baseline
Tempdb capacity planning
• There is no exact formula to forecast tempdb
• Monitor tempdb size regularly to forecast the trend, and use it as baseline
• Create SQL job to monitor tempdb size at peak time and non peak time
• Put the size in production, and monitor it!
• Example:
• Starting point: 10% of your biggest database size
• Revise the size regularly
Monitoring tempdb sizeChecking query with tempdb usage
demo
Tempdb tuning
• Pre-size the tempdb files, don’t relay on auto growth
• Split to MDF several files
– Starting point: ¼ of CPU cores
– Increase the number by 2 o 4, until allocation bottleneck disappear
• All data files should be at the same size
• You need to restart SQL Server after adding new data file
• Put tempdb on faster disk
• Adding more RAM
Random tempdb myth
• Tempdb files has to be = #CPU cores
– Not really, you may lead to allocation bottleneck!
• You can shrink tempdb to improve performance
– Don’t do it if you have enough space
– Tempdb will grow again, you got IO and allocation problem!
• Placing tempdb files to different disk will improve performance
– Only apply to IO issues, it does not solve allocation bottleneck
More reference• Sunil Agawal blog (MSFT)
– http://blogs.msdn.com/b/sqlserverstorageengine/archive/tags/tempdb/
• Working with tempdb in SQL Server 2005
– http://technet.microsoft.com/en-us/library/cc966545.aspx
• Optimizing tempdb Perfomance
– http://technet.microsoft.com/en-US/library/ms175527(v=sql.90).aspx
Thank You
• Discussion…
• Download PPT and Code from my blog:
– http://www.choirulamri.org
– Twitter: mchoirul