NOTE: I’ve released other versions of sp_helpindex since this post. Check out the category: sp_helpindex rewrites for all of the options!
OK, I first blogged about re-writing sp_helpindex here as sp_helpindex2 (April 2008). Shortly thereafter (Aug 2008), a reader found a bug and when I went digging, so did I (a couple of others). I did a few tweaks and the version that’s stuck for the past couple of years is in this blog post: Updates (fixes) to sp_helpindex2.
However, as much as I’ve used that – I’ve always been frustrated by its output. And, so, over the months+, I wrote sp_helpindex3 (this solely added the index_id as the first column and then ordered the output; I’ve always hated the weird way in which sp_helpindex/2 ordered the output). Then, I started writing sp_helpindex4 which listed the clustering key columns with each index so that I could more easily identify columns on large tables without a lot of scrolling. Ultimately, I didn’t like this because the clustering key (or some of the columns in a multi-column clustering key – might already be in the nonclustered index and so I still had to figure things out more manually). And, so I finally sat down and created what’s jokingly being called sp_helpindex8.
Geeky note: Er, you might wonder with how many geeky SQL people I’ve joked about sp_helpindex8 but in fact, in my last few events (both onsite with a couple of customers as well as when teaching the Indexing Strategies and Performance Tuning portion of the Microsoft Certified Masters – SQL Server course (two weeks ago)), I’ve actually joked – and promised – that I’d have this blogged before this particular 3 week MCM rotation ends. And, well, that’s this week. So, I’ve actually joked with quite a few folks on this and I’m close to the end of time on blogging it. So, here I am – sitting in a hotel room in Houston, after having presented at the Houston Area User Group (7-9pm) (yes, with Paul) and after having been onsite all day with a customer – blogging this darn thing. I just have to get it out there. ;-)
Having said that – I’m pretty happy with this version but it was pretty painful to write. And, before I get into what it does – and why I wanted this version – let me explain what I didn’t do and why. There are a lot of other things that could be added. And, quite a few folks have said – why don’t you do this or why don’t you do that… Specific suggestions have been made such as adding index usage stats (from the current state shown by the DMV) or add index operational stats (to show if an index is suffering from latch IO waits, for example) or add index physical stats (to show if an index is fragmented), etc. And while many of these suggestions would make for lots of interesting uses of this output, I’ve stayed away from these. Why? Some are expensive to run – such as physical stats – even a limited scan can be painful on a really large table. Or, the results might not be able to be truly trusted – such as with index_operational_stats. Why? Because the information from that DMV might only cover a short period of time. The information help by them is cleared whenever SQL Server is restarted (or if a database is taken offline or restored, etc.). So, when were those usage or operational stats last cleared? Moreover, is this single “look” at the DMV information really the best on which to base our decisions (even just in general).
In the end, I think that physical stats, operational stats and even usage stats – should be used wisely and carefully (definitely used but not by this). In fact, we often recommend that the best way to use these is by studying them over time and then analyzing their trends not just the point in time values. And, there are LOTS of posts that can help you with understanding this information; here’s just a couple to get you started:
- Craig Freedman’s (blog) post: What is the difference between sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats?
- Paul’s blog: Indexes From Every Angle: How can you tell if an index is being used?
- Brent Ozar’s (blog | twitter) SQLServerPedia post + video: http://sqlserverpedia.com/wiki/Find_Indexes_Not_In_Use
So, having said that – I’ve kept my sp_helpindex8 more pure ;-). What I want to see is EXACTLY what’s stored in that index. In fact, this has always been my main goal. The first version (sp_helpindex2) added INCLUDED columns (2005+) as well as filter definitions (2008) to the output. So, what was missing? The added columns that SQL Server might have to add if a nonclustered is nonunique and/or when the clustering key is nonunique. The reason this is so important to me is that there are many things coming together and recommending indexes (DTA, the missing index DMVs, application tools, etc.) and while many of these tools are solely there to “help” us get better performance and “help” us create more effective indexing strategies – these tools sometimes recommend the same index over and over again (see Paul’s recent blog post: Missing index DMVs bug that could cost your sanity… about the bug in the missing index DMVs) OR the tools might recommend similar indexes to what we already have.
Additionally, the output from sp_helpindex/2 only shows the columns that are EXPLICITLY defined in the key and in the leaf (using INCLUDE) but they don’t show what SQL adds for you. Most folks know that SQL Server adds the clustering key to the nonclustered indexes and when you have a simple single-column clustering key it’s not difficult to know that this IS “there” in your index even though it doesn’t show up. But, even with that, I think there are still a few things that are lesser known. For example, if your nonclustered index is nonunique – did you know that the clustering key is not only added to the leaf level of the index (like INCLUDE does) but that the clustering key is added into the tree (as part of the key and ordering?). And, if have one index that lists this clustering key explicitly and another that doesn’t – are you actually going to be able to easily tell that these are in fact the SAME. Ultimately, I want you to be able to better recognize what’s REALLY in that index. What you might end up seeing is that two indexes are more similar than you first thought and as a result – maybe you can drop one of them. Or, you might be able to combine them to reduce your overall indexes (I call this index consolidation). And, all of this becomes significantly more challenging when you have more complex clustering keys (and, yes, they do exist!). The other thing I did is delimit the columns and index names properly using brackets. This is also nice because you might have identifiers that make the output of sp_helpindex/2 hard to read. This should help. However, I do have to admit that you probably have other problems if your index/column names have commas, spaces or brackets in them. But, this should still help/work.
And, that’s why I wrote “sp_helpindex8” it’s a MUCH better version of sp_helpindex but it requires a few other components. And, it was a lot more challenging to write. Basically, this information is more internal to SQL Server and not directly exposed anywhere. So, we had to do a bit of digging. Paul started the process (thanks!) but, I ended up spending about 5 hours getting it exactly the way I wanted it. And, I also ended up breaking it up into two parts: one sp that gets column definitions and the other that builds the final output. And, I’ve actually named it with a longer name (sorry!). But, I rarely type in the name anyway. What I typically do is go to SSMS, Tools | Options, then under Environment | Keyboard, I set the keyboard shortcut for Ctrl+F1 to my sp_SQLskills_SQL2008_helpindex.
Now, when I want to see the true defintions of indexes on a table – I highlight the tablename and hit Ctrl+F1. But, if you want to rename this to sp_helpindex8 – I won’t be offended. ;-)
So, how do you create this?
- You need to create sp_SQLskills_ExposeColsInIndexLevels (sp_SQLskills_ExposeColsInIndexLevels.sql (6.73 kb)). This is what gives us the tree/leaf definitions. And, this works for both SQL Server 2005 and SQL Server 2008.
- You need to create the version specific sp_helpindex8. These are named: sp_SQLskills_SQL2005_helpindex (sp_SQLskills_SQL2005_helpindex.sql (10.50 kb)) and sp_SQLskills_SQL2008_helpindex (sp_SQLskills_SQL2008_helpindex.sql (10.73 kb)). Again, I know they’re long names but using the keyboard shortcuts can help. And, if you do rename these make sure you do a replace ALL. There are multiple places where there are comments and/or statements that need to execute to get everything to work (for example the sp_MS_marksystemobject execution).
NOTE: The very first version of the 2005 script had a bug in it (thanks Calvin!). Funnily one I already knew about and thought I had caught (it was fixed in the 2008 version). Anyway, as of 3:15pm CT on May 19, it’s fixed in the scripts here. There could be others but so far – it’s looking good!
Enjoy!
kt
5 thoughts on “(OLD): A new and improved sp_helpindex (jokingly sp_helpindex8)”
The SQL 2005 script is missing a statement that resets variable @inc_columns.
— Second, we’ll figure out what the included columns are.
[b]select @inc_columns = NULL — missing from SQL2005 script[/b]
SELECT @inc_Count = count(*)
Ha… I can’t believe I did it again. I had that in there and somehow I went tweaking and I removed it. But, yes – you’re right. It’s in the 2008 script but not my 2005 script.
NOTE: This is updated now – 3:15 pm Central Time
THANKS Calvin!!
kt