Download presentation
Presentation is loading. Please wait.
1
Indexing For Optimal Performance
Robert Douglas SQL Saturday #464, Melbourne 20th February 2016
2
Housekeeping Mobile Phones Evaluations Wifi Details
please set to “stun” during sessions Evaluations complete online to be in the draw for fantastic prizes Wifi Details Network: Eduroam Login: ext-sqlsat Password: sqlsaturd4y SESSIONS EVENT sessions/sessionevaluation.aspx eventeval.aspx
3
Connect with the Community
Event staff, volunteers and speakers are here to help and answer questions. Scan the QR code on the speaker badges to connect and network with them. speaker @SQLDropbear I attack SQL challenges by dropping onto them from above.
4
Thanks to the Sponsors Please make sure you visit our fantastic sponsors:
5
Robert Douglas Consultant for SQL Services Limited
BA(hons),MCTS, MCITP(2005/2008), MCSA(2012) Consultant for SQL Services Limited Christchurch\Nelson SQL Server Users Group
6
What We Will Cover What is an Index and why should I care?
Fragmentation What is it? Why does it effect my queries? How to combat it? Designing Smarter Indexes Indexing Mistakes to Avoid
7
Types of Indexes Heaps Clustered Index One per table The actual data
Non-Clustered Index Many per table Copies ‘bits’ of the table Covering Index?
8
What is an Index and Why Should I Care?
PAGE HEADER DATA PAGE DATA 8K DATA (Index OR Data Rows) SLOT ARRAY
9
What is an Index and Why Should I Care?
DATA 8K Index Pages DATA 8K DATA 8K DATA 8K DATA 8K DATA 8K DATA 8K DATA 8K DATA 8K DATA 8K DATA 8K DATA 8K DATA 8K
10
What is an Index and Why Should I Care?
A-J A - E F-J A B C D E F G H I J
11
What is an Index and Why Should I Care?
A-J A - E F-J A B C D E F G H I J
12
SELECT * FROM dbo.TABLE WHERE Word=‘Go’
A-J A - E F-J A B C D E F G H I J
13
SELECT * FROM dbo.TABLE WHERE Word BETWEEN ‘B’ AND ‘G’
A-J A - E F-J A B C D E F G H I J
14
SELECT * FROM dbo.TABLE WHERE Word BETWEEN ‘B’ AND ‘G’ READAHEAD
A-J A - B C-D E-F G-H I-J A B C D E F G H I J
15
What is Fragmentation? External Fragmentation
Referred to as “Logical Fragmentation” in books online. Next logical page is not the next physical page. External Fragmentation reduces Read Ahead Performance. That reduces the efficiency of your range scans. Only refers to pages on disk(pages in memory are always logically contiguous) avg_fragmentation_in_percent in the sys.dm_db_index_physical_stats DMF
16
What is Fragmentation? Internal Fragmentation
Sometimes referred to as physical fragmentation. Empty space on pages. Avg_page_space_used_in_percent in sys.dm_db_index_physical_stats DMF. The effects of internal fragmentation are: More disk space needed to hold the data. More IO to read the same amount of data. More space in memory used to hold the same data.
17
What Causes Fragmentation? Page Splits
Page Splits happen when there is not enough space on a page. SQL has to write in as specific location. Inserts on pages that are already full. Updates which increase the size of a row. Snapshot isolation(makes updated records 14 bytes longer). Page must either be compacted or split. Close to, but not always 50/50.
18
What happens when you discover fire?
F-J FIRE F G H I J
19
What happens when you discover fire?
F-J FIRE G H I J F F2
20
What happens when you discover fire? A “Good” Page Split
A-F FIRE A B C D E F F2
21
How Do I Remove Fragmentation? REBUILD and REORGANISE Indexes
Rebuild Indexes This is an expensive operation It requires the size of the index as additional space It reads and writes the entire index. It takes locks on your pages. Reorganise Indexes Always online Rollback is one page. Doesn’t update stats. Maintenance Plans. Smart scripts
22
How Do I Remove Fragmentation? Avoid it in the first place
Re-Architect your tables Avoid GUIDS or other random keys on highly transactional tables. Be aware of when updates are likely to occur on variable length columns. Keep clustered index narrow if possible. Avoid very wide rows Fill Factor Select * from sys.configurations where name like 'fill factor%‘ select * from sys.indexes order by fill_factor DESC Avoid database shrinks.
23
How Shrinking Works
24
Designing Smarter Indexes
Know your workload Query Hints Missing Indexes DMV Sys.dm_db_missing_index_details SYS.dm_db_missing_index_Columns SYS.dm_db_missing_index_groups SYS.dm_db_missing_index_group_stats Prune unwanted Indexes Unused Indexes SP_BlitzIndex – BrentOzar.com
25
Indexing Mistakes to Avoid #1: Blindly Following “Suggested Indexes” Recommendations
Query Plan Hints Missing Indexes DMV Sys.dm_db_missing_index_details SYS.dm_db_missing_index_Columns SYS.dm_db_missing_index_groups SYS.dm_db_missing_index_group_stats Database Tuning Adviser 3rd Party Tools The Internet
26
Indexing Mistakes to Avoid #2: Fill Factor Suicide
Fill factor will remove or slow External Fragmentation But it increases internal fragmentation.
27
Indexing Mistakes to Avoid #3: The Wrong Maintenance
If you have regular maintenance in place – do you know what it does? Rebuilding Indexes that don’t need it. Reorganising Indexes without updating stats
28
Bonus Indexing Mistake: Readable Secondaries
If you have a readable secondary (Mirroring or AOAG) it turns on snapshot isolation on the secondary replica. That doesn’t turn on snapshot isolation on the primary, but it will add the 14 byte versioning tag on them. If your pages are built to 100%, this will cause every page you query on the secondary split on the primary.
29
Questions? Please make sure you visit our fantastic sponsors:
30
How did we do? Please complete an online Evaluation to be included the draw for a fantastic prize! There is a prize for each session timeslot and for the overall event survey – so the more feedback you provide the more chances you have. Post-Event Survey eventeval.aspx Session Surveys sessions/sessionevaluation.aspx
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.