Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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:

SELECT *
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:

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

Why isn't this query returning data?

Added

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

SELECT *
FROM sys.indexes

SELECT *
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.

share|improve this question
    
What db user is running the query, and what grants does that user have? –  Joe Frambach 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. –  Martin Smith Jun 17 '13 at 14:29
add comment

migrated from stackoverflow.com Jun 17 '13 at 15:00

This question came from our site for professional and enthusiast programmers.

1 Answer

up vote 8 down vote accepted

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

SELECT *
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() 
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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