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?
If I run the below queries, I receive data from both of these
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.