- Community Pick
- Experts Exchange Approved
The two in particular that we are going to discuss are sys.dm_db_index_usage_stat
Recently, I was involved in a discussion as to what some of these views really do, and thought I might share some of that discussion in a relatively light hearted way. Considerably more light hearted than the aforementioned discussion...
Now, despite being able to find a lot of discussions about these views, these two in particular can cause confusion. Mainly due to the terminology used in naming these views. Notably, the words "index" and "usage" are a little ambiguous. You would think that those names imply what one would reasonably expect to get if using them, and, when you might use them. Think again.
At first glance they give information which doesn't quite make sense when compared side by side. One view says "this" many reads and the other says "that". One view has an index entry for a table and the other doesn't. Both have entries for tables that don't have indexes. So, what is going on ? Why are they so different ? Which happens to be the wrong question, the fact that the information doesn't match is a very important differentiator. Question should be "How can I take advantage of those difference".
The differences in the information from those views actually tell us important pieces of the index puzzle. Collectively, that information is very telling about your index designs (which will be a different post).
So, lets have a look at those views :
SYS.DM_DB_INDEX_USAGE_STA
SYS.DM_DB_INDEX_OPERATION
Now there is a third very interesting DMV sys.dm_db_index_physical_s
Because operational stats more accurately reflects actual usage, it is easy to get these two confused. The way I remember how to use them is by their name (duh), but, with a small twist, as in "is my index useful" then usage, "is my index operating efficiently" then operational.
To demonstrate the differences, let's create a couple of test tables :
But first, to use the queries below it has to be in sequence, so open a new query window and paste the code. Highlight the code until you get to the next "go" and then run the queries (keyboard short cut F5).
When looking at the results, you will need to scroll right to see the columns being referred to (namely singleton_lookup_count and range_scan_count in the "operational" view). So please browse and scroll when we look at the results from the DMV's.
Because our focus is on Indexes, then our Heap Table has served it's purpose (i.e., non-indexed tables show up).
So, we wont worry about the heap table any more.
Few pointers if you want to continue to play with the Heap Table (please do)
1) Heaps will have an index number of 0 and no name.
2) the column stats are a bit different because they have no indexes
3) Usage stats are well populated regardless of being Heap.
4) Look in operational stats for the leaf_%_count columns, and row_lock% etc.
5) the lesson was that even a table without indexes is getting in those views.
6) some of the assertions or assumptions made below will apply, but take care.
7) Heaps with lots of writes over time might become fragmented.
8) Heaps with lots of reads might (especially if scans) benefit from indexing.
So, we say farewell to the humble heap and turn our attention to our index table
Now we could go on and start to examine all the various activities like updates and deletes, and seeks, and more. But, I am not going to. Why? Because we set out to show the differences in those DMV views, and to show that they are not restricted to Indexes. Besides, explaining what all those counts actually mean, and answering a few of those "why" questions, will make for a good Articles for next time !
Instead, let us now consider what we have seen and can summarise from the above activity.
SYS.DM_DB_INDEX_USAGE_STA
- It is cleared when service starts / reboots etc. So keep that firmly in mind.
- Good to help identify if an index is used - no entry unless it is.
- Good to help identify if a table is used - no entry unless it is.
- Has dates to help identify when indexes and tables were last used.
- Has counts to help identify frequency of use.
- If machine has been up for the entire period that encapsulates all usage, we can highlight unused indexes and consider their removal.
- Can look at read / write ratios to determine general activity (mainly read, written etc).
SYS.DM_DB_INDEX_OPERATION
- Exists when table / indexes are created
- Shows volumes of activity (plan, rows, pages), not just an instance of a statement.
- Far more detailed to help identify what type of activity
- Shows row_lock counters indicating lock contention
- Shows latch counters showing lower level IO contention
- Can measure the cost of having an index, or missing one (scans)
- Can start to ascertain the cost of an index by looking at volume of inserts over the type of activity (e.g. scans, but lack of read in the usage_stats).
If considering changing indexes, then you really do need to consider long and hard before you do. While some will stand out fairly clearly, such as written, but not read, you do need to consider the overall impact, and should be profiled, run on a test database, run with the actual execution plans, and consider what the Query Optimiser might be doing if the index is not there, or, the cost of an index if you were to add it. Over indexing can be counter productive.
Armed with the above information, looking at DMV's activity, you can soon work out where possible areas of further inspection are.
Now go read books on line for a more detailed understanding of what each column is saying...
http://msdn.microsoft.com/
http://msdn.microsoft.com/
Also, you are encouraged to go back and run all the queries again. This time, stop and look at the actual execution plan for the Inserts and Select from our indexed table. The actual execution plan will show you the Clustered Seek, Scans etc. The numbers will make more sense to you. Additionally, there are various postings through the Internet which delight in their technical dexterity as to what each column means.