Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 



2 Stored Procedures to Tune Your Indexes

RSS
Subscribe to Windows IT Pro | See More SQL Server and Database Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

If you’ve been a DBA for a while, you likely have encountered developers who are extremely proficient in front-end application design but have a rather limited understanding of the database technology they’re using as the back end. In these situations, it seems that indexes are only applied as an afterthought, if they are even applied at all.

Once these applications start to process a serious amount of data, performance problems begin to surface. This typically happens a few days after the application is released to production. A DBA at this point might be asked to take a look and see what can be done.

After some preliminary investigations and performing a few traces, the DBA might decide to perform a full index analysis. The Database Engine Tuning Advisor might be useful here, but I prefer getting to know my indexes personally rather than relying on a wizard’s best educated guess. So, I wrote two T-SQL stored procedures to aid in this task: sp_GenerateIndexes and sp_ListAllIndexes, which work on SQL Server 2005 and SQL Server 2000.

The sp_GenerateIndexes stored procedure generates SQL statements that can drop and create indexes for a specified table. By having the SQL code drop and create indexes, you can do some experimenting by adding or removing columns as you see fit. If you’re unhappy with the results, you can simply use sp_GenerateIndexes again to drop the new indexes and re-create the original indexes by running the SQL code that was generated previously. You would, of course, do this experimentation only in a test environment, not on your live production server.

The sp_GenerateIndexes stored procedure also creates an extremely useful report that details the various properties of each index, such as whether the index is clustered, the sort order, and the fill factor. Figure 1 contains an example of a report that’s been edited for space. I tried to keep the column names short so that I could include as much information as possible in the report. Here is a brief description of what each column in the report contains:

  • TableName—Specifies the name of the table.
  • IndexName—Specifies name of the index.
  • ID—Specifies the index ID as retrieved from the sysindex table. (Note that 0 and 255 aren’t present because they have special meanings.)
  • Clust—Specifies whether the index is clustered.
  • Uniq—Specifies whether the index is unique.
  • PK—Specifies whether the index is a primary key constraint.
  • IgDup—Specifies whether the index ignores duplicate entries.
  • Stat—Specifies whether the index is really an autogenerated statistic and not a true index.
  • Hyp—Specifies whether the index is really a hypothetical index created by the Index Tuning Wizard and not a true index.
  • Fill—Notes the original fill factor that was specified when the index was created.
  • PadIdx—Specifies whether index padding is enabled for the index.
  • ColumnNames—Provides a concatenated list of all the columns in the index.

As you can see from the sample report in Figure 1, the MyTable table has a clustered primary key, a unique index, and a nonclustered compound index with a descending sort order on its second column. The original fill factor was specified as 100 for each index. What stands out in this report is that the third index is a compound index that includes the same column as the clustered index (i.e., TheID). This is wasteful because each index row in the nonclustered index already contains a row-locater, which in this case, is the clustered index key (i.e., TheID).

The second stored procedure that I created— sp_ListAllIndexes—iterates through all the tables in the current database, calling sp_GenerateIndexes for each table found. The output from sp_GenerateIndexes is captured and stored in a worktable, then displayed at the end as one big master report.

After running sp_ListAllIndexes, you’ll have a master report that lists all the indexes on all the tables in the current database. A quick glance at this report is often all you need to make a few strategic decisions, such as whether to add clustered indexes to heap tables or whether to remove obviously useless indexes. If there are no indexes to report on, you can bring the empty report to the development team and politely explain to them that a database without indexes is about as useful as a mouse without buttons.

End of Article



Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: Week of November 24, 2008

An often irreverent look at some of the week's other news, including a Vista Capable dismissal request, Zune price reductions, Morrow musings, Novell and Microsoft sitting in a tree ... two years later, Yahoo!, IE 6 on Windows Mobile, and so much more ...


SQL Server and Database Whitepapers StoreVault SnapManagers for Microsoft Exchange and SQL Server

Related Events Microsoft BI Unleashed | Online Conference

SQL Server 2008 – Can You Wait? | Philadelphia

Check out our list of Free Email Newsletters!

SQL Server and Database eBooks Safeguarding Your Windows Servers

SQL Server Administration for Oracle DBAs

Taking Control: Monitoring the Windows Platform Proactively

Related SQL Server and Database Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing