Geek Sync: Understanding and Monitoring Tempdb

45
Understanding and Monitoring Tempdb June 3, 2015

Transcript of Geek Sync: Understanding and Monitoring Tempdb

Page 1: Geek Sync: Understanding and Monitoring Tempdb

Understanding and Monitoring Tempdb

June 3, 2015

Page 2: Geek Sync: Understanding and Monitoring Tempdb

About Me – Vicky Harp• Product Manager with Idera• Working with SQL Server

since 2001• Working at Idera since 2004

• Twitter: @vickyharp• Email: [email protected]

Page 3: Geek Sync: Understanding and Monitoring Tempdb

What is tempdb?

master model

msdb tempdb

Page 4: Geek Sync: Understanding and Monitoring 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!

Page 5: Geek Sync: Understanding and Monitoring Tempdb

Why monitor tempdb?Temp tables

Table variables

Cursors

Worktables

DBCC CHECKDB

Version Store

Online Index Rebuilds

After Triggers

Page 6: Geek Sync: Understanding and Monitoring Tempdb

Tempdb Issues

File Topology Contention

Version Store

Session Space Usage

Page 7: Geek Sync: Understanding and Monitoring Tempdb

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

Page 8: Geek Sync: Understanding and Monitoring Tempdb

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

Page 9: Geek Sync: Understanding and Monitoring Tempdb

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

Page 10: Geek Sync: Understanding and Monitoring Tempdb

What Size Files?• DBCC CHECKDB with ESTIMATEONLY• Index size of largest index being rebuilt• Monitor, monitor, monitor!

Page 11: Geek Sync: Understanding and Monitoring Tempdb

File Space and Topology• What to check

– File location– File count– Available space

• How often– Every 5 seconds forever

Page 12: Geek Sync: Understanding and Monitoring Tempdb

File Space and Topology• What to check

– File location– File count– Available space

• How often– After server restart– Up to hourly for space available

Page 13: Geek Sync: Understanding and Monitoring Tempdb

Monitoring Files• tempdb.sys.dm_db_file_space_usage• tempdb.sys.database_files

Page 14: Geek Sync: Understanding and Monitoring Tempdb

DEMO

Page 15: Geek Sync: Understanding and Monitoring Tempdb

SQL Server Allocation 101Database

Filegroup

File

Extent

Page

Record

Page 16: Geek Sync: Understanding and Monitoring Tempdb

SQL Server Allocation 101Database

Filegroup

File

Extent

Page

Record

Page 17: Geek Sync: Understanding and Monitoring Tempdb

Pages and Records

Page Header

Row Data

96 bytes

8096bytes

8192 bytes

Page 18: Geek Sync: Understanding and Monitoring Tempdb

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

Page 19: Geek Sync: Understanding and Monitoring Tempdb

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

Page 20: Geek Sync: Understanding and Monitoring Tempdb

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)

Page 21: Geek Sync: Understanding and Monitoring Tempdb

PFS IntervalsPFS Interval (64 MB)

1 2 3 8085 8086 8087…

PFSPage Free space in heap and LOB pages

Page 22: Geek Sync: Understanding and Monitoring Tempdb

Allocation Process

File

GAMIntervals

PFSIntervals

GAMPage

SGAMPage

PFSPage

(Extent 39)Page 312

Page ID 2:1:312

File 1DB 2

Page 23: Geek Sync: Understanding and Monitoring Tempdb

Allocation Page Contention

File

1

GAMPage

SGAMPage

PFSPage

2

3

4

4 3 2 1

Page 24: Geek Sync: Understanding and Monitoring Tempdb

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

Page 25: Geek Sync: Understanding and Monitoring Tempdb

Proportional Fill

File 4File 3File 2File 1

Allocation 1 Allocation 2 Allocation 3 Allocation 4

Page 26: Geek Sync: Understanding and Monitoring Tempdb

Proportional Fill

File 4File 3File 2File 1

Allocation 1Allocation 2

Allocation 3 Allocation 4

Page 27: Geek Sync: Understanding and Monitoring Tempdb

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

Page 28: Geek Sync: Understanding and Monitoring Tempdb

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

Page 29: Geek Sync: Understanding and Monitoring Tempdb

Trace Flags and SQL 2016• TF1117

– Grow all files in a filegroup equally

• TF1118– Do not use mixed extents

• Default behavior for SQL 2016

Page 30: Geek Sync: Understanding and Monitoring Tempdb

Contention• What to check

– Waits on allocation objects in tempdb

• How often– Continuously while configuring tempdb– Weekly spot checks thereafter

Page 31: Geek Sync: Understanding and Monitoring Tempdb

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

Page 32: Geek Sync: Understanding and Monitoring Tempdb

DEMO

Page 33: Geek Sync: Understanding and Monitoring Tempdb

Blocking

CATDOG

Write Read

Read

Page 34: Geek Sync: Understanding and Monitoring Tempdb

Snapshot Isolation

CATFISH

(Ver 1)

Write Read

Read

CATDOG

(Ver 2)

CATFISH

(Ver 1)

Tempdb Version

Store

Page 35: Geek Sync: Understanding and Monitoring Tempdb

Snapshot IsolationRead

Read

CATFISH

(Ver 1)

Tempdb Version

Store

CATDOG

(Ver 2)

CATFISH

(Ver 3)

Page 36: Geek Sync: Understanding and Monitoring Tempdb

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

Page 37: Geek Sync: Understanding and Monitoring Tempdb

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)

Page 38: Geek Sync: Understanding and Monitoring Tempdb

DEMO

Page 39: Geek Sync: Understanding and Monitoring Tempdb

Session Space UsageTemp tables

Table variables

Cursors

Worktables

DBCC CHECKDB

Version Store

Online Index Rebuilds

After Triggers

Page 40: Geek Sync: Understanding and Monitoring Tempdb

Tempdb SpillsOperation RAM

Tempdb

Disk

Page 41: Geek Sync: Understanding and Monitoring Tempdb

Session Space Usage• What to check

– Heavy use of tempdb by user sessions

• How often– Continuously if tuning workload– Otherwise, daily spot checks

Page 42: Geek Sync: Understanding and Monitoring Tempdb

Monitor Session Space Used• tempdb.sys.dm_db_session_space_usage • tempdb.sys.dm_db_task_space_usage

Page 43: Geek Sync: Understanding and Monitoring Tempdb

DEMO

Page 44: Geek Sync: Understanding and Monitoring Tempdb

Tempdb Issues

File Topology Contention

Version Store

Session Space Usage

Page 45: Geek Sync: Understanding and Monitoring Tempdb

THANK YOU!

[email protected]

http://community.idera.com

Q&A Coming Up!