Wednesday, September 28, 2016

How to get the most accessed tables from SQL Server database

While discussing with one of my clients, this question raised but I knew that there is no direct way unless we use something related to indexes. So the query is simple, all we have to do is, get the total count of all scans, seeks and lookups for particular indexes;

  1. SELECT   
  2.  t.name AS TableName,  
  3.  SUM(u.user_seeks + u.user_scans + u.user_lookups) AS NumberOfTimeAccessed  
  4. FROM sys.dm_db_index_usage_stats u  
  5.  INNER JOIN sys.tables t   
  6.   ON t.object_id = u.object_id  
  7. WHERE database_id = DB_ID()  
  8. GROUP BY t.name  
  9. ORDER BY SUM(u.user_seeks + u.user_scans + u.user_lookups) DESC  

Is this 100% accurate? As per my understanding, this is not 100% accurate. You might get larger numbers when you have multiple indexes but if you all want is to know which tables get more hit, this does a reasonable job.

Does this capture the tables that are based on heap structure? Yes of course, still you see the count on heap tables too.

No comments: