The Art Of Indexing


Published on

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Before we jump directly into applying indexes and tuning queries, let’s take a look at the types of indexes that are available and when you might want to use them.
  • As you can see, there are several types of Indexes, but we will be focusing on Clustered and Non Clustered Indexes and the relationship they have with each other.
  • I like to think of Unique as an attribute of an index. Both Clustered and Non Clustered Indexes can be Unique. Unique indexes have some hidden benefits the query optimizer can use. For example, if you are performing a SELECT DISTINCT on a column that has a Unique Index, SQL Server knows the values are already distinct and doesn’t have to do a SORT operation to weed out records.Books Online even has Non Clustered Indexes with INCLUDE as a type of index, but to me, INCLUDE is really not a type of index, it is just a way for you to get more bang for your buck out of Non Clustered indexes.
  • Filtered Indexes where introduced in SQL Server 2008 and are a good way to create that are less expensive to maintain. Filtered Indexes are an extension of Non Clustered Indexes and allow you to create an index on a subset of data. Filtered Indexes basically allow you to add a WHERE clause to a Non Clustered Index.
  • Finally, Full-Text, Spatial, and XML indexes are more specialized indexes that are only used when called for in certain scenarios.
  • If you notice, they have increased the maximum indexes per table from 250 to 1000. This is Inclusive of the Clustered Index, meaning you can have 1 Clustered Index and 249 Non Clustered Indexes in 2005 and 999 Non Clustered Indexes in 2008.The increase has a lot to do with the ability to add Filtered Indexes in 2008. You can create several different indexes on the same column with different ranges in each. Although in most circumstances, I wouldn’t even want to come close to these numbers because indexes have to be maintained and can cause negative performance impacts.To help reduce these performance impacts you can only create an index with 16 columns or 900 bytes, whichever comes first.
  • Identity columns have all of the qualities that make a good Clustered Index. So, let’s take a look why.The Clustered Index is duplicated across all of your Non Clustered Indexes so you want to make it as efficient as possible.
  • Having a Narrow Index means less data will be duplicated and you will be able to fit more data per page. Meaning Less I/O to read through your Indexes. This also leads to smaller databases, backups, and index maintenance times.
  • So why does Unique matter? Clustered Indexes are not required to be unique, but if you add values to your clustered index that are not unique, SQL Server will add a 4 byte internal identifier called a uniquifier. This kind of goes against our keeping the index narrow thing.
  • You don’t want to change the values in your Clustered Index for a couple of reasons. First of all, since the values of your Clustered Index are contained in your Non Clustered Indexes, every time you update your Clustered Index, all of your Non Clustered Indexes have to be updated. Secondly, the pointers in the Clustered Index have to be maintained. If the Clustered Index Key changes, the record has to change locations causing fragmentation.
  • Finally, if the Clustered Index is increasing, data is always added to the end of the Index. This is good because if you insert records in the middle of your table, SQL Server has to make room for the records causing page splits and page splits lead to fragmented indexes.
  • That leaves the job of the Non Clustered Index to cover all of the other commonly used queries that aren’t covered by the Clustered Index and if they can’t completely cover the query, they can at least efficiently find the record and link back to the Clustered Index to get any other information the query may need.
  • USE AdventureWorksGO--CleanupIF EXISTS (SELECT name FROM sysindexes WHERE name = 'ix_PersonContact_LastName') DROP INDEX ix_PersonContact_LastName ON Person.Contact--SeekSELECT Phone, EmailAddressFROM Person.ContactWHERE ContactID = 42--ScanSELECT Phone, EmailAddressFROM Person.ContactWHERE LastName = 'Wruck'--**********Lookup*********************CREATE NONCLUSTERED INDEX ix_PersonContact_LastNameON Person.Contact (LastName)SELECT Phone, EmailAddressFROM Person.ContactWHERE LastName = 'Wruck'--***********Include (Covering)***********CREATE INDEX ix_PersonContact_LastNameON Person.Contact (LastName) INCLUDE(Phone, EmailAddress) WITH (DROP_EXISTING = ON)SELECT Phone, EmailAddressFROM Person.ContactWHERE LastName = 'Wruck'--***********Hint************************SELECT Phone, EmailAddressFROM Person.Contact WITH (INDEX(PK_Contact_ContactID))WHERE LastName = 'Wruck'SELECT Phone, EmailAddressFROM Person.ContactWHERE LastName = 'Wruck'--***************************************
  • Select * is like developers throwing ninja stars at your server.Select * basically means that your query will ALWAYS have to do a lookup on the Clustered Index unless you add every column in the table to the Non-Clustered Index (which is generally not a very good idea).
  • DMVs give you a better perspective of your entire systemExpose information about your server/databaseFor example, every time SQL Server thinks it needs an index, it is recorded or every time a index is used (scan, seek, lookup, update)Stats reset whenever SQL is restartedMay want to collect information to analyze over time
  • Always look to make sure you are not going to create overlapping or duplicate indexes.You may be able to alter an existing index by simply adding an extra column to the INCLUDE.Consolidate Indexes whenever where you can.
  • The Art Of Indexing

    1. 1. The Art ofIndexing<br /><br />Ken Simmons<br />
    2. 2. Who is this guy?<br /><ul><li>DBA
    3. 3. Developer
    4. 4. Author
    5. 5. SQL Server MVP
    6. 6. IT Industry for 10+ years</li></li></ul><li>Objectives<br /><ul><li>Introduce the different kinds of indexes and index options along with best practices for using them.
    7. 7. Provide strategies for creating indexes by taking advantage of DMVs and the Plan Cache.
    8. 8. Cover the various ways indexes can degrade performance along with options for finding and removing them.</li></li></ul><li><br />
    9. 9.<br />
    10. 10. The BoringTechnical Stuff<br /> <br />
    11. 11. Types of Indexes<br /><ul><li>Clustered
    12. 12. Non Clustered
    13. 13. Unique
    14. 14. Filtered
    15. 15. Full-Text
    16. 16. Spatial
    17. 17. XML</li></ul><br />
    18. 18. Types of Indexes<br /><ul><li>Clustered
    19. 19. Non Clustered
    20. 20. Unique
    21. 21. Filtered
    22. 22. Full-Text
    23. 23. Spatial
    24. 24. XML</li></ul><br />
    25. 25. Types of Indexes<br /><ul><li>Clustered
    26. 26. Non Clustered
    27. 27. Unique
    28. 28. Filtered
    29. 29. Full-Text
    30. 30. Spatial
    31. 31. XML</li></ul><br />
    32. 32. Types of Indexes<br /><ul><li>Clustered
    33. 33. Non Clustered
    34. 34. Unique
    35. 35. Filtered
    36. 36. Full-Text
    37. 37. Spatial
    38. 38. XML</li></ul><br />
    39. 39. Regulations<br /><ul><li>1 Clustered Index
    40. 40. 249 Non Clustered (2005)
    41. 41. 999 Non Clustered (2008)
    42. 42. 16 Columns or 900 Bytes</li></ul><br />
    43. 43. Characteristics<br /><ul><li>Clustered Index
    44. 44. Narrow
    45. 45. Unique
    46. 46. Static
    47. 47. Increasing</li></ul><br />
    48. 48. Characteristics<br /><ul><li>Clustered Index
    49. 49. Narrow
    50. 50. Less Data Duplicated
    51. 51. Smaller…
    52. 52. Databases
    53. 53. Backups
    54. 54. Maintenance times
    55. 55. Unique
    56. 56. Static
    57. 57. Increasing</li></ul><br />
    58. 58. Characteristics<br /><ul><li>Clustered Index
    59. 59. Narrow
    60. 60. Unique
    61. 61. Not Required
    62. 62. 4 Byte Uniquifier
    63. 63. Narrow Remember?
    64. 64. Static
    65. 65. Increasing</li></ul><br />
    66. 66. Characteristics<br /><ul><li>Clustered Index
    67. 67. Narrow
    68. 68. Unique
    69. 69. Static
    70. 70. Changes Cause…
    71. 71. Additional Overhead for Non Clustered Indexes
    72. 72. Clustered Index Fragmentation
    73. 73. Increasing</li></ul><br />
    74. 74. Characteristics<br /><ul><li>Clustered Index
    75. 75. Narrow
    76. 76. Unique
    77. 77. Static
    78. 78. Increasing
    79. 79. Data is added to the end of the Index
    80. 80. Reduces Page Splits and Fragmentation </li></ul><br />
    81. 81. Characteristics<br /><ul><li>Non Clustered Index
    82. 82. Cover Common Queries
    83. 83. Link to Clustered Index
    84. 84. Lookup</li></ul><br />
    85. 85. The Cool Stuff<br /><br />
    86. 86. Demo <br /><br />
    87. 87. SELECT<br /><br />
    88. 88.<br />
    89. 89. What about the thousands of other queries that run on my server?<br /><br />
    90. 90. DMVs Provide a<br />Better<br />Perspective<br /><ul><li>Index Usage
    91. 91. Unused Indexes
    92. 92. Expensive To Maintain
    93. 93. Missing Indexes
    94. 94. No Query Information
    95. 95. Plan Cache
    96. 96. Query
    97. 97. Execution Plan
    98. 98. Missing Index Information</li></ul><br />
    99. 99. Demo<br /><br />
    100. 100. Intelligent<br />Make<br />Decisions<br /><br />
    101. 101. Demo<br /><br />
    102. 102. Review<br /><ul><li>Clustered Indexes
    103. 103. Unique, Narrow, Static, Increasing
    104. 104. Non-Clustered Indexes
    105. 105. Cover Queries Not Covered By The Clustered Index
    106. 106. Use INCLUDE To Cover Non-Key Columns
    107. 107. Use DMVs To Get A Better Perspective
    108. 108. Unused Indexes, Missing Indexes, Cached Plans
    109. 109. Make Intelligent Decisions When Applying Indexes
    110. 110. Don’t Apply Indexes Based On Recommendations </li></li></ul><li><br />
    111. 111. Contact Info<br />Blog<br /><br />Email<br /><br />Twitter@KenSimmons<br /><br />LinkedIN<br /><br />