sqlschool.gr logo

articles

Articles of SQLschool.gr Team

SQL Server Query for Index Recommendation

Antonios Chatzipavlis
Monday 07 September 2009

Το παρακάτω query είναι πάρα πολύ καλό!!!.

Το ξαναβρήκα τυχαία μπροστά μου καθώς σκάλιζα τα διάφορα scripts που έχω στην βιβλιοθήκη μου. Αν θυμάμαι καλά, γιατί έχει περάσει καιρός, το έχει φτιάξει ένα παλικάρι από την μαμά εταιρία, νομίζω ότι είναι ο Swapnil Bajaj.

Κάνει φοβερή εκμετάλευση των dymamic views (DMV)

  • sys.dm_db_missing_index_group_stats,
  • sys.dm_db_missing_index_groups,
  • sys.dm_db_missing_index_details.

Θα σας συνιστούσα να διαβάσετε το documentation στα BOL του SQL Server γιατί έχουν κάποιες "ιδιοτροπίες" οι συγκεκριμένες DMV.

Επίσης παράγει και το script για την δημιουργία του index ;).

Enjoy it.

Code Snippet
  1. SELECT  
  2. sys.objects.name,
  3. (avg_total_user_cost * avg_user_impact) * (user_seeks + user_scans) as Impact,  
  4. cast('CREATE NONCLUSTERED INDEX ~NewNameHere~ ON ' + sys.objects.name
  5. + ' ( ' + mid.equality_columns +
  6. CASE WHEN mid.inequality_columns IS NULL THEN '' ELSE
  7. CASE WHEN mid.equality_columns IS NULL THEN ''
  8. ELSE ',' END +     mid.inequality_columns END + ' ) ' +
  9. CASE WHEN mid.included_columns IS NULL THEN ''
  10. ELSE 'INCLUDE (' + mid.included_columns + ')' END + ';' as xml) AS CreateIndexStatement,
  11. mid.equality_columns, mid.inequality_columns, mid.included_columns
  12. FROM sys.dm_db_missing_index_group_stats AS migs
  13.     INNER JOIN sys.dm_db_missing_index_groups AS mig ON migs.group_handle = mig.index_group_handle
  14.     INNER JOIN sys.dm_db_missing_index_details AS mid ON mig.index_handle = mid.index_handle
  15.     INNER JOIN sys.objects WITH (nolock) ON mid.object_id = sys.objects.object_id
  16. WHERE (migs.group_handle IN(SELECT TOP (500) group_handle
  17.                             FROM sys.dm_db_missing_index_group_stats WITH (nolock)
  18.                             ORDER BY (avg_total_user_cost * avg_user_impact) * (user_seeks + user_scans) DESC))
  19. ORDER BY 2 DESC

Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios is a Data Solutions Consultant and Trainer. He has been working in IT since 1988. In his career, he has worked as senior developer, IT Manager, Solutions Architect and IT Consultant. Since 1995 he has been devoted on new technologies and software development tools, mainly by Microsoft, either by training company staff and colleagues or assisting them in design, development and implementation as a consultant or chief developer. He has focused in Databases and Data Science since 1995. He specialized in Microsoft SQL Server since version 6.0 in areas like SQL Server Internals, Database Design and Development, Business Intelligence and in 2010 he has started working with Azure Data Platform, NoSQL databases, Big Data Technologies and Machine Learning. He is an active member of many IT communities in Greece, answering colleagues' questions and writing articles in his web site. He is the owner of SQLschool.gr which is a community portal with a lot of information about Microsoft SQL Server. He has been a Microsoft Certified Trainer (MCT) since 2000. Microsoft honored him as MVP on Data Platform due to his activities in SQL Server since 2010. He holds a large number of Microsoft Certifications and Microsoft SQL Server Certifications since version 6.5.

Episode

First look: SQL Database in Microsoft Fabric

image

More Episodes...

Tip

What's New in SQL Server 2022 - Episodes

More Tips...

Become a member

If you want to receive updates from us become a member to our community.

Connect

Explore

Learn


sqlschool.gr © 2010-2024 All rights reserved

This site uses cookies for operational and analytics purposes only. By continuing to browse this site, you agree to their use.