I'm trying to obtain information on indexes. According to several websites, the below TSQL query should join information on indexes to the indexes table, returning indexes and related information. The below query is simplified to the bare join, as I'm currently trying to return data and then from there, I will return what I need:

FROM sys.indexes i
     INNER JOIN sys.dm_db_index_usage_stats s ON i.object_id = s.object_id

This query returns nothing. Yet, in several databases there are hundreds of tables that have at least one index, and according to various articles online, this query should return those indexes (because it's joining on the object ids). Also, if I try to change the query to the below query, it returns nothing as well:

    INNER JOIN DATABASENAME.sys.dm_db_index_usage_stats s ON i.object_id = s.object_id

Why isn't this query returning data?


If I run the below queries, I receive data from both of these SELECT queries:

FROM sys.indexes

FROM sys.dm_db_index_usage_stats

The problem (on the original query) is the object_id join. However, I don't know why I have objects in one table that don't exist in another.

Final: Martin had the right answer originally - even though data was being returned from the sys.dm_db_index_usage_stats table, it was on the msdb database, not the specific database I was seeking. I ran queries on tables again, and found the database id in the table. Unsure of why the sys.dm_db_index_usage_stats table didn't retain data from previous queries on that database.

  • What db user is running the query, and what grants does that user have? Jun 17 '13 at 14:09
  • @MartinSmith If I use sa, running SQL Server express on my local machine with tables holding indexes, it also returns nothing.
    – user123
    Jun 17 '13 at 14:09
  • @MartinSmith Yes, both tables return data individually.
    – user123
    Jun 17 '13 at 14:14
  • No usage stats?
    – Tony Hopkinson
    Jun 17 '13 at 14:22
  • 2
    Ah re your edit. SQL Express auto closes databases by default so you probably lose it then. Jun 17 '13 at 14:29

One possibility would be that you don't have sufficient permissions to view the metadata.

Another is that sys.dm_db_index_usage_stats only reports on indexes that have been used at least once since the server was last restarted. Also

whenever a database is detached or is shut down (for example, because AUTO_CLOSE is set to ON), all rows associated with the database are removed.

AUTO_CLOSE is the default for SQL Server Express.

You need to filter sys.dm_db_index_usage_stats by database_id to determine if it has metadata loaded for a particular database also the join should include index_id

FROM   sys.indexes i
       LEFT JOIN sys.dm_db_index_usage_stats s
         ON i.object_id = s.object_id
            AND i.index_id = s.index_id
            AND s.database_id = DB_ID() 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.