Home > SQL Server Tips > Database Development > Stored procedure to find fragmented indexes in SQL Server
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

DATABASE DEVELOPMENT

Stored procedure to find fragmented indexes in SQL Server


Michelle Gutzait
11.07.2007
Rating: -4.33- (out of 5)


Expert advice on database development
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


The problem

SQL Server tables and indexes tend to become fragmented over time because of data modifications. In large I/O operations, application performance using these fragmented indexes and tables in SQL Server can be adversely affected.

When you are among a small number of DBAs who are in charge of tens of SQL Server instances and hundreds of databases, the situation can lead to a loss of control, or understanding, of the activity types in the diverse databases. In that situation, it's important to check for problematic fragmented tables and indexes regularly (unless you defragment all tables recurrently). That task often falls to the DBA because, often, development teams are not aware of the physical representation of the data.

This article provides an example code for collecting information on all fragmented indexes into one central database.

Possible commands

To find fragmented tables, here are two commands you can run:

  • DBCC SHOWCONTIG, for SQL Server 2000 or SQL Server 2005.
  • Select …. From sys.dm_db_index_physical_stats, in SQL Server 2005.
  • These commands need to be executed on all servers in all the databases for all the indexes. There are a few options for doing so. For example:

    1. Run a DTS/SSIS package.
    2. Write a program that loops on the server list and collects the information.
    3. Run a SQL Server T-SQL batch job.

    In order to run the utility in any version of SQL Server without asking for the version first, I decided to execute the DBCC SHOWCONTIG command. For simplicity, I created a SQL Server job using only T-SQL and linked servers.

    Note: I intend to replace the DBCC SHOWCONTIG with the sys.dm_db_index_physical_stats when all my servers are upgraded to SQL Server 2005, to follow a best practice.

    The environment

    I have one central development server hosting a SQL Server instance where I created a database for DBA use (DBA_DB) and linked servers to all my production servers. In the DBA_DB database, I have a table with the list of production servers with a column named "IsActive" indicating if the server should be checked (this way I can exclude some servers from being checked):

    Click here to download the above script.

    I have a few programs looping on the list of "live" servers (IsActive = 1) and executing common tasks. These tasks include checking databases that were not backed up for the last X days and performing periodical performance traces, among others. Now, I would like to add a program that will check for fragmented indexes. I enabled the xp_cmdshell stored procedure in the central database, as I am using it in my program.

    The strategy

    My strategy is to use a simple stored procedure. I am collecting the index fragmentation information from all servers and databases into a constant table I created in my DBA_DB database:


    Click here to download the above script.

    The stored procedure looks like this:

    Click here to view and download the complete stored procedure.

    The stored procedure results

    The fragmentation data is collected into the t_FragmentedTables table. What can be done with the collected data? Here are a few examples:

    1. Send an email with the fragmented indexes information.
    2. Open tickets regarding the fragmentation -- assuming you have a ticketing system and you can interact with it.

    Example (send email with fragmented tables -- up to 8,000 characters in the message body):


    Click here to download the above script.

    Performance:

    Just to give you an idea about its performance, I ran my stored procedure in a test environment when activity was very low on the databases:
    More on SQL Server indexes:
  • Indexes made handy in SQL Server<
  • Best SQL Server indexing strategies
  • Top 10 SQL Server indexing tips to improve performance
  • Windows 2003 Standard Edition SP1
  • 2* Dual Core CPU 3.06 GHz
  • 4 GB RAM, maximum of 2 GB configured for SQL Server
  • SQL Server 2000 SP3
  • Total size of user databases = 46 GB
  • Total of 2,170 user tables
  • Total of 8,154 indexes
  • Total of 5,864 fragmented indexes
  • Execution ended successfully after exactly three hours.

    Conclusion:

    If you set up an environment to include a SQL Server instance with

    • A special "DBA" database (for example: DBA_DB) that includes:
      • a table with the server list (t_ServerList) as described in the article.
      • a table to capture fragmentation data as described in the article.
    • Linked servers configured for each "active" server in the list table (t_ServerList).

    Then you can just copy and paste the stored procedure code and execute it. Later, you can analyze the results in the local table with the fragmentation details and implement alerts/reports/email according to your needs. You can also automatically defragment the fragmented indexes using one of the methods described in Microsoft Books Online.

    Note: The stored procedure used in my example should run when activity is low (for example, once a month during the weekend).


    ABOUT THE AUTHOR:   
    Michelle Gutzait works as a senior database consultant for ITERGY International Inc., an IT consulting firm specializing in the design, implementation, security and support of Microsoft products in the enterprise. Gutzait has been involved in IT for 20 years as a developer, business analyst and database consultant. For the last 10 years, she has worked exclusively with SQL Server. Her skills include SQL Server infrastructure design, database design, performance tuning, security, high availability, VLDBs, replication, T-SQL/packages coding, and more.
    Copyright 2007 TechTarget


    Rate this Tip
    To rate tips, you must be a member of SearchSQLServer.com.
    Register now to start rating these tips. Log in if you are already a member.




    Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



    RELATED CONTENT
    SQL Server Stored Procedures
    SQL Server Mailbag: CALs, witnesses and unwanted changes
    SQL Server Mailbag: Stored procedures, triggers and SSRS reports
    Top tips and tricks for SQL Server database development
    Top 10 SQL Server development tips of 2008
    SQL Server trigger vs. stored procedure to receive data notification
    SQL Server errors, failures and other problems fixed from the trenches
    SQL Server and data manipulation in T-SQL
    How to use SQL Server 2008 hierarchyid data type
    SQL Server stored procedures tutorial: Write, tune and get examples
    Check SQL Server database and log file size with this stored procedure

    Microsoft SQL Server Performance Monitoring and Tuning
    SQL Server Mailbag: CALs, witnesses and unwanted changes
    SQL Server Mailbag: Data restoration and DB property management
    Working with IntelliSense in SQL Server 2008 Management Studio
    SQL Server Mailbag: Stored procedures, triggers and SSRS reports
    Troubleshooting Distributed Transaction Coordinator errors in SQL Server
    Clearing the Windows page file and its effect on server performance
    Optimizing SQL Server indexes –- even when they're not your indexes
    Performance implications of transaction log autogrowth in SQL Server
    The short course on how SQL Server really works
    Determining the source of full transaction logs in SQL Server

    Database Development
    Speed up reports in SQL Server Reporting Services with caching
    Data Transformation Services vs. SSIS: The key differences
    Working with IntelliSense in SQL Server 2008 Management Studio
    Top tips and tricks for SQL Server database development
    Managing the development lifecycle with Visual Studio Team System 2008
    Processing XML files with SQL Server functions
    A first look at Visual Studio Team System 2008 Database Edition
    How to create a SQL inner join and outer join: Basics to get started
    New datetime data types in SQL Server 2008 offer flexibility
    Using DATEADD and DATEDIFF to calculate SQL Server datetime values

    RELATED GLOSSARY TERMS
    Terms from Whatis.com − the technology online dictionary
    library  (SearchSQLServer.com)
    trigger  (SearchSQLServer.com)

    RELATED RESOURCES
    2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
    Search Bitpipe.com for the latest white papers and business webcasts
    Whatis.com, the online computer dictionary

    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



    SQL Server Development - .NET, C#, T-SQL, Visual Basic
    HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts