Top Banner
Mastering Tempdb Monica Rathbun, Consultant Denny Cherry and Associates Consulting
33

Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

Sep 27, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

Mastering Tempdb

Monica Rathbun, Consultant

Denny Cherry and Associates

Consulting

Page 2: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

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

Page 3: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

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

Page 4: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

01WHAT IS IT

02CONFIGURE

03CONTENTION

04LOOKING

INSIDE

05SPILLS

06ALWAYSON

AGs

DEMOS

Page 5: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

Understanding TEMPDB

Page 6: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

What is it?

It’s a Global Resource

Used by Everyone

Works in a Round Robin fashion

Non-Durable

Page 7: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

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

Page 8: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

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

Page 9: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

What’s in it?

Temporary user objects that are explicitly created

Internal objects created by the database engine

Worktables to store intermediate results

Page 10: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

What’s in it?

Work files for hash join or hash aggregate operations

Intermediate sort results

Version stores

Page 11: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

TEMPDB Configuration

Page 12: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

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

Page 13: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

TEMPDB Contention

Page 14: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

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

Page 15: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

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

Page 16: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

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/

Page 17: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

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)

Page 18: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

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)

Page 19: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

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

Page 20: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

TEMPDB Space Usage

Page 21: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

What’s taking space?

sys.dm_db_file_space_usage

sys.dm_db_session_space_usage

sys.dm_db_task_space_usage

Page 22: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

What’s in the Version

Store?

sys.dm_tran_active_snapshot_database_transactions

sys.dm_tran_version_store

ALLOW_SNAPSHOT_ISOLATION READ_COMMITTED_SNAPSHOT -

Page 23: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

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

Page 24: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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
Page 25: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

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.

Page 26: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

Spills to TEMPDB

Page 27: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

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.

Page 28: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

Based on Statistics

THE FIXUPDATE YOUR STATISTICS

UNDER ESTIMATESSpills to TEMPDB – causing significant performance degradation

Page 29: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

HOW ALWAYS ON AVAILABILITY GROUPS USE

TEMPDB

Page 30: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

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

Page 31: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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
Page 32: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

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

Page 33: Mastering Tempdb - A Shot of SQLEspresso€¦ · 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

What is using your TEMPDB?

Monica Rathbun

[email protected]

@SQLEspresso

sqlespresso.com

/in/sqlespresso