Investigate TempDB Like Sherlock Holmes
-
Upload
richard-douglas -
Category
Technology
-
view
1.358 -
download
0
Transcript of Investigate TempDB Like Sherlock Holmes
![Page 1: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/1.jpg)
‘What object is served by this circle of misery and violence and fear? It must tend to some end, or else our universe is ruled by chance,
which is unthinkable.’Sherlock Holmes-The Cardboard Box
That’ll be TempDB
![Page 2: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/2.jpg)
INVESTIGATE TEMPDB LIKE
SHERLOCK HOLMES
Richard Douglas• Sales Engineer, SQL Sentry• Social Media: @SQLRich, LinkedIn, • Blog Info: http://blogs.sqlsentry.com/author/RichardDouglas/;
http://SQL.RichardDouglas.co.uk
![Page 3: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/3.jpg)
FREE SENTRY RESOURCES•SQL Performance: www.sqlperformance.com•Team Blogs: blogs.sqlsentry.comArticles:•Educational and Product Videos at sqlsentry.tv Videos:•Trial Downloads at www.sqlsentry.com/HA-DR-Trial•Free Plan Explorer at www.sqlsentry.com/HA-DR-PlanExplorerDownloads:•Get our series of SQL Server Performance Tuning eBooks at www.sqlsentry.com/DBAFundamentals-eBooks. $10 value each, for free!eBooks:
•To schedule a one-on-one demo yourself, go to www.sqlsentry.com/BookADemo. •Or email us at [email protected]:•Free query tuning advice at Answers.SQLPerformance.com Services:•Twitter: @SQLSentry | Facebook: Facebook.com/SQLSentry | LinkedIn: SQL Sentry, LLCCommunity:
The Hairy Execution
Plan Contest starts in April!
![Page 4: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/4.jpg)
YOUR HOST• Richard Douglas• Sales Engineer at SQL Sentry• Blogs: • http://
blogs.sqlsentry.com/author/RichardDouglas• http://SQL.RichardDouglas.co.uk• Twitter: @SQLRich• Email: [email protected]• Slides:
http://www.Slideshare.net/SQLRich
![Page 5: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/5.jpg)
Book a Demo with me by going to http://SQLSentry.com/BookRich
![Page 6: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/6.jpg)
AGENDA• Introduction to TempDB• The life of a temporary object• TempDB Architecture• How queries affect TempDB• Monitoring TempDB• Obscure quotes
![Page 7: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/7.jpg)
WHAT IS TEMPDB USED FOR• Internal Objects
o Joins and aggregations o Cursorso INSTEAD OF Triggers
• Version Storeo DML Triggerso Online Index Rebuildso Snapshot Isolation
• Query Operatorso Sortso Hasheso Spools
• Temp Tables AND Table Variables
![Page 8: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/8.jpg)
TEMP TABLE CREATION PROCESS• An SGAM page is read to find a mixed
extent with space. An exclusive latch is placed
• A PFS page is read to find a free page in the chosen extent. Again an exclusive latch is placed.
Things change in SQL Server 2016
SGAM: Shared Global Allocation Map
PFS: Page Free Space
Latch: Controls thread access to in-memory data structures
Why is this a problem?
![Page 9: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/9.jpg)
‘It is a capital mistake to theorize before one has data. Insensibly one begins to twist facts to suit theories, instead
of theories to suit facts.’Sherlock Holmes -A Scandal in Bohemia
![Page 10: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/10.jpg)
YOU HAVE A GAMMY DATABASE• Each User Database can have 1 or more Filegroups• Each User Database can have 1 or more Data files in
said Filegroups• By default TempDB has 1 Filegroup with 1 data file.
You cannot create a secondary Filegroup• TempDB will pick up certain defaults from the Model
database• TempDB is reinitialised upon service restart
![Page 11: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/11.jpg)
YOU HAVE A GAMMY DATABASE (2)• Each Data File has “special” pages
o PFS – Page Free Space, 1 per ca64MBo GAM – Global Allocation Map, 1 per ca4GBo SGAM – Shared Global Allocation Map , 1 per ca4GB
• By default TempDB has 1 data file sized at 8MBThis means initially 1 SGAM page and 1 PFS page.We have a resource bottleneck.
Page 1: First PFSPage 2: The first GAMPage 3: The first SGAM page
![Page 12: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/12.jpg)
HOW MANY TEMPDB FILES?• Well it depends…• Best current wisdom can be found in KB215845
As a general rule, if the number of logical processors is less than or equal to 8, use the same number of data files as logical processors. If the number of logical processors is greater than 8, use 8 data files and then if contention continues, increase the number of data files by multiples of 4 (up to the number of logical processors) until the contention is reduced to acceptable levels or make changes to the
workload/code.
![Page 13: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/13.jpg)
ADDING TEMPDB FILES
![Page 14: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/14.jpg)
ADDING TEMPDB FILES
![Page 15: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/15.jpg)
2016 TEMP TABLE CREATION PROCESS
• SQL Server 2016 enables the behaviour of trace flags 1117 and 1118 by default.
• 1117 – Grow all files in a file group equally
• 1118 – Use uniform extents only
![Page 16: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/16.jpg)
OTHER NEW FEATURESCommand line option for multiple TempDB files can be set via parameter /SQLTEMPDBFILECOUNT
![Page 17: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/17.jpg)
![Page 18: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/18.jpg)
PERFORMANCE TUNING ADAGE
‘It has long been an axiom of mine that the little things are infinitely the most important.’
Sherlock Holmes -A Case of Identity
![Page 19: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/19.jpg)
![Page 20: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/20.jpg)
TEMP TABLE CACHINGThe following stops the ability to cache temp tables:• Creating a named constraint• Using DDL after creation• Creating using dynamic SQL• Used in an ad-hoc command
![Page 21: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/21.jpg)
MEMORY GRANTS• Query submitted• Optimizer checks stats• Predicts memory needed• Tries to assign contiguous memory (Memory Grants
Pending)• Query runs (Hopefully)
![Page 22: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/22.jpg)
“The world is full of obvious things which nobody by any chance ever observes.”
Sherlock Holmes -The Hound of the BaskervillesChapter 3: “The Problem”
![Page 23: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/23.jpg)
MEASURING TEMPDB PERFORMANCE
![Page 24: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/24.jpg)
PERFMON COUNTERS• Access Methods – Workfiles Created/Sec• Access Methods – Worktables Created/Sec• Access Methods – Worktables From Cache Base• Access Methods – Worktables From Cache Ratio
![Page 25: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/25.jpg)
PERFMON COUNTERS• Cursor Manager By Type – Cursor Worktable Usage
o TSQL Local Cursoro TSQL Global Cursoro API Cursor
• Cursor Manager Total – Cursor Conversion Rate
![Page 26: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/26.jpg)
PERFMON COUNTERS• General Statistics – Active Temp Tables• General Statistics – Temp Tables Creation Rate• General Statistics – Temp Tables For Destruction• Logical Disk - Avg Disk Bytes/Read• Logical Disk - Avg Disk Bytes/Write• Logical Disk - Avg Disk sec/Read• Logical Disk - Avg Disk sec/Write
![Page 27: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/27.jpg)
PERFMON COUNTERS• Transactions - Free Space in TempDB(KB)• Transactions - Transactions• Transactions - Snapshot Transactions• Transactions - Version Cleanup Rate (KB/s)• Transactions - Version Generation Rate(KB/s)• Transactions - Version Store Size(KB)
![Page 28: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/28.jpg)
SPACE USAGESELECTSUM (user_object_reserved_page_count)*8 as Usr_Obj_kb,SUM (internal_object_reserved_page_count)*8 as Internal_Obj_kb,SUM (version_store_reserved_page_count)*8 as Version_Store_kb,SUM (unallocated_extent_page_count)*8 as Freespace_kb,SUM (mixed_extent_page_count)*8 as MixedExtent_kbFROM sys.dm_db_file_space_usage
High % suggests majority of space is taken up by applications creating temporary objects
![Page 29: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/29.jpg)
SPACE USAGESELECTSUM (user_object_reserved_page_count)*8 as Usr_Obj_kb,SUM (internal_object_reserved_page_count)*8 as Internal_Obj_kb,SUM (version_store_reserved_page_count)*8 as Version_Store_kb,SUM (unallocated_extent_page_count)*8 as Freespace_kb,SUM (mixed_extent_page_count)*8 as MixedExtent_kbFROM sys.dm_db_file_space_usage
High % suggests majority of space is taken up by query plan operators. Optimising queries will reduce space usage.
![Page 30: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/30.jpg)
SPACE USAGESELECTSUM (user_object_reserved_page_count)*8 as Usr_Obj_kb,SUM (internal_object_reserved_page_count)*8 as Internal_Obj_kb,SUM (version_store_reserved_page_count)*8 as Version_Store_kb,SUM (unallocated_extent_page_count)*8 as Freespace_kb,SUM (mixed_extent_page_count)*8 as MixedExtent_kbFROM sys.dm_db_file_space_usage
High % suggests majority of space is taken up by version store. Look to curtail long running queries.
![Page 31: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/31.jpg)
SHERLOCK SAYS TO BASELINE
‘You see, but you do not observe. The distinction is clear.’
Sherlock Holmes -A Scandal in Bohemia
![Page 32: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/32.jpg)
LEARN MORE ABOUT BASELININGMy Baselining session:The Day After Tomorrow; Why You Need to BaselineSQLBits Recording:
http://www.sqlbits.com/Sessions/Event12/The_Day_After_Tomorrow_Why_You_Need_to_Baseline
SlideShare:http://www.slideshare.net/SQLRich/the-day-after-tomorrow-why-you-need-to-baseline-sql-rally-2013-amsterdam
![Page 33: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/33.jpg)
‘Having gathered these facts, Watson, I smoked several pipes over them, trying to separate those which were
crucial from others which were merely incidental.’
Sherlock Holmes-The Crooked Man
![Page 34: Investigate TempDB Like Sherlock Holmes](https://reader033.fdocuments.net/reader033/viewer/2022050614/58a622431a28ab416c8b4915/html5/thumbnails/34.jpg)
THANK YOU!• Slides will be available at http://Slideshare.net/SQLRich• More information at:
o SQLSkills, et al
• E-mail [email protected] for free copies of our e-books:o Just tell them where you met me
• My contact info for other questions:o [email protected] Twitter: @SQLRicho Blogs: http://SQL.RichardDouglas.co.uk / http://http://
blogs.sqlsentry.com/author/RichardDouglas