1. design objectives.
Periodically describe the index fragments of tables or views in the specified database and rebuild or reorganize the indexes required for the collation.
Ii. design description.
Data Tables are added, updated, and deleted with index fragmentation, which slows down the query speed, especially when querying large data tables, therefore, it is necessary to perform regular index scanning on some important tables and reorganize or reconstruct the indexes required by the operators.
The method for Sorting index fragments is as follows:
1. Drop the original index and re-create these indexes. The advantage of this method is that the index is completely re-built to achieve the ideal situation. However, this will block all queries, preferably when the index fragmentation is very serious and the effect cannot be achieved using rebuild. Note: using the drop_existing clause can prevent the non-clustered index from being rebuilt twice.
2. Alter index... reorganize restructured the index.
Reorganizing an index with minimal system resources also compresses the index page. It can be executed when the database is online, but the sorting is not thorough enough.
3. Alter index... rebuild to regenerate the index.
Re-indexing will delete and re-create the index. This will delete fragments, reclaim disk space, and re-Sort index rows on consecutive pages based on specified or existing fill factor settings. This method completes operations in a transaction and requires sufficient free space in the data file to reconstruct all indexes and related objects. Otherwise, the operation may fail, or the reconstruction is incomplete. For tables with a large amount of data, more free space is required. Pay special attention to this.
This method only uses the rebuild or reorganize method to re-create indexes. For tables or views that have serious index fragmentation, please drop and recreate the indexes on your own.
You can view index fragments as follows:
Use indexservice3
Select * From SYS. dm_db_index_physical_stats (db_id (), null, 'limited ')
View avg_fragmentation_in_percent Field
3. instructions for use.
Because this SP uses the new sql2005 feature, it can only run on a database with the Compatibility Level of SQL Server 2005 (90.
1. Execute the SQL script in the attachment and create the Stored Procedure dm_defragment_indexes on the database for fragment maintenance.
2. You can create a job to run the script periodically or manually.
3. parameter description:
@ Dbname Database Name
@ Owner (default value: DBO)
@ Objectlist: List of tables or views scanned
@ Basefrag minimum fragment rate (default value: 10.00)
@ Maxfrag minimum fragment rate involved in reconstruction (default value: 15.00)
Complete call example:
Declare @ RC int
Declare @ dbname nvarchar (256)
Declare @ owner varchar (30)
Declare @ objectlist varchar (8000)
Declare @ basefrag float
Declare @ maxfrag float
Execute @ rc = [indexservice3]. [DBO]. [dm_defragment_indexes]
@ Dbname = n' indexservice3'
, @ Owner = 'dbo'
, @ Objectlist = 'contentindex, contenttag '--'*'
, @ Basefrag = 10.00
@ Maxfrag = 15.00
Simplified call
Execute @ rc = [indexservice3]. [DBO]. [dm_defragment_indexes]
@ Dbname = n' indexservice3'
, @ Objectlist = 'contentindex, contenttag' -- write only the table name and separate it ","
Or
Execute @ rc = [indexservice3]. [DBO]. [dbm_defragment_indexes]
@ Dbname = n' indexservice3'
, @ Objectlist = '*' -- scan all indexes in the database
Note:
@ Basefrag <frag <@ maxfrag execute rebuild
Frag> @ maxfrag execute reorganize
Default Value: @ owner = 'dbo' @ basefrag = 10.0 @ maxfrag = 15.0
Set @ objectlist = '*' to scan all tables or views in the database.
4. output description:
1.1 <frag <2.1 execute index reorganization; frag> = 2.1 execute index generation
Indexservice3.dbo. contentindex. idx_id index fragmentation is 4.98504% rebuild index: Alter index idx_id on DBO. contentindex rebuild time: 0 s
Indexservice3.dbo. contentindex. idx_guid index fragmentation: 1.10939% reorganize index: Alter index idx_guid on DBO. contentindex reorganize time: 0 s
Indexservice3.dbo. contentindex. idx_createtime index fragmentation is 10.2933% rebuild index: Alter index idx_createtime on DBO. contentindex rebuild time: 0 s
Indexservice3.dbo. contentindex. idx_author index fragmentation is 1.10047% reorganize index: Alter index idx_author on DBO. contentindex reorganize time: 0 s
The output information describes the conditions for re-indexing or re-indexing, the operations performed and the time and resources consumed.
5. Suggestion: only perform index maintenance for the tables that require key maintenance, because it takes a lot of time to restructure or recreate the indexes, in this way, maintenance can be controlled within a controllable range. The following method is recommended:
Declare @ RC int
Declare @ dbname nvarchar (256)
Declare @ owner varchar (30)
Declare @ objectlist varchar (8000)
Declare @ basefrag float
Declare @ maxfrag float
Execute @ rc = [indexservice3]. [DBO]. [dm_defragment_indexes]
@ Dbname = n' indexservice3'
, @ Objectlist = 'contentindex, contenttag'
MS-help: // Ms. sqlcc. V9/ms. sqlsvr. v9.zh-CHS/tsqlref9/html/b796c829-ef3a-405c-a784-48286d4fb2b9.htm
Http://www.sql-server-performance.com/rd_index_fragmentation.asp
Http://www.sql-server-performance.com/tp_automatic_reindexing.asp
Http://www.cnblogs.com/RicCC/archive/2006/09/01/492442.html