Whenever I’m discussing index maintenance, and specifically fragmentation, I always make a point of saying ‘Make sure the index is being used before doing anything about fragmentation’.
If an index isn’t being used very much, but has very low page density (lots of free space in the index pages), then it will be occupying a lot more disk space than it could do and it may be worth compacting (with a rebuild or a defrag) to get that disk space back. However, usually there’s not much point spending resources to remove any kind of fragmentation when an index isn’t being used. This is especially true of those people who rebuild all indexes every night or every week.
You could even go so far as to say if a non-clustered index isn’t being used, why is it there at all? Extra non-clustered indexes drag down performance in a number of ways. Consider a non-clustered index called IX_MyNCIndex on the table MyTable:
That’s a significant amount of extra IOs to maintain each extraneous non-clustered index.
So, how can you tell if an index is being used? There are a few different ways in SQL Server 2005 – the one I want to discuss in this post is using the sys.dm_db_index_usage_stats DMV.
This DMV exposes the information that is tracked about index usage (as the name suggests). It does not generate any information itself; it just returns info from a cache inside SQL Server. This cache is empty when the server instance starts, and is not persisted across instance restarts. All cache entries for indexes in a database are removed when that database is closed. So, the cache tracks usage information about indexes since the database they are part of was last opened (either manually or as part of instance start-up).
The cache tracks the following info for each index (for user queries and system queries):
· The number of times it was used in a seek operation (either looking up a single row, or doing a range scan) along with the time of the last seek.
· The number of times it was used in a scan operation (e.g. a select * operation) along with the time of the last scan
· The number of times it was used in a lookup operation (this means a bookmark lookup – where a non-clustered index does not fully cover a query and additional columns must be retrieved from the base table row) along with the time of the last lookup.
· The number of times it was used in an update operation (this counts inserts, updates, and deletes) along with the time of the last update.
Let’s have a look at its use.
Unless you've just re-started your instance, you'll see a bunch of output from this, representing all index activity since the instance/databases started. If you're interested in whether an index is being used, you can filter the output. Let's focus in on a particular table - AdventureWorks.Person.Address.
You'll probably see nothing in the output, unless you've been playing around with that table. Let's force the clustered index on that table to be used, and look at the DMV output again.
Now there's a single row, showing a scan on the clustered index. Let's do something else.
And there's another row, showing a seek in one of the table's non-clustered indexes.
So, its easy to look at the index usage for particular tables and indexes. But how can you monitor this over time? This is easy too - let's see how.
First we need to create our own table to store snapshots of the DMV output.
Next we need to take a baseline snapshot of the DMV output.
And now simulate a few operations and take another snapshot of the DMV:
And look at the filtered contents of our snapshot table:
You should see four rows - two from the baseline snapshot and two from the final snapshot. If you ran just the statements above, you'll see that the user_scans count for the clustered index has increased by two, and the user_seeks count for the non-clustered index has increased by one.
So this is a pretty simple example of how you can track index usage. By putting something like this into a regularly run script you can tell which indexes aren't being used and could be candidates for less-regular index maintenance or removal altogether.
Let me know how you get on.
(Btw - still looking for more people to fill out my VLDB Maintenance survey...)
On day-to-day basis a DBA might come across with the issues on the fragmentation on the database, it
Great information and was useful for me.
BTW, is there any equivalent code for SQL server 2000? I tried searching for the same,but wasn't successful.
No - unfortunately there's no equivalent in SQL Server 2000. You'd need to profile a workload, run it through the Index Tuning Wizard and see which indexes ITW recommends dropping. Nowhere near as good.
One evening last week I sat down with Kimberly for 5 minutes to come up with a top-of-our heads list
转载、解释一下来自SQL Server Storage Team的VLDB维护建议清单。 虽然主题是维护，但相信理解这些对于从事数据库开发的人员来说也有一定价值。
This entry is going to be a collected list of practices for index, fragmentation, statistics, and general...
I believe I understand what are user_seeks, user_scans and user_lookups in sys.dm_db_index_usage_stats, but do not really understand what are system_seeks, system_scans and system_lookups in that same DMV.
Can you give some details ?