Your SlideShare is downloading. ×
0
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Indexing Strategies
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Indexing Strategies

547

Published on

This presentation discuss SQL Server\'s indexing strategies and how they are used.

This presentation discuss SQL Server\'s indexing strategies and how they are used.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
547
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
30
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Indexing Primer John LaSpada Senior SQL Architect – IBM DBA Solution Center of Excellence
  • 2. Overview <ul><li>Table Concepts </li></ul><ul><li>Table Structures & Heaps </li></ul><ul><li>Index Concepts </li></ul><ul><li>Finding the Right Balance </li></ul><ul><li>Table Scans </li></ul><ul><li>Clustered vs. Non-clustered Indexes </li></ul><ul><li>Index for Performance </li></ul>
  • 3. Table Concepts <ul><li>Base unit for data storage = table </li></ul><ul><li>Collection of unordered pages = heap </li></ul><ul><li>Table with no index = heap = exhaustive searches </li></ul><ul><li>Why exhaustive? </li></ul><ul><ul><li>Table scans, table scans -> Not Good </li></ul></ul><ul><ul><li>No indexes means no guarantee of uniqueness </li></ul></ul><ul><ul><li>No order to the data, could be more records even if you find a match on the first row of the first page (ex. where ID = 1) </li></ul></ul>
  • 4. Table Structure – Heap <ul><li>Table without a Clustered Index </li></ul><ul><li>Records are NOT ORDERED </li></ul><ul><li>No Doubly-Linked List (ordering algorithm or sequence) </li></ul><ul><li>If NO Indexes exist – a full Table Scan occurs. </li></ul>
  • 5. Index Concepts <ul><li>If a tree were data and you were looking for </li></ul><ul><li>leaves with a certain property, you would </li></ul><ul><li>have two options to find that data…. </li></ul><ul><li>1) Touch every leaf – Reading </li></ul><ul><li>each one to determine if they </li></ul><ul><li>held that property…SCAN </li></ul><ul><li>2) Root -> B ranch -> L eaves </li></ul><ul><li>… SEEK </li></ul>
  • 6. Index Concepts <ul><li>Pros </li></ul><ul><li>Can speed up access to data – more options over exhaustive table scans </li></ul><ul><li>Can guarantee uniqueness of data </li></ul><ul><li>Can offer better lock granularity </li></ul><ul><li>Generally lead to better balanced performance – when indexed appropriately </li></ul><ul><li>Cons </li></ul><ul><li>Adds overhead in INSERTs and DELETEs (update pointers), </li></ul><ul><li>add overhead in UPDATEs only when indexed column is modified </li></ul><ul><li>Add overhead in terms of Disk space </li></ul><ul><li>Add overhead in terms of Maintenance </li></ul><ul><li>(there are strategies for minimizing this) </li></ul>
  • 7. Finding a Balance <ul><li>Start with a minimal number of indexes </li></ul><ul><ul><li>Clustered Index – How base data stored (1 per/table) </li></ul></ul><ul><ul><li>Primary Key </li></ul></ul><ul><ul><li>Unique Keys </li></ul></ul><ul><li>Manually index foreign keys </li></ul><ul><ul><li>Speed up join performance </li></ul></ul><ul><li>Use ITW (Index Tuning Wizard) </li></ul><ul><li>Manually index based on either: </li></ul><ul><ul><li>Execution Plans </li></ul></ul><ul><ul><li>WHERE Clauses </li></ul></ul><ul><ul><li>Query frequency </li></ul></ul>
  • 8. Dreaded TABLE SCANS <ul><li>Caused when a search is performed on </li></ul><ul><li>a column where an index is not </li></ul><ul><li>Functions causing scans: SELECT *, OR, BETWEEN, LIKE, IN (try to stay away) </li></ul><ul><li>Utilize execution plans and the optimizers hints! </li></ul>
  • 9. <ul><li>Demo </li></ul>
  • 10. Clustered Indexes <ul><li>Clustered Index = Actual table sorted in order of the clustered key </li></ul><ul><li>Each table = 1 Clustered Index </li></ul><ul><li>Inserted new rows can either be added at the end (if sequential), or the row will have to be inserted into the correct data page, which might require a page split if there is not enough room on the page for the new row. </li></ul><ul><li>Pointers maintains the order between the pages so rows in other pages will not have to move. </li></ul>
  • 11. Clustered Index Candidates <ul><li>Identity columns are ideal… </li></ul><ul><li>Narrow small keys (int) </li></ul><ul><ul><li>Unique – Minimal overhead, data takes care of uniqueness </li></ul></ul><ul><ul><li>Improved Performance </li></ul></ul><ul><ul><li>Reduces fragmentation – improves up time! </li></ul></ul><ul><ul><li>Minimizes cache </li></ul></ul>
  • 12. Non Clustered Index <ul><li>249 Allowed, takes additional space. </li></ul><ul><li>Similar to indexes in back of books </li></ul><ul><li>Contains the indexed columns and a pointer or bookmark pointing to the actual row . </li></ul><ul><li>Think of a Google search… </li></ul>
  • 13. <ul><li>Demo 2 </li></ul>
  • 14. Hints <ul><li>Covered Indexes - All of the columns requested in the output are covered by a single index. </li></ul><ul><li>Crucial queries - Consider creating a covering index to give the query the best performance. </li></ul><ul><li>Avoid “Bookmark Lookup” in the execution plan. </li></ul><ul><li>Anytime you see &quot;Hash&quot; in your plan it means temp tables and this can be done better! </li></ul><ul><li>Use Stored Procedures whenever you can. </li></ul>
  • 15. Keeping Performance Optimal <ul><li>For the Optimizer to use indexes appropriately you must have Statistics! </li></ul><ul><ul><ul><li>Auto Update Statistics </li></ul></ul></ul><ul><ul><ul><li>Auto Create Statistics </li></ul></ul></ul><ul><li>Rebuild Index </li></ul><ul><ul><ul><li>Removes all levels of fragmentation </li></ul></ul></ul><ul><ul><ul><li>Updates statistics </li></ul></ul></ul>
  • 16. Use System Views <ul><li>SELECT </li></ul><ul><li>DISTINCT </li></ul><ul><li>sys.objects.name, sys.partitions.rows, migs.user_seeks,migs.avg_total_user_cost,migs.avg_user_impact, </li></ul><ul><li>'CREATE NONCLUSTERED INDEX <NewNameHere> ON ' + sys.objects.name + ' ( ' + mid.equality_columns + </li></ul><ul><li>CASE WHEN mid.inequality_columns IS NULL </li></ul><ul><li>THEN '' ELSE CASE WHEN mid.equality_columns IS NULL </li></ul><ul><li>THEN '' ELSE ',' END + mid.inequality_columns END + ' ) ' + CASE WHEN mid.included_columns IS NULL </li></ul><ul><li>THEN '' ELSE 'INCLUDE (' + mid.included_columns + ')' END + ' with (online =ON, maxdop = 2, sort_in_tempdb = ON ) on IndexFileGroup ;' </li></ul><ul><li>AS CreateIndexStatement , mid.equality_columns, mid.inequality_columns, </li></ul><ul><li>mid.included_columns </li></ul><ul><li>FROM </li></ul><ul><li>sys.dm_db_missing_index_group_stats AS migs INNER JOIN </li></ul><ul><li>sys.dm_db_missing_index_groups AS mig ON migs.group_handle = mig.index_group_handle INNER JOIN </li></ul><ul><li>sys.dm_db_missing_index_details AS mid ON mig.index_handle = mid.index_handle INNER JOIN </li></ul><ul><li>sys.objects WITH (nolock) ON mid.object_id = sys.objects.object_id INNER JOIN </li></ul><ul><li>sys.partitions on sys.objects.object_id = sys.partitions.object_id </li></ul><ul><li>--and sys.partitions.index_id = 1 </li></ul><ul><li>WHERE </li></ul><ul><li>migs.group_handle IN </li></ul><ul><li>(SELECT TOP (10) group_handle </li></ul><ul><li>FROM sys.dm_db_missing_index_group_stats WITH (nolock) </li></ul><ul><li>ORDER BY user_seeks DESC ) </li></ul><ul><li>order by </li></ul><ul><li>--migs.user_seeks desc </li></ul><ul><li>--migs.avg_total_user_cost desc </li></ul><ul><li>migs.avg_user_impact desc </li></ul>
  • 17. <ul><li>Demo 3 </li></ul>
  • 18. System Views cont. <ul><li>select </li></ul><ul><li>TableName=o.name, migs_Adv.index_advantage </li></ul><ul><li>, s.avg_user_impact </li></ul><ul><li>, s.avg_total_user_cost </li></ul><ul><li>, s.last_user_seek </li></ul><ul><li>,s.unique_compiles, </li></ul><ul><li>d.index_handle </li></ul><ul><li>,d.equality_columns, d.inequality_columns, d.included_columns, d.[statement] </li></ul><ul><li>from sys.dm_db_missing_index_group_stats s </li></ul><ul><li>inner join sys.dm_db_missing_index_groups g on g.index_group_handle=s.group_handle </li></ul><ul><li>inner join sys. dm_db_missing_index_details d on d.index_handle=g.index_handle </li></ul><ul><li>inner join sys.objects o on o.object_id=d.object_id </li></ul><ul><li>inner join (select user_seeks * avg_total_user_cost * (avg_user_impact * 0.01) as index_advantage, </li></ul><ul><li>migs.* from sys.dm_db_missing_index_group_stats migs) as migs_adv on migs_adv.group_handle=g.index_group_handle </li></ul><ul><li>order by migs_adv.index_advantage desc, s.avg_user_impact desc </li></ul>
  • 19. Indexing for Performance Here’s the Key <ul><li>Do not use the mindset were every column has to have an index, </li></ul><ul><li>Have a strategy! </li></ul><ul><li>Use a strategy with data to back it. </li></ul><ul><li>Utilize Server-side traces to see how your system is utilized by each client at different times of day. </li></ul><ul><li>Look at activity, data usage, and query access </li></ul><ul><ul><li>Find most used queries </li></ul></ul><ul><ul><li>Find highest duration queries </li></ul></ul><ul><ul><li>Find highest CPU queries </li></ul></ul><ul><li>Prioritize access – in terms of user queries/type </li></ul><ul><li>Minimize total indexes and find the right balance! </li></ul><ul><li>Start with only the necessary indexes and ADD from there. </li></ul>
  • 20. <ul><li>Questions </li></ul>

×