index the obvious and not so obvious

44
Index the obvious and not so obvious

Upload: harry-zheng

Post on 27-Jun-2015

138 views

Category:

Software


2 download

DESCRIPTION

Discuss SQL Server index.

TRANSCRIPT

Page 1: Index the obvious and not so obvious

Index the obvious and not so obvious

Page 2: 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.comhttps://twitter.com/HarryZheng

http://ca.linkedin.com/in/harryzhenghttps://www.facebook.com/Harry.H.Zheng

2

Page 3: Index the obvious and not so obvious

Session agenda

1. SQL Server index basics

2. Index Design

3. Common index issues

3

Page 4: Index the obvious and not so obvious

SQL Server index basics

Why do we need index?

• Reduce rows selected in a query as early as possible• Help join • Support sort

4

Page 5: Index the obvious and not so obvious

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

Page 6: Index the obvious and not so obvious

SQL Server index basicsB-Tree Index structure:

6

Page 7: Index the obvious and not so obvious

SQL Server index basics

Get Index Information

• exec sp_helpindex

• select * from sys.indexes • select * from sys.index_columns

7

Page 8: Index the obvious and not so obvious

SQL Server index basics

Index Statistics Info• DBCC SHOW_STATISTICS ('demo.person', 'IX_Person_BusinessIdentityID');

8

Page 9: Index the obvious and not so obvious

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

Page 10: Index the obvious and not so obvious

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

10

Page 11: Index the obvious and not so obvious

SQL Server index basics

  Clustered index - Primary key:

• Alter table Demo.Person add constraint PK_Person primary key (BusinessEntityID);

11

Page 12: Index the obvious and not so obvious

SQL Server index basics

Non-clustered index:

• Leaf level contains the key and include columns• Non-leaf levels contains the index key columns

12

Page 13: Index the obvious and not so obvious

SQL Server index basics

 Unique index:• Create unique index IX_Person_BusinessIdentityID on

demo.Person(BusinessEntityID);

• Is clustered index always unique?

13

Page 14: Index the obvious and not so obvious

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, ModifiedDatefrom Demo.Personwhere LastName = 'Miller'and FirstName = 'Dylan'

14

Page 15: Index the obvious and not so obvious

SQL Server index basics

Filtered index:Create index IX_Person_persontype on demo.Person ( persontype) where persontype = 'EM';

Covered query:select * from Demo.Personwhere persontype = 'EM'

15

Page 16: Index the obvious and not so obvious

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

16

Page 17: Index the obvious and not so obvious

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

17

Page 18: Index the obvious and not so obvious

Index Design

Help Where clause:Case 1:

Select * from Demo.PersonWhere FirstName = 'Dylan' and LastName = 'Miller'

How to index?

18

Page 19: Index the obvious and not so obvious

Index Design

Help Where clause:Case 1:

Select * from Demo.PersonWhere FirstName = 'Dylan' and LastName = 'Miller'

How to index?

Create index IX_Person_FirstName_LastName on Demo.Person

( FirstName, LastName);

19

Page 20: Index the obvious and not so obvious

Index Design

Help Where clause:Case 2 Session 1:

Select * from Demo.PersonWhere FirstName = 'Dylan' and LastName = 'Miller'

Case 2 Session 2:Select * from Demo.PersonWhere LastName = 'Miller'

How to index?

20

Page 21: Index the obvious and not so obvious

Index Design

Help Where clause: Case 2 Session 1:

Select * from Demo.PersonWhere FirstName = 'Dylan' and LastName = 'Miller'

Case 2 Session 2:Select * from Demo.PersonWhere LastName = 'Miller'

How to index?Create index IX_Person_LastName_FirstName on demo.Person ( LastName, FirstName);

21

Page 22: Index the obvious and not so obvious

Index Design

Help Where clause: Session 3:

Select * from Demo.PersonWhere LastName = 'Miller'and ModifiedDate > '2006-09-01‘

How to index?

22

Page 23: Index the obvious and not so obvious

Index Design

Help Where clause: Session 3:

Select * from Demo.PersonWhere LastName = 'Miller'and ModifiedDate > '2006-09-01‘

How to index?

Create index IX_Person_LastName_ModifiedDate on demo.Person ( LastName, ModifiedDate);

23

Page 24: Index the obvious and not so obvious

Index Design

Help Where clause: Case 4:

Select * from Demo.PersonWhere LastName = 'Miller'and FirstName > 'Dylan'and ModifiedDate > '2006-09-01‘

How to index?

24

Page 25: Index the obvious and not so obvious

Index Design

Help Where clause: Case 4:

Select * from Demo.PersonWhere 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);

25

Page 26: Index the obvious and not so obvious

Index Design

Help Where clause: Case 5:

Select * from Demo.PersonWhere LastName = 'Miller'Or FirstName = 'Dylan‘

How to index?

26

Page 27: Index the obvious and not so obvious

Index Design

Help Where clause: Case 5:

Select * from Demo.PersonWhere 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);

27

Page 28: Index the obvious and not so obvious

Index Design

Help join: Case 6:

Select A.AddressLine1,A.City, SP.Name as StateProvinceFrom Demo.Address AInner Join Demo.StateProvince SP

on SP.StateProvinceID = A.StateProvinceID

How to index?

28

Page 29: Index the obvious and not so obvious

Index Design

Help join: Case 6:

Select A.AddressLine1,A.City, SP.Name as StateProvinceFrom Demo.Address AInner Join Demo.StateProvince SP

on SP.StateProvinceID = A.StateProvinceID

How to index?Create index IX_Address_StateProvinceID on demo.Address ( StateProvinceID);

29

Page 30: Index the obvious and not so obvious

Index Design

Help Where clause: Case 7:

Select A.AddressLine1,A.City, SP.Name as StateProvinceFrom Demo.Address AInner Join Demo.StateProvince SP

on SP.StateProvinceID = A.StateProvinceIDWhere A.City = 'Bothell‘

How to index?

30

Page 31: Index the obvious and not so obvious

Index Design

Help Where clause: Case 7:

Select A.AddressLine1,A.City, SP.Name as StateProvinceFrom Demo.Address AInner Join Demo.StateProvince SP

on SP.StateProvinceID = A.StateProvinceIDWhere A.City = 'Bothell‘

How to index?

Create index IX_Address_City_StateProvinceID on demo.Address ( City, StateProvinceID);

31

Page 32: Index the obvious and not so obvious

Index Design

Help order by clause:Case 8:Select * from Demo.PersonWhere LastName = 'Miller'Order by FirstName

How to index?

32

Page 33: Index the obvious and not so obvious

Index Design

Help Order by clause:Case 8:

Select * from Demo.PersonWhere LastName = 'Miller'Order by FirstName

How to index?

Create index IX_Person_LastName_FirstName on Demo.Person ( LastName, FirstName);

33

Page 34: Index the obvious and not so obvious

Index Design

Help Order by clause:Case 9:

Select FirstName, LastName, ModifiedDate from Demo.PersonWhere LastName = 'Miller'and ModifiedDate > '2006-09-01'Order by FirstName

How to index?

34

Page 35: Index the obvious and not so obvious

Index Design

Help Order by clause:Case 9:

Select FirstName, LastName, ModifiedDate from Demo.PersonWhere 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);

35

Page 36: Index the obvious and not so obvious

Common index issues

Drawbacks of index:

• Slow down DML• Disk Space• Fragmentation – page split – low page density

36

Page 37: Index the obvious and not so obvious

Common index issues

Index Fragmentation:• DBCC SHOWCONTIGDBCC SHOWCONTIG scanning 'Person' table...Table: 'Person' (615673241); index ID: 2, database ID: 17LEAF 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

37

Page 38: Index the obvious and not so obvious

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

Page 39: Index the obvious and not so obvious

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

Page 40: Index the obvious and not so obvious

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

40

Page 41: Index the obvious and not so obvious

Common index issues

Diagnostics Tools:

• sp_BlitzIndex • http://www.brentozar.com/blitzindex/

41

Page 42: Index the obvious and not so obvious

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?

42

Page 43: Index the obvious and not so obvious

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

43

Page 44: Index the obvious and not so obvious

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-n

arrow-indexes/• http://www.sqlservercentral.com/articles/Indexing/68439/

44