DOWNLOAD THE CODE:
Download the Code 43783.zip

Executing the Job
You now have all of the tables, stored procedures, and jobs necessary to use this utility and rebuild your table indexes as necessary. The DBCC SHOWCONTIGDEMO job that Listing 2 creates performs five steps:

1.Inserts Northwind table and index data into the SHOWCONTIGDEMO database.

2.Deletes obsolete index names and IDs from the OBJECT_DATA table.

3.Inserts new index names and IDs into the OBJECT_LIST_DATA table.

4.Inserts information about the tables and indexes you're going to scan into the OBJECT_DATA table by using DBCC SHOWCONTIG.

5.Runs the reindex check and rebuilds any indexes that need it.

The first time this utility runs, it has to build the initial catalog of tables and indexes; it executes DBCC SHOWCONTIG against all the tables in Northwind and populates the STAGE_OBJECT_DATA table with the resulting data about Northwind's tables and indexes. I put all the data in this new table because it lets the DBA check multiple databases on the same SQL Server instance. Also, using a separate table means I don't have to store maintenance data in my production databases.

The utility doesn't delete any rows on its first run because all the tables are empty. But in subsequent runs, Step 2 of the job checks that the name of each index and its associated ID in the STAGE_OBJECT_DATA table match the current corresponding values in the OBJECT_LIST table. If the values in the two tables are different, the job deletes the data for that index from OBJECT_LIST and OBJECT_DATA. For example, if index 5 on the Orders table has been deleted and replaced by a new index, the job deletes the existing data for index 5 and collects data for the new index 5. Then, steps 3 and 4 insert the new index data into the SHOWCONTIGDEMO database tables OBJECT_LIST and OBJECT_DATA. The job rebuilds the existing index in Step 5 as required, according to the current threshold values in the OBJECT_LIST table.

The job uses the stored procedure p_ins_OBJECT_LIST to populate the OBJECT_LIST table with the default thresholds for Scan Density (80 percent) and Logical Scan Fragmentation (10 percent). These default values are a generic baseline for all tables; you can change the values after the OBJECT_LIST table is populated. The OBJECT_LIST table contains only one record for each index and doesn't contain historical records.

The job then populates the OBJECT_DATA table with the results from DBCC SHOWCONTIG that were stored in STAGE_OBJECT_DATA in Step 1 of the job. The OBJECT_DATA table contains a new record for each index every time you run the DBCC SHOWCONTIGDEMO job, and the STAGE_OBJECT_DATA table contains the current view of the tables and indexes. Each time it runs, the reindexing utility compares the staging table to the OBJECT_DATA table to determine whether any indexes have been dropped or are obsolete and to update OBJECT_DATA with new records to maintain an index history.

Finally, the job uses the p_REINDEX_CHECK stored procedure, which Web Listing 3 created, to check the Scan Density and Logical Scan Fragmentation DBCC SHOWCONTIG results for each index against the default thresholds in the OBJECT_LIST table. This stored procedure uses a cursor to select every table and index in Northwind, then compares the current table values to the threshold levels you set. The job uses the DBCC REINDEX command to rebuild any index that has a Scan Density value below 80 percent and a Logical Scan Fragmentation value greater than 10 percent. Then, the job writes the comparison results to a text file that you can view in Enterprise Manager; you just select Step 5 of the job, click the Advanced tab, then click View. Figure 1, page 42, shows part of the output of DBCC SHOWCONTIG for index ID 3 of Northwind's Orders table, which is an example of the kind of results you'll see when you run the job.

In Step 5, the DBCC SHOWCONTIGDEMO job automatically reindexes the Orders table because the results of DBCC SHOWCONTIG are outside the default thresholds you set in the p_ins_OBJECT_LIST stored procedure. Remember that if the default threshold of 80 percent for Scan Density isn't appropriate for a certain index, you can change the threshold in the OBJECT_LIST table.

Track Table Growth
In addition to being a repository for DBCC SHOWCONTIG data for the utility, the OBJECT_DATA table is also useful for table-growth analysis. Because the DBCC SHOWCONTIGDEMO job inserts a new record into this table every time it runs, you can track the usage of tables and indexes over time. For example, the count_rows field in the OBJECT_DATA table shows the number of rows for each table in the Northwind database, so as you run the job and collect records, you can calculate the growth of a given table. If you scheduled the job to run weekly, you could calculate the average number of rows added to each table weekly, monthly, and yearly.

Using this utility on your production systems will help you minimize maintenance-related downtime and track table growth in your database. This article's example job uses the DBCC SHOWCONTIG Scan Density and Logical Scan Fragmentation values to determine when to rebuild an index. But you could easily modify the job to include another field or fields as determining criteria. Or you could filter out any table that doesn't meet certain requirements, such as a minimum number of pages, so you don't rebuild small tables that have only one extent.

End of Article

Prev. page     1 [2]     next page -->



You must log on before posting a comment.

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

Reader Comments

There is an error in the listing 1 the stored procedure p_del_object_list_data is a select statement and not a delete as mentioned in the article

hmaandek

Article Rating 4 out of 5

Please specify the correct syntax for the stored procedure "p_del_object_list_data" in Listing 1.

mjmclean

Article Rating 4 out of 5

There appears to be a syntax error in Listing 1 ( line 11-12: SELECT FROM o FROM ...

and another at line 23-24: SELECT FROM l FROM ...

Should these be: SELECT * FROM ...

on both incidences?

dbbyleo

Article Rating 4 out of 5

I am also getting this when executing Listing 2:

Server: Msg 8145, Level 16, State 2, Procedure sp_add_job, Line 0 @SELECT_level is not a parameter for procedure sp_add_job.

dbbyleo

Article Rating 4 out of 5

Oh yeah...Listing 1 does NOT show any delete statements, yet article says it is suppose to delete obsolete records.

dbbyleo

Article Rating 4 out of 5

I was thinking of doing a similiar approach. One thing I do different is when a cluster index needs to be Reindex I do all the indexes on the table by not specifying the index name. I found the command runs quicker than if I just did the cluster index. The Profiler reports the read and write counts are higher but the duration is less.

dbird

Article Rating 5 out of 5

Very useful idea, the syntax errors in the p_del_object_list_data procedure invalidate its' usefulness.

mselway

Article Rating 2 out of 5

Very useful tool. But I am getting syntex errors when I implemented on production.

DBCC REINDEX syntex error. If you know the fix please let me know. I'm lazy to fix on my own.

r_angani

Article Rating 4 out of 5

not only are there syntax errors in listing 1, but there are also errors in p_REINDEX_check in that it doesnt encapsulate the table names in braces to prevent it from erroring when a table has a space or period in its name.

eigh

Article Rating 1 out of 5

Ditto. Errors in p_del_OBJECT_LIST_DATA limit the usefullness of the utility. I want to use it but make chages to use instead DBCC INDEXDEFRAG. Not knowing what the author was trying to do, means more time on my part. I would expect the listings in this Mag to work out of the box.

domodog

Article Rating 2 out of 5

The syntax error is easy to fix - just modify the select statements (select from o to something like select * --from o). I compiled everything,and the demo works great Anastasia

LoveDanger

Article Rating 4 out of 5

DBCC SHOWCONTIG WITH TABLERESULTS, ALL_INDEXES;

the above will give you better results without a cursor

mrweaver59

Article Rating 3 out of 5

A piece of crap - it just doesn't work - it won't even make the objects much less actually do something.

cduwel@thresholds.org

Article Rating 1 out of 5

Very useful article and seems to fill my needs.

msosis

Article Rating 5 out of 5

 
 

ADS BY GOOGLE