Performing maintenance tasks in SQL Server 2000 for Reindex and Stat.

Performing maintenance tasks in SQL Server

Problem
With any database systems there are a couple of key items that help with performance.  These items include:

  • good database design
  • properly written SQL code
  • correct indexes
  • current statistics
  • defragmented indexes and data

The first two items are often the most difficult challenges, because systems grow and their usage patterns change over time getting these right the first time is not that easy. To further complicate the issue making changes to these two items is often very time consuming, difficult or maybe impossible.  When applications are first written the code is written to complete the job and the developer doesn't think about load testing or issues as the database grows.  Also, the database is often designed to handle the need at that time, but this can change very dramatically over time. Although these two items can be modified to handle the changes in usage, the level of effort is quite high and therefore not as much is done that could be done.

The third item is something that can be easily changed as time goes by and usage patterns change in the system. By reviewing the statements that are being issued new indexes can be created, old indexes removed or existing indexes modified to meet the need.

The last two items can be handled by maintenance tasks that can be run on a periodic basis to ensure that the SQL statements are running as efficiently as possible. Maintenance tasks do not take much level of effort to do, but could provide significant results if done properly.  The core maintenance tasks should include index rebuilds, statistics updates and defragmenting indexes and data.

Solution
With SQL Server there are several options that you can use to perform maintenance tasks. The area that most people are familiar with is using Database Maintenance Plans.  This is a simple way to ensure that maintenance routines are being run on your databases.  The following screen shows you the options that can be selected to rebuild your indexes or update your statistics.

The first item is "Reorganize data and index pages".  This option will rebuild your indexes so that the data is laid out in a more efficient manner when queries are run to use the data.  There are two options that can be used:

  • Reorganize pages with the original amount of free space - this will use whatever was specified when the table was created
  • Change free space per page percentage to - this will use the same setting for all tables

What this option does is determine how much free space is left on a page of data for additional inserts. Whenever there is not enough space on the page SQL Server needs to do a page split where a new page is created and some of the data from this existing page will go to one page and other data will go to another page.  So this setting could be very critical in a very busy system.

The second item is "Update statistics used by query optimizer".  This option will rebuild the statistics on the tables which SQL Server will use to determine how to access the data.  From the statistics SQL Server determines what index to use and whether to use the index or just read through the entire table.  The "% of the database" tells this process how much data should be sampled to determine the statistics.  If you specify 100%, SQL Server will read through all of your data to rebuild the stats. 

A couple of notes: when you rebuild the indexes the stats are rebuilt as well. This is why the option is disable if you select "Reorganize data and index pages"  Also, when you create your databases you have the option of setting "Auto Create Statistics" and "Auto Update Statistics".  So when data changes these changes are applied to the statistics automatically.(如果已经做了Reindex,则也涵盖了stats are rebuilt )

In addition to setting these tasks up using Database Maintenance Plans, all of this work can be done using T-SQL commands.  Following is a list of some of the commands that you can use in place of the Maintenance Plans.  These can be put in SQL Agent jobs or run from Query Analyzer.

T-SQL CommandDescription
DBCC SHOWCONTIGDisplays fragmentation information for the data and indexes of the specified table.
DBCC DBREINDEXRebuilds one or more indexes for a table in the specified database.
DBCC INDEXDEFRAGDefragments clustered and secondary indexes of the specified table or view.  DBCC INDEXDEFRAG is an online operation, so it does not hold long-term locks that can block running queries or updates. DBCC INDEXDEFRAG can be considerably faster than running DBCC DBREINDEX because a relatively unfragmented index can be defragmented much faster than a new index can be built.
  
DBCC SHOW_STATISTICSDisplays the current distribution statistics for the specified target on the specified table.
UPDATE STATISTICSUpdates information about the distribution of key values for one or more statistics groups in the specified table or indexed view.
sp_autostatsDisplays or changes the automatic UPDATE STATISTICS setting for a specific index and statistics, or for all indexes and statistics for a given table or indexed view in the current database.
sp_updatestatsRuns UPDATE STATISTICS against all user-defined tables in the current database.

The advantage to using T-SQL commands is that you can control what happens and when it happens. With the Maintenance Plan the same operation is done on all tables.  With the T-SQL commands you can set which tables to rebuild indexes on or even which index to rebuild.  Using DBCC SHOWCONTIG you can also see how fragmented a table or index is and then determine whether you need to rebuild the index or not.

For additional reading on any of these commands or processing refer to SQL Server Books Online or click on the links above.

posted on 2009-09-10 15:34 Jacky Xu 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/JackyXu1981/archive/2009/09/10/1564103.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值