We Don't Need Roads: A Developers Look Into SQL Server Indexes

  • View
    272

  • Download
    0

Embed Size (px)

Transcript

  • 1. WE DON'T NEED ROADS: A DEVELOPER'S LOOK INTO SQL SERVER INDEXESRICHIE RUMPJORRISS LLC @JORRISS HTTP://JORRISS.NET

2. WHO IS THIS GUY? 3. WHAT WERE GOING TO TALK ABOUT Why indexes? How indexes are used DB Storage Basics Index Structure Clustered indexes vs non-clustered Includes, Fill factor and covering 4. A TRAGEDY 5. Great Scott! 6. WHY ARE INDEXES IMPORTANT?An index makes the query fast.Reference: http://use-the-index-luke.com/sql/anatomy 7. WHY ARE INDEXES IMPORTANT?An index makes your query fast.Reference: http://use-the-index-luke.com/sql/anatomy 8. HOW ARE INDEXES USED BY SQL SERVER?SQL Server creates a plan to determine whats the best way to execute the query. (Unlike some developers) 9. EXECUTION PLANSDemo 10. TYPES OF INDEXES Clustered Nonclustered Heaps Column Store XML Spatial Full-Text 11. HOW DO INDEXES WORK?In order to understand the magic of indexes we need to understand how SQL Server stores data. 12. HOW SQL SERVER STORES INFORMATION Page HeaderPages Offset ArrayUsed to store just about everything.Page Header: Metadata about the page. (Page Number, Type, etc.)RecordsPage Size: 8KOffset Array: Pointers to where each row begins. 13. HOW SQL SERVER STORES INFORMATION Page HeaderPage HeaderPage HeaderRow 1Row 1Row 1Row 2Row 2Row 3121321 14. HOW SQL SERVER STORES INFORMATION Page HeaderPage HeaderRow 1Row 6Row 11Row 2Row 7Row 12Row 3Row 8Row 13Row 4Row 9Row 14Row 55Page HeaderRow 10Row 1543211098761514131211 15. HOW INDEXES ARE STORED B-Tree or Balanced Tree The tree is made up of Pages 16. HOW ARE INDEXES STORED 1400Root LevelIntermediate Level12001100150101 20051 100101 150201 400201 300151 200201250Leaf Level301 400251 300301 350351 400 17. HOW ARE INDEXES STOREDCREATE NONCLUSTERED INDEX IX_Posts_OwnerUserId ON Posts (OwnerUserId) SELECT UserId, Title FROM Posts WHERE UserId = 16014001200201 4001100150101 20051 100101 150201 300151 200201250301 400251 300301 350351 400 18. CLUSTERED INDEXES What is a table, Alex? Only one per table! Can the key be more than one column. Must be unique (SQL Server will help if it isnt. Cant be more than 900 bytes combined 19. CLUSTERED INDEXES 1400Root LevelIntermediate Level12001100150101 20051 100101 150201 400201 300151 200201250301 400251 300Leaf Level THIS IS SPARTA THE DATA!301 350351 400 20. NONCLUSTERED INDEXES Subset of data optimized for searching. Key can be more than one column Cant be more than 900 bytes combined Doesnt have to be unique. Max of 999 per table. (If I find 999 indexes on your table I will find you) 21. NONCLUSTERED INDEXES 1400Root LevelIntermediate Level12001100150101 20051 100101 150201 400201 300151 200201250301 400251 300Leaf Level Key Data and Included columns.301 350351 400 22. CLUSTERED AND NONCLUSTERED INDEXESDemo 23. DEMO REVIEW Key lookups are slow. Use Includes to combat. Use columns in the index key if you intend on searching them. Index order matters! Suggested indexes may not be the best way to go. 24. SORRY, YOUR PAGE IS IN ANOTHER CASTLE Page HeaderPage HeaderId: 2Id: 6Id: 3 Id: 7 Id: 12Id: 1454321321 25. FILL FACTOR Minimizes page splits by leaving empty space in the page. Is a number 0 to 100. A value of 80 means that 80% of the page will be filled with data. 0 and 100 both mean that 100% of the page will be filled with data. Only applies to leaf pages 26. DISADVANTAGES OF INDEXES Increases storage. Creating duplicate copies of data. Insert, Updates and Deletes are slower. The DB has to delete data from the table and all of the related indexes. Additional maintenance 27. BONUS! ENTITY FRAMEWORK!Demo 28. REFERENCES 29. THANK YOU Richie Rump @Jorriss http://jorriss.net http://dotnetmiami.comhttp://statisticsioparser.com