SlideShare a Scribd company logo
1 of 44
Index the obvious and not so obvious
About myself: 
• Seasoned data architect/DBA with 15 years of SQL Server experience 
• Independent consultant, currently Technical Delivery Manager at 
Government of Alberta 
• Microsoft Certified System Engineer: MCSE 
• Oracle Certified Professional: Oracle DBA 
• IBM Certified Solution Expert: DB2 UDB 
http://netdbsolutions.com 
https://twitter.com/HarryZheng 
http://ca.linkedin.com/in/harryzheng 
https://www.facebook.com/Harry.H.Zheng 
2
Session agenda 
1. SQL Server index basics 
2. Index Design 
3. Common index issues 
3
SQL Server index basics 
Why do we need index? 
• Reduce rows selected in a query as early as possible 
• Help join 
• Support sort 
4
SQL Server index basics 
Index Type: 
• Clustered index 
• Non-clustered index 
• Unique 
• Index with included columns 
• Filtered index 
Not covered in this session 
• Columnstore 
• Spatial index 
• XML index 
• Full-text index 
5
SQL Server index basics 
B-Tree Index structure: 
6
SQL Server index basics 
Get Index Information 
• exec sp_helpindex 
• select * from sys.indexes 
• select * from sys.index_columns 
7
SQL Server index basics 
Index Statistics Info 
• DBCC SHOW_STATISTICS ('demo.person', 'IX_Person_BusinessIdentityID'); 
8
SQL Server index basics 
Index Statistics Info 
• The statistics allow the optimizer to determine the selectivity of an 
index 
• A unique, single-column index always has a selectivity of 1 
• One index entry points to exactly one row 
• Density is the inverse of selectivity 
• Density values range from 0 to 1 
• A selective index has a density of 0.10 or less 
• A unique, single-column index always has a density of 0.0 
• When the index is composite 
• SQL Server maintains detailed statistics only on the leftmost column 
• It does compute density for each column 
• Assume there is an index on (col1, col2, col3) 
• Density is computed for 
• Col1 
• Col1 + Col2 
• Col1 + Col2 + Col3 
9
SQL Server index basics 
Clustered index: 
• Leaf level is the actual data page 
• Non-leaf levels contains the index key columns 
• Clustered index scan = table scan on Heap 
1 
0
SQL Server index basics 
Clustered index - Primary key: 
• Alter table Demo.Person add constraint PK_Person primary key 
(BusinessEntityID); 
1 
1
SQL Server index basics 
Non-clustered index: 
• Leaf level contains the key and include columns 
• Non-leaf levels contains the index key columns 
1 
2
SQL Server index basics 
Unique index: 
• Create unique index IX_Person_BusinessIdentityID on 
demo.Person(BusinessEntityID); 
• Is clustered index always unique? 
1 
3
SQL Server index basics 
Index with included columns - Covering index: 
Create index IX_Person_LastName_FirstName 
on demo.Person ( LastName,FirstName) 
include (ModifiedDate); 
Covered query: 
select FirstName, LastName, ModifiedDate 
from Demo.Person 
where LastName = 'Miller' 
and FirstName = 'Dylan' 
1 
4
SQL Server index basics 
Filtered index: 
Create index IX_Person_persontype 
on demo.Person ( persontype) 
where persontype = 'EM'; 
Covered query: 
select * from Demo.Person 
where persontype = 'EM' 
1 
5
Index Design 
General design rule: 
• Clustered index: 
• Narrow 
• Unique 
• Static 
• Ever-increasing 
• Non-clustered index: 
• Wider often more useful than narrow 
• Decided by the queries that are run against the table 
1 
6
Index Design 
Help Where clause: 
• Queries can seek on index Where the columns in the Where clause 
are a left based subset of the index key 
• Columns used in equality predicates before inequality 
1 
7
Index Design 
Help Where clause: 
Case 1: 
Select * from Demo.Person 
Where FirstName = 'Dylan' 
and LastName = 'Miller' 
How to index? 
1 
8
Index Design 
Help Where clause: 
Case 1: 
Select * from Demo.Person 
Where FirstName = 'Dylan' 
and LastName = 'Miller' 
How to index? 
Create index IX_Person_FirstName_LastName 
on Demo.Person 
( FirstName, LastName); 
1 
9
Index Design 
Help Where clause: 
Case 2 Session 1: 
Select * from Demo.Person 
Where FirstName = 'Dylan' 
and LastName = 'Miller' 
Case 2 Session 2: 
Select * from Demo.Person 
Where LastName = 'Miller' 
How to index? 
2 
0
Index Design 
Help Where clause: 
Case 2 Session 1: 
Select * from Demo.Person 
Where FirstName = 'Dylan' 
and LastName = 'Miller' 
Case 2 Session 2: 
Select * from Demo.Person 
Where LastName = 'Miller' 
How to index? 
Create index IX_Person_LastName_FirstName 
on demo.Person ( LastName, FirstName); 
2 
1
Index Design 
Help Where clause: 
Session 3: 
Select * from Demo.Person 
Where LastName = 'Miller' 
and ModifiedDate > '2006-09-01‘ 
How to index? 
2 
2
Index Design 
Help Where clause: 
Session 3: 
Select * from Demo.Person 
Where LastName = 'Miller' 
and ModifiedDate > '2006-09-01‘ 
How to index? 
Create index IX_Person_LastName_ModifiedDate 
on demo.Person ( LastName, ModifiedDate); 
2 
3
Index Design 
Help Where clause: 
Case 4: 
Select * from Demo.Person 
Where LastName = 'Miller' 
and FirstName > 'Dylan' 
and ModifiedDate > '2006-09-01‘ 
How to index? 
2 
4
Index Design 
Help Where clause: 
Case 4: 
Select * from Demo.Person 
Where LastName = 'Miller' 
and FirstName > 'Dylan' 
and ModifiedDate > '2006-09-01‘ 
How to index? 
Create index IX_Person_LastName_FirstName_ModifiedDate 
on demo.Person ( LastName,FirstName, ModifiedDate); 
Create index IX_Person_LastName_ModifiedDate_FirstName 
on demo.Person ( LastName, ModifiedDate,FirstName); 
2 
5
Index Design 
Help Where clause: 
Case 5: 
Select * from Demo.Person 
Where LastName = 'Miller' 
Or FirstName = 'Dylan‘ 
How to index? 
2 
6
Index Design 
Help Where clause: 
Case 5: 
Select * from Demo.Person 
Where LastName = 'Miller' 
Or FirstName = 'Dylan‘ 
How to index? 
Create index IX_Person_FirstName on demo.Person ( FirstName); 
Create index IX_Person_LastName on demo.Person ( LastName); 
2 
7
Index Design 
Help join: 
Case 6: 
Select A.AddressLine1,A.City, SP.Name as StateProvince 
From Demo.Address A 
Inner Join Demo.StateProvince SP 
on SP.StateProvinceID = A.StateProvinceID 
How to index? 
2 
8
Index Design 
Help join: 
Case 6: 
Select A.AddressLine1,A.City, SP.Name as StateProvince 
From Demo.Address A 
Inner Join Demo.StateProvince SP 
on SP.StateProvinceID = A.StateProvinceID 
How to index? 
Create index IX_Address_StateProvinceID 
on demo.Address ( StateProvinceID); 
2 
9
Index Design 
Help Where clause: 
Case 7: 
Select A.AddressLine1,A.City, SP.Name as StateProvince 
From Demo.Address A 
Inner Join Demo.StateProvince SP 
on SP.StateProvinceID = A.StateProvinceID 
Where A.City = 'Bothell‘ 
How to index? 
3 
0
Index Design 
Help Where clause: 
Case 7: 
Select A.AddressLine1,A.City, SP.Name as StateProvince 
From Demo.Address A 
Inner Join Demo.StateProvince SP 
on SP.StateProvinceID = A.StateProvinceID 
Where A.City = 'Bothell‘ 
How to index? 
Create index IX_Address_City_StateProvinceID 
on demo.Address ( City, StateProvinceID); 
3 
1
Index Design 
Help order by clause: 
Case 8: 
Select * from Demo.Person 
Where LastName = 'Miller' 
Order by FirstName 
How to index? 
3 
2
Index Design 
Help Order by clause: 
Case 8: 
Select * from Demo.Person 
Where LastName = 'Miller' 
Order by FirstName 
How to index? 
Create index IX_Person_LastName_FirstName 
on Demo.Person ( LastName, FirstName); 
3 
3
Index Design 
Help Order by clause: 
Case 9: 
Select FirstName, LastName, ModifiedDate from Demo.Person 
Where LastName = 'Miller' 
and ModifiedDate > '2006-09-01' 
Order by FirstName 
How to index? 
3 
4
Index Design 
Help Order by clause: 
Case 9: 
Select FirstName, LastName, ModifiedDate from Demo.Person 
Where LastName = 'Miller' 
and ModifiedDate > '2006-09-01' 
Order by FirstName 
How to index? 
1.Create index IX_Person_LastName_ModifiedDate on demo.Person ( 
LastName, ModifiedDate); 
2.Create index IX_Person_LastName_ModifiedDate_FirstName on 
demo.Person ( LastName, ModifiedDate,FirstName); 
3.Create index IX_Person_LastName_ModifiedDate on demo.Person ( 
LastName, ModifiedDate) include (FirstName); 
3 
5
Common index issues 
Drawbacks of index: 
• Slow down DML 
• Disk Space 
• Fragmentation – page split – low page density 
3 
6
Common index issues 
Index Fragmentation: 
• DBCC SHOWCONTIG 
DBCC SHOWCONTIG scanning 'Person' table... 
Table: 'Person' (615673241); index ID: 2, database ID: 17 
LEAF level scan performed. 
- Pages Scanned................................: 25 
- Extents Scanned..............................: 5 
- Extent Switches..............................: 4 
- Avg. Pages per Extent........................: 5.0 
- Scan Density [Best Count:Actual Count].......: 80.00% [4:5] 
- Logical Scan Fragmentation ..................: 12.00% 
- Extent Scan Fragmentation ...................: 20.00% 
- Avg. Bytes Free per Page.....................: 107.2 
- Avg. Page Density (full).....................: 98.68% 
DBCC execution completed. If DBCC printed error messages, contact your system administrator. 
Defrag: 
• DBCC DBREINDEX 
• DBCC INDEXDEFRAG 
3 
7
Common index issues 
Misconception: 
• Index is wonderful, create index to cover every query 
• How about create a separate index for each commonly 
used columns? 
38
Common index issues 
Disorder: 
1. Duplicate indexes 
• Will SQL Server use both indexes? 
2. Index Hoarder 
• Lots of Non-Clustered indexes on a single table 
• >5% of Non-Clustered indexes unused 
• Non-Clustered indexes with 0 reads 
• Indexes with 7 or more columns 
• Clustered indexes with > 1 column 
39
Common index issues 
Disorder: 
3. Feature Phobia 
• Included columns SQL 2005+ 
• Filtered indexes – SQL 2008+ Enterprise Edition 
4. Self-Loathing indexes 
• Low fill factor, disabled indexes 
5. Index-A-Phobia 
• Afraid to add index 
4 
0
Common index issues 
Diagnostics Tools: 
• sp_BlitzIndex 
• http://www.brentozar.com/blitzindex/ 
41
Common index issues 
Don’t: 
• Don’t index for the sake of indexing 
• Don’t create duplicated indexes 
• Don’t create very wide indexes 
• Don’t just trust SSMS suggestion 
Discussion: 
• Should we always create indexes for FK columns? 
4 
2
Common index issues 
DMVs for Index Tune 
• sys.dm_db_index_usage_stats 
• sys.dm_db_index_operational_stats 
• sys.dm_db_index_physical_stats 
• sys.dm_db_missing_index_groups 
• sys.dm_db_missing_index_details 
• sys.dm_db_missing_index_group_stats 
• sys.dm_db_missing_index_columns 
4 
3
Reference 
PASS Summit 2012 Sessions: 
• What, Where, Why and How of Indexes (DBA-210) 
• Index Psychiatry Diagnose and Treat the Top 5 Disorders (AD-204) 
• http://sqlinthewild.co.za/index.php/2010/09/14/one-wide-index-or-multiple-narrow-indexes/ 
• http://www.sqlservercentral.com/articles/Indexing/68439/ 
4 
4

More Related Content

Viewers also liked

Saudi Conventional Ammunition and Chemical Warfare Disposal
Saudi Conventional Ammunition and Chemical Warfare DisposalSaudi Conventional Ammunition and Chemical Warfare Disposal
Saudi Conventional Ammunition and Chemical Warfare DisposalPaul Dooley paul@firstaff.ie
 
Managing database project with Visual Studio SSDT and TFS
Managing database project with Visual Studio SSDT and TFSManaging database project with Visual Studio SSDT and TFS
Managing database project with Visual Studio SSDT and TFSHarry Zheng
 
The 21st Century Classroom & Multi-touch Technology
The 21st Century Classroom & Multi-touch TechnologyThe 21st Century Classroom & Multi-touch Technology
The 21st Century Classroom & Multi-touch TechnologyHarry van der Veen
 
Database index(sql server)
Database index(sql server)Database index(sql server)
Database index(sql server)Aaron King
 

Viewers also liked (7)

Saudi Conventional Ammunition and Chemical Warfare Disposal
Saudi Conventional Ammunition and Chemical Warfare DisposalSaudi Conventional Ammunition and Chemical Warfare Disposal
Saudi Conventional Ammunition and Chemical Warfare Disposal
 
Managing database project with Visual Studio SSDT and TFS
Managing database project with Visual Studio SSDT and TFSManaging database project with Visual Studio SSDT and TFS
Managing database project with Visual Studio SSDT and TFS
 
The 21st Century Classroom & Multi-touch Technology
The 21st Century Classroom & Multi-touch TechnologyThe 21st Century Classroom & Multi-touch Technology
The 21st Century Classroom & Multi-touch Technology
 
Database index(sql server)
Database index(sql server)Database index(sql server)
Database index(sql server)
 
Learning from data
Learning from dataLearning from data
Learning from data
 
Learning from Data
Learning from DataLearning from Data
Learning from Data
 
Understanding Mathematical Proof
Understanding Mathematical ProofUnderstanding Mathematical Proof
Understanding Mathematical Proof
 

Similar to Index the obvious and not so obvious

A Billion Goods in a Few Categories: When Optimizer Histograms Help and When ...
A Billion Goods in a Few Categories: When Optimizer Histograms Help and When ...A Billion Goods in a Few Categories: When Optimizer Histograms Help and When ...
A Billion Goods in a Few Categories: When Optimizer Histograms Help and When ...Sveta Smirnova
 
Billion Goods in Few Categories: How Histograms Save a Life?
Billion Goods in Few Categories: How Histograms Save a Life?Billion Goods in Few Categories: How Histograms Save a Life?
Billion Goods in Few Categories: How Histograms Save a Life?Sveta Smirnova
 
Sterling for Windows Phone 7
Sterling for Windows Phone 7Sterling for Windows Phone 7
Sterling for Windows Phone 7Jeremy Likness
 
How Clean is your Database? Data Scrubbing for all Skill Sets
How Clean is your Database? Data Scrubbing for all Skill SetsHow Clean is your Database? Data Scrubbing for all Skill Sets
How Clean is your Database? Data Scrubbing for all Skill SetsChad Petrovay
 
Tips & Tricks SQL in the City Seattle 2014
Tips & Tricks SQL in the City Seattle 2014Tips & Tricks SQL in the City Seattle 2014
Tips & Tricks SQL in the City Seattle 2014Ike Ellis
 
Rails Tips and Best Practices
Rails Tips and Best PracticesRails Tips and Best Practices
Rails Tips and Best PracticesDavid Keener
 
Automated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index RobotAutomated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index RobotMongoDB
 
Billion Goods in Few Categories: How Histograms Save a Life?
Billion Goods in Few Categories: How Histograms Save a Life?Billion Goods in Few Categories: How Histograms Save a Life?
Billion Goods in Few Categories: How Histograms Save a Life?Sveta Smirnova
 
Building better SQL Server Databases
Building better SQL Server DatabasesBuilding better SQL Server Databases
Building better SQL Server DatabasesColdFusionConference
 
PL/SQL New and Advanced Features for Extreme Performance
PL/SQL New and Advanced Features for Extreme PerformancePL/SQL New and Advanced Features for Extreme Performance
PL/SQL New and Advanced Features for Extreme PerformanceZohar Elkayam
 
Data+Modelling.pptx
Data+Modelling.pptxData+Modelling.pptx
Data+Modelling.pptxssuser58c23b
 
Data+Modelling.pptx
Data+Modelling.pptxData+Modelling.pptx
Data+Modelling.pptxssuser58c23b
 
SQL Pass Architecture SQL Tips & Tricks
SQL Pass Architecture SQL Tips & TricksSQL Pass Architecture SQL Tips & Tricks
SQL Pass Architecture SQL Tips & TricksIke Ellis
 
Geek Sync I Consolidating Indexes in SQL Server
Geek Sync I Consolidating Indexes in SQL ServerGeek Sync I Consolidating Indexes in SQL Server
Geek Sync I Consolidating Indexes in SQL ServerIDERA Software
 
SQL Server Tips & Tricks
SQL Server Tips & TricksSQL Server Tips & Tricks
SQL Server Tips & TricksIke Ellis
 
60 reporting tips in 60 minutes - SQLBits 2018
60 reporting tips in 60 minutes - SQLBits 201860 reporting tips in 60 minutes - SQLBits 2018
60 reporting tips in 60 minutes - SQLBits 2018Ike Ellis
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersJonathan Levin
 
Database continuous integration, unit test and functional test
Database continuous integration, unit test and functional testDatabase continuous integration, unit test and functional test
Database continuous integration, unit test and functional testHarry Zheng
 

Similar to Index the obvious and not so obvious (20)

A Billion Goods in a Few Categories: When Optimizer Histograms Help and When ...
A Billion Goods in a Few Categories: When Optimizer Histograms Help and When ...A Billion Goods in a Few Categories: When Optimizer Histograms Help and When ...
A Billion Goods in a Few Categories: When Optimizer Histograms Help and When ...
 
Billion Goods in Few Categories: How Histograms Save a Life?
Billion Goods in Few Categories: How Histograms Save a Life?Billion Goods in Few Categories: How Histograms Save a Life?
Billion Goods in Few Categories: How Histograms Save a Life?
 
Sterling for Windows Phone 7
Sterling for Windows Phone 7Sterling for Windows Phone 7
Sterling for Windows Phone 7
 
How Clean is your Database? Data Scrubbing for all Skill Sets
How Clean is your Database? Data Scrubbing for all Skill SetsHow Clean is your Database? Data Scrubbing for all Skill Sets
How Clean is your Database? Data Scrubbing for all Skill Sets
 
Understanding indices
Understanding indicesUnderstanding indices
Understanding indices
 
Tips & Tricks SQL in the City Seattle 2014
Tips & Tricks SQL in the City Seattle 2014Tips & Tricks SQL in the City Seattle 2014
Tips & Tricks SQL in the City Seattle 2014
 
Rails Tips and Best Practices
Rails Tips and Best PracticesRails Tips and Best Practices
Rails Tips and Best Practices
 
Automated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index RobotAutomated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index Robot
 
Billion Goods in Few Categories: How Histograms Save a Life?
Billion Goods in Few Categories: How Histograms Save a Life?Billion Goods in Few Categories: How Histograms Save a Life?
Billion Goods in Few Categories: How Histograms Save a Life?
 
Building better SQL Server Databases
Building better SQL Server DatabasesBuilding better SQL Server Databases
Building better SQL Server Databases
 
PL/SQL New and Advanced Features for Extreme Performance
PL/SQL New and Advanced Features for Extreme PerformancePL/SQL New and Advanced Features for Extreme Performance
PL/SQL New and Advanced Features for Extreme Performance
 
Data+Modelling.pptx
Data+Modelling.pptxData+Modelling.pptx
Data+Modelling.pptx
 
Data+Modelling.pptx
Data+Modelling.pptxData+Modelling.pptx
Data+Modelling.pptx
 
The perfect index
The perfect indexThe perfect index
The perfect index
 
SQL Pass Architecture SQL Tips & Tricks
SQL Pass Architecture SQL Tips & TricksSQL Pass Architecture SQL Tips & Tricks
SQL Pass Architecture SQL Tips & Tricks
 
Geek Sync I Consolidating Indexes in SQL Server
Geek Sync I Consolidating Indexes in SQL ServerGeek Sync I Consolidating Indexes in SQL Server
Geek Sync I Consolidating Indexes in SQL Server
 
SQL Server Tips & Tricks
SQL Server Tips & TricksSQL Server Tips & Tricks
SQL Server Tips & Tricks
 
60 reporting tips in 60 minutes - SQLBits 2018
60 reporting tips in 60 minutes - SQLBits 201860 reporting tips in 60 minutes - SQLBits 2018
60 reporting tips in 60 minutes - SQLBits 2018
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for Developers
 
Database continuous integration, unit test and functional test
Database continuous integration, unit test and functional testDatabase continuous integration, unit test and functional test
Database continuous integration, unit test and functional test
 

Recently uploaded

KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 

Recently uploaded (20)

KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 

Index the obvious and not so obvious

  • 1. Index the obvious and not so obvious
  • 2. About myself: • Seasoned data architect/DBA with 15 years of SQL Server experience • Independent consultant, currently Technical Delivery Manager at Government of Alberta • Microsoft Certified System Engineer: MCSE • Oracle Certified Professional: Oracle DBA • IBM Certified Solution Expert: DB2 UDB http://netdbsolutions.com https://twitter.com/HarryZheng http://ca.linkedin.com/in/harryzheng https://www.facebook.com/Harry.H.Zheng 2
  • 3. Session agenda 1. SQL Server index basics 2. Index Design 3. Common index issues 3
  • 4. SQL Server index basics Why do we need index? • Reduce rows selected in a query as early as possible • Help join • Support sort 4
  • 5. SQL Server index basics Index Type: • Clustered index • Non-clustered index • Unique • Index with included columns • Filtered index Not covered in this session • Columnstore • Spatial index • XML index • Full-text index 5
  • 6. SQL Server index basics B-Tree Index structure: 6
  • 7. SQL Server index basics Get Index Information • exec sp_helpindex • select * from sys.indexes • select * from sys.index_columns 7
  • 8. SQL Server index basics Index Statistics Info • DBCC SHOW_STATISTICS ('demo.person', 'IX_Person_BusinessIdentityID'); 8
  • 9. SQL Server index basics Index Statistics Info • The statistics allow the optimizer to determine the selectivity of an index • A unique, single-column index always has a selectivity of 1 • One index entry points to exactly one row • Density is the inverse of selectivity • Density values range from 0 to 1 • A selective index has a density of 0.10 or less • A unique, single-column index always has a density of 0.0 • When the index is composite • SQL Server maintains detailed statistics only on the leftmost column • It does compute density for each column • Assume there is an index on (col1, col2, col3) • Density is computed for • Col1 • Col1 + Col2 • Col1 + Col2 + Col3 9
  • 10. SQL Server index basics Clustered index: • Leaf level is the actual data page • Non-leaf levels contains the index key columns • Clustered index scan = table scan on Heap 1 0
  • 11. SQL Server index basics Clustered index - Primary key: • Alter table Demo.Person add constraint PK_Person primary key (BusinessEntityID); 1 1
  • 12. SQL Server index basics Non-clustered index: • Leaf level contains the key and include columns • Non-leaf levels contains the index key columns 1 2
  • 13. SQL Server index basics Unique index: • Create unique index IX_Person_BusinessIdentityID on demo.Person(BusinessEntityID); • Is clustered index always unique? 1 3
  • 14. SQL Server index basics Index with included columns - Covering index: Create index IX_Person_LastName_FirstName on demo.Person ( LastName,FirstName) include (ModifiedDate); Covered query: select FirstName, LastName, ModifiedDate from Demo.Person where LastName = 'Miller' and FirstName = 'Dylan' 1 4
  • 15. SQL Server index basics Filtered index: Create index IX_Person_persontype on demo.Person ( persontype) where persontype = 'EM'; Covered query: select * from Demo.Person where persontype = 'EM' 1 5
  • 16. Index Design General design rule: • Clustered index: • Narrow • Unique • Static • Ever-increasing • Non-clustered index: • Wider often more useful than narrow • Decided by the queries that are run against the table 1 6
  • 17. Index Design Help Where clause: • Queries can seek on index Where the columns in the Where clause are a left based subset of the index key • Columns used in equality predicates before inequality 1 7
  • 18. Index Design Help Where clause: Case 1: Select * from Demo.Person Where FirstName = 'Dylan' and LastName = 'Miller' How to index? 1 8
  • 19. Index Design Help Where clause: Case 1: Select * from Demo.Person Where FirstName = 'Dylan' and LastName = 'Miller' How to index? Create index IX_Person_FirstName_LastName on Demo.Person ( FirstName, LastName); 1 9
  • 20. Index Design Help Where clause: Case 2 Session 1: Select * from Demo.Person Where FirstName = 'Dylan' and LastName = 'Miller' Case 2 Session 2: Select * from Demo.Person Where LastName = 'Miller' How to index? 2 0
  • 21. Index Design Help Where clause: Case 2 Session 1: Select * from Demo.Person Where FirstName = 'Dylan' and LastName = 'Miller' Case 2 Session 2: Select * from Demo.Person Where LastName = 'Miller' How to index? Create index IX_Person_LastName_FirstName on demo.Person ( LastName, FirstName); 2 1
  • 22. Index Design Help Where clause: Session 3: Select * from Demo.Person Where LastName = 'Miller' and ModifiedDate > '2006-09-01‘ How to index? 2 2
  • 23. Index Design Help Where clause: Session 3: Select * from Demo.Person Where LastName = 'Miller' and ModifiedDate > '2006-09-01‘ How to index? Create index IX_Person_LastName_ModifiedDate on demo.Person ( LastName, ModifiedDate); 2 3
  • 24. Index Design Help Where clause: Case 4: Select * from Demo.Person Where LastName = 'Miller' and FirstName > 'Dylan' and ModifiedDate > '2006-09-01‘ How to index? 2 4
  • 25. Index Design Help Where clause: Case 4: Select * from Demo.Person Where LastName = 'Miller' and FirstName > 'Dylan' and ModifiedDate > '2006-09-01‘ How to index? Create index IX_Person_LastName_FirstName_ModifiedDate on demo.Person ( LastName,FirstName, ModifiedDate); Create index IX_Person_LastName_ModifiedDate_FirstName on demo.Person ( LastName, ModifiedDate,FirstName); 2 5
  • 26. Index Design Help Where clause: Case 5: Select * from Demo.Person Where LastName = 'Miller' Or FirstName = 'Dylan‘ How to index? 2 6
  • 27. Index Design Help Where clause: Case 5: Select * from Demo.Person Where LastName = 'Miller' Or FirstName = 'Dylan‘ How to index? Create index IX_Person_FirstName on demo.Person ( FirstName); Create index IX_Person_LastName on demo.Person ( LastName); 2 7
  • 28. Index Design Help join: Case 6: Select A.AddressLine1,A.City, SP.Name as StateProvince From Demo.Address A Inner Join Demo.StateProvince SP on SP.StateProvinceID = A.StateProvinceID How to index? 2 8
  • 29. Index Design Help join: Case 6: Select A.AddressLine1,A.City, SP.Name as StateProvince From Demo.Address A Inner Join Demo.StateProvince SP on SP.StateProvinceID = A.StateProvinceID How to index? Create index IX_Address_StateProvinceID on demo.Address ( StateProvinceID); 2 9
  • 30. Index Design Help Where clause: Case 7: Select A.AddressLine1,A.City, SP.Name as StateProvince From Demo.Address A Inner Join Demo.StateProvince SP on SP.StateProvinceID = A.StateProvinceID Where A.City = 'Bothell‘ How to index? 3 0
  • 31. Index Design Help Where clause: Case 7: Select A.AddressLine1,A.City, SP.Name as StateProvince From Demo.Address A Inner Join Demo.StateProvince SP on SP.StateProvinceID = A.StateProvinceID Where A.City = 'Bothell‘ How to index? Create index IX_Address_City_StateProvinceID on demo.Address ( City, StateProvinceID); 3 1
  • 32. Index Design Help order by clause: Case 8: Select * from Demo.Person Where LastName = 'Miller' Order by FirstName How to index? 3 2
  • 33. Index Design Help Order by clause: Case 8: Select * from Demo.Person Where LastName = 'Miller' Order by FirstName How to index? Create index IX_Person_LastName_FirstName on Demo.Person ( LastName, FirstName); 3 3
  • 34. Index Design Help Order by clause: Case 9: Select FirstName, LastName, ModifiedDate from Demo.Person Where LastName = 'Miller' and ModifiedDate > '2006-09-01' Order by FirstName How to index? 3 4
  • 35. Index Design Help Order by clause: Case 9: Select FirstName, LastName, ModifiedDate from Demo.Person Where LastName = 'Miller' and ModifiedDate > '2006-09-01' Order by FirstName How to index? 1.Create index IX_Person_LastName_ModifiedDate on demo.Person ( LastName, ModifiedDate); 2.Create index IX_Person_LastName_ModifiedDate_FirstName on demo.Person ( LastName, ModifiedDate,FirstName); 3.Create index IX_Person_LastName_ModifiedDate on demo.Person ( LastName, ModifiedDate) include (FirstName); 3 5
  • 36. Common index issues Drawbacks of index: • Slow down DML • Disk Space • Fragmentation – page split – low page density 3 6
  • 37. Common index issues Index Fragmentation: • DBCC SHOWCONTIG DBCC SHOWCONTIG scanning 'Person' table... Table: 'Person' (615673241); index ID: 2, database ID: 17 LEAF level scan performed. - Pages Scanned................................: 25 - Extents Scanned..............................: 5 - Extent Switches..............................: 4 - Avg. Pages per Extent........................: 5.0 - Scan Density [Best Count:Actual Count].......: 80.00% [4:5] - Logical Scan Fragmentation ..................: 12.00% - Extent Scan Fragmentation ...................: 20.00% - Avg. Bytes Free per Page.....................: 107.2 - Avg. Page Density (full).....................: 98.68% DBCC execution completed. If DBCC printed error messages, contact your system administrator. Defrag: • DBCC DBREINDEX • DBCC INDEXDEFRAG 3 7
  • 38. Common index issues Misconception: • Index is wonderful, create index to cover every query • How about create a separate index for each commonly used columns? 38
  • 39. Common index issues Disorder: 1. Duplicate indexes • Will SQL Server use both indexes? 2. Index Hoarder • Lots of Non-Clustered indexes on a single table • >5% of Non-Clustered indexes unused • Non-Clustered indexes with 0 reads • Indexes with 7 or more columns • Clustered indexes with > 1 column 39
  • 40. Common index issues Disorder: 3. Feature Phobia • Included columns SQL 2005+ • Filtered indexes – SQL 2008+ Enterprise Edition 4. Self-Loathing indexes • Low fill factor, disabled indexes 5. Index-A-Phobia • Afraid to add index 4 0
  • 41. Common index issues Diagnostics Tools: • sp_BlitzIndex • http://www.brentozar.com/blitzindex/ 41
  • 42. Common index issues Don’t: • Don’t index for the sake of indexing • Don’t create duplicated indexes • Don’t create very wide indexes • Don’t just trust SSMS suggestion Discussion: • Should we always create indexes for FK columns? 4 2
  • 43. Common index issues DMVs for Index Tune • sys.dm_db_index_usage_stats • sys.dm_db_index_operational_stats • sys.dm_db_index_physical_stats • sys.dm_db_missing_index_groups • sys.dm_db_missing_index_details • sys.dm_db_missing_index_group_stats • sys.dm_db_missing_index_columns 4 3
  • 44. Reference PASS Summit 2012 Sessions: • What, Where, Why and How of Indexes (DBA-210) • Index Psychiatry Diagnose and Treat the Top 5 Disorders (AD-204) • http://sqlinthewild.co.za/index.php/2010/09/14/one-wide-index-or-multiple-narrow-indexes/ • http://www.sqlservercentral.com/articles/Indexing/68439/ 4 4