How Can I Determine Which Tables and Indexes Are Not Being Used in My Database?
Q: I have inherited a legacy applications of which I am certain there are many unneccesary tables and indexes. I am slightly paranoid about ripping them out however as I am not 100% sure that they are not used. Is there any way that I can determine which indexes and tables are safe to drop?
A: As long as your application is running on SQL Server 2005+, you have access to a Dynamic Management View (DMV) that displays exactlky this information. Querying from sys.dm_db_index_usage_stats will tell you how many times a given index/table has been scanned, seeked, looked up, updated, and even gives you the last date when an operation happened. Browsing through this DMV will allow you to determine with confidence any index/table that has never been touched (since SQL Server was last restarted). There are many exampes of some elegant queries that people have written that can be found by searching the DMV name but its a fairly straight forward one to understand.
One got-chya, the data in sys.dm_db_index_usage_stats will show tables/indexes across all databases where sys.indexes only shows you data from the database you are in. This creates some challenging hurdles if you are trying to join on those tables to lookup the index name. It’s best to keep it simple and filter dm_db_index_usage_stats to only show you data from the database you are in.
A fairly simple example with explanation can be found here.