sqlschool.gr logo

articles

Articles of SQLschool.gr Team

How old are the statistics of indexes in your database?

Antonios Chatzipavlis
Monday 16 July 2012

Φαντάζομαι ότι σε όλους είναι γνωστό ότι για να εκτελεστεί ένα query στον SQL Server περνάει από τον Query Optimizer. Αυτός για να παράξει το execution plan βλέπει τα index statistics. Αυτό σημαίνει ότι όσο καλύτερα ενημερωμένα είναι αυτά τόσο καλύτερα αποτελέσματα θα δώσει ο Query Optimizer.

Πότε όμως αυτά έχουν ενημερωθεί τελευταία φορά;

Αν κάποιος θέλει να δώσει απάντηση στο ερώτημα του αυτό δεν έχει παρά να εκτελέσει το παρακάτω query στην βάση του.

Αυτό θα του επιστρέψει ένα dataset το οποίο περιέχει όλους τους indexes των πινάκων που υπάρχουν σε αυτή την βάση με έμφαση στα δύο τελευταία πεδία

  • το last_update_stats_date το οποίο δείχνει την τελευταία ημερομηνία που έχουν γίνει update τα στατιστικά στο συγκεκριμένο index
  • και το stats_age_in_days το οποίο επιστρέψει την διαφορά σε ημέρες της τρέχουσας ημερομηνίας με την τελευταία ημερομηνίας ενημέρωσης των στατιστικών.

Το επιστρεφόμενο dataset  είναι ταξινομημένο  με βάση το stats_age_in_days σε φθίνουσα σειρά τα αποτελέσματα

 

Τo Query


SELECT 
        sys.objects.name AS table_name,
        sys.indexes.name as index_name, 
        sys.indexes.type_desc as index_type, 
        stats_date(sys.indexes.object_id,sys.indexes.index_id) 
        as last_update_stats_date,
        DATEDIFF(d,stats_date(sys.indexes.object_id,sys.indexes.index_id),getdate()) 
        as stats_age_in_days
FROM  
        sys.indexes
        INNER JOIN sys.objects on sys.indexes.object_id=sys.objects.object_id
WHERE 
        sys.objects.type = 'U' 
        AND
        sys.indexes.index_id > 0 
ORDER BY 
        stats_age_in_days DESC;
GO


Enjoy it
/*antonch*/

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

Task Flows 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-2025 All rights reserved

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