简体   繁体   中英

Querying dm_db_index_usage_stats returns empty row

I have a user telling me that some data in our app has changed.

Using this example , I ran the following query on the table that populates the data in the app:

SELECT OBJECT_NAME(OBJECT_ID) AS TableName,
 last_user_update,*
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'Mydatabase')
AND OBJECT_ID=OBJECT_ID('mytable')

I don't get any rows back, so it seems there's no history of the data in the table as having been updated?

I ran

SELECT OBJECT_NAME(OBJECT_ID) AS TableName,
 last_user_update,*
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'Mydatabase')

and I got back 13 rows, several of them had null for the TableName though.

The first point:

The counters are initialized to empty whenever the SQL Server (MSSQLSERVER) service is started. In addition, 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.

So if your server was restarted or something you don't have statistics.

The second point:

Use database_id parameter when you call OBJECT_NAME function:

SELECT OBJECT_NAME(OBJECT_ID, DB_ID( 'Mydatabase')) AS TableName,
 last_user_update,*
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'Mydatabase')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM