managing your tempdb

18
Managing Your Tempdb Choirul Amri Senior Consultant, MCS http://www.choirulamri.org @mchoirul SQL Server User Group Indonesia Jakarta, February 28, 2013

Upload: irawan-soetomo

Post on 25-Dec-2014

881 views

Category:

Technology


0 download

DESCRIPTION

By Choirul Amri.

TRANSCRIPT

Page 1: Managing Your TempDb

Managing Your Tempdb

Choirul Amri

Senior Consultant, MCS

http://www.choirulamri.org

@mchoirul

SQL Server User Group Indonesia

Jakarta, February 28, 2013

Page 2: Managing Your TempDb

Session Takeaway

• What is tempdb, by the way?

• What objects are stored

• How tempdb works

• How to monitor tempdb

• Tempdb best practices

Page 3: Managing Your TempDb

Are you familiar with this?

• Slow query performance

• Runs out of disk space for system database (tempdb)

Page 4: Managing Your 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

Page 5: Managing Your TempDb

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!

Page 6: Managing Your TempDb

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

Page 7: Managing Your TempDb

Monitoring tempdb

• Allocation bottleneck

– Balancing between files

• IO bottleneck

– Disk contention problem

• Space utilization

– Internal, user object, version store

Page 8: Managing Your TempDb

Allocation bottleneck

• Check for PAGELATCH and PAGEIOLACTH wait

• Check for this type of page:

– PFS: 1

– GAM: 2

– SGAM: 3

Page 9: Managing Your TempDb

Checking allocation bottleneck

demo

Page 10: Managing Your TempDb

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

Page 11: Managing Your TempDb

Checking IO bottleneck

demo

Page 12: Managing Your TempDb

Tempdb capacity planning

• There is no exact formula to forecast tempdb

• Monitor tempdb size regularly to forecast the trend, and use it as baseline

Page 13: Managing Your TempDb

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

Page 14: Managing Your TempDb

Monitoring tempdb sizeChecking query with tempdb usage

demo

Page 15: Managing Your TempDb

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

Page 16: Managing Your TempDb

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

Page 17: Managing Your TempDb

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

Page 18: Managing Your TempDb

Thank You

• Discussion…

• Download PPT and Code from my blog:

– http://www.choirulamri.org

– Twitter: mchoirul