22
SQL Server Indexing for the Client Developer Denny Cherry Manager of Information Systems [email protected] MVP, MCSA, MCDBA, MCTS, MCITP

Denny Cherry Manager of Information Systems [email protected] MVP, MCSA, MCDBA, MCTS, MCITP

Embed Size (px)

Citation preview

Page 1: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

SQL Server Indexing for the Client Developer

Denny CherryManager of Information Systems

[email protected], MCSA, MCDBA, MCTS, MCITP

Page 2: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

2

About MeAuthor or Coauthor of 4 books6+ SQL Mag articlesDozens of other articlesMicrosoft MVP since Oct 200812 Microsoft SQL Certifications

1 of 2 MCM Test Completed (go me)

Sr. DBA for Phreesia

Page 3: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Today’s GoalsIntroduce the different kinds of indexesCommon Misconceptions about indexesDownsides to indexesIntroduce advanced index tuning techniquesQ & A

Page 4: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Today’s GoalsIntroduce the different kinds of indexesCommon Misconceptions about indexesDownsides to indexesIntroduce advanced index tuning techniquesQ & A

Page 5: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Different Kinds of IndexesFour Kinds of Indexes

ClusteredNon-clusteredFull TextXML

Page 6: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Clustered Indexes1 Clustered Index per tableContain Full Copy of row data within in the

indexUp to 16 indexed columns can be part of the

index(15 if the table contains any XML indexes)

Primary Key will by default be the Clustered Index

Must be created on the same filegroup as the table

Clustered Indexes should be as narrow as possible

While not required, they are highly recommended

Page 7: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Non-clustered IndexUp to 999 per tableUp to 16 indexed columns in the indexNon-indexed columns can be included via INCLUDE

statementNon-Clustered indexes always contain the clustered

index columns (when table has a clustered index)When table is a heap, the Row ID is stored in every

non-clustered index.Can be created on any filegroup within the databaseCan be filtered indexes to include fewer rows in the

index.

Page 8: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Differences between unique and non-unique indexesNon-Unique indexes have an extra column

called the uniqueifier which ensures that values within the index are unique.

Uniqueifier is only used for rows which are not unique. EmpId Uniqufier

1  

2  

3  

4 0

4 1

5  

6  

7 0

7 1

8  

Page 9: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Full Text IndexesNot accessed via normal SELECT statementsRequire use of a predicate:

CONTAINSCONTAINSTABLEFREETEXTFREETEXTTABLE

Can be used to search binary values (doc, docx, xls, pdf) stored within the database.

Natural Language Search

Page 10: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Full Text Indexes (SQL 2005 and below)Created and managed outside of the database

via Microsoft Search ServiceBacked up with the database (starting in SQL

2005)Searches entire index and returns all

matches, which you then filter against your normal table to return correct set of rows.

Page 11: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Full Text Indexes (SQL 2008 and up)Now stored within the databaseCommand is still parsed via MS Search

service, but looking is done nativelyFull text search now only searches the

required subset of rows

Page 12: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

XML IndexesAllows you to index specific nodes of the XML

document249 XML Indexes pre tableRequires a Clustered Index on the tableEach xml column can have a single primary

XML index and multiple secondary XML indexes

XML Indexes can only be created on a single XML Column

Page 13: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Today’s GoalsIntroduce the different kinds of indexesCommon Misconceptions about indexesDownsides to indexesIntroduce advanced index tuning techniquesQ & A

Page 14: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Common Misconceptions about indexesIndexes don’t require maintenanceIf I create one index for each column in my

where clause I’ll be fineThe table is sorted based on the order of the

Clustered IndexClustered Indexes are required

Page 15: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Today’s GoalsIntroduce the different kinds of indexesCommon Misconceptions about indexesDownsides to indexesIntroduce advanced index tuning techniquesQ & A

Page 16: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Downsides to indexesIndexes take up space

On large complex databases the indexes can take up more space than the table

Data is duplicated in each index which contains the column

Indexes slow down insert, update, delete (especially full text indexes) statements

Using the wrong index can be slower than using no index

Encrypted data can’t be effectively indexed

Page 17: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Today’s GoalsIntroduce the different kinds of indexesCommon Misconceptions about indexesDownsides to indexesIntroduce advanced index tuning

techniquesQ & A

Page 18: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Advanced Index Tuning TechniquesFillfactor

Tells the SQL Server how much free space to leave in the leaf level pages.

PaddingTells the SQL Server to use the Fillfactor

setting to leave free space in the intermediate-level pages.

Online RebuildsData Compression

Page 19: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Using the Advanced Index Tuning TechniquesCREATE INDEX MyIndex ON dbo.MyTableON (Col1, Col5, Col3)INCLUDE (Col4, Col2)WHERE Col6 = ‘Value3’WITH (FILLFACTOR=70, PAD_INDEX=ON,

ONLINE=ON, DATA_COMPRESSION = ROW | PAGE);

Page 21: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Q & A

Page 22: Denny Cherry Manager of Information Systems mrdenny@mrdenny.com MVP, MCSA, MCDBA, MCTS, MCITP

Denny [email protected]

http://itke.techtarget.com/sql-server/http://www.twitter.com/mrdenny

Please rate my presentation at http://speakerrate.com/mrdenny