Maintenance Plan Usage3:Task Usage (Maintain Index)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maintenance Plan Usage3:Task Usage (Maintain Index)相关的知识,希望对你有一定的参考价值。

Maintenance Plan用于维护Index Fragmentation 和 Statistics的Task 共有三个,后台是使用相应的TSQL来进行data维护。

Reorganize Index Task

Move index pages into a more efficient search order. This task uses the ALTER INDEX REORGANIZE statement with SQL Server 2012 databases.

Rebuild Index Task

Re-create the indexes on the tables in the database with a new fill factor. The fill factor determines the amount of empty space on each page in the index, to accommodate future expansion. As data is added to the table, the free space fills because the fill factor is not maintained. Reorganizing data and index pages can re-establish the free space. The Rebuild Index Task uses the ALTER INDEX statement.

Update Statistics Task

Update Microsoft SQL Server information about the data in the tables and indexes. This task resamples the distribution statistics of each index created on user tables in the database. The distribution statistics are used by SQL Server to optimize navigation through tables during the processing of Transact-SQL statements. To build the distribution statistics automatically, SQL Server periodically samples the data in the corresponding table for each index. This size of the sample is based on the number of rows in the table and the frequency of data modification. Use this option to perform an additional sampling using the specified percentage of data in the tables. SQL Server uses this information to create better query plans. This task executes the UPDATE STATISTICS statement.

 

一,Rebuild Index Task

1,Change free space per page percentage to :设置Index每个Leaf-level page的空闲空间。

Drop the indexes on the tables in the database and re-create them with a new, automatically calculated fill factor, thereby reserving the specified amount of free space on the index pages. The higher the percentage, the more free space is reserved on the index pages, and the larger the index grows. Valid values are from 0 through 100.

2,Sort results in tempdb 

Use the SORT_IN_TEMPDB option, which determines where the intermediate sort results, generated during index creation, are temporarily stored. If a sort operation is not required, or if the sort can be performed in memory, the SORT_IN_TEMPDBoption is ignored.

The intermediate sort results that are used to build the index are stored in tempdb. If tempdb is on a different set of disks than the user database, this may reduce the time needed to create an index. However, this increases the amount of disk space that is used during the index build.

3,Keep index online while reindexing                               

Use the ONLINE option which allows users to access the underlying table or clustered index data and any associated nonclustered indexes during index operations.

Online clause Specifies whether underlying tables and associated indexes are available for queries and data modification during the index operation. The default is OFF.

4,Other Rebuild index options

在Rebuild Index Task GUI中无法配置的Index Option 有5个。选项 PAD_INDEX,ALLOW_ROW_LOCKS ,ALLOW_PAGE_LOCKS 使用sys.indexes存储的metadata;Rebuild Index Task使用create index的配置值来设置选项 Data_Compression,STATISTICS_NORECOMPUTE。

技术分享

View T-SQL 查看后台的TSQL 脚本,示例Sql 脚本

ALTER INDEX [IndexName] 
ON SchemeName.TableName
REBUILD 
PARTITION = ALL 
WITH 
(
PAD_INDEX = OFF, 
STATISTICS_NORECOMPUTE = OFF, 
SORT_IN_TEMPDB = OFF, O
NLINE = OFF, 
ALLOW_ROW_LOCKS = ON, 
ALLOW_PAGE_LOCKS = ON, 
FILLFACTOR = 95
)

如果在创建Index时,指定 DATA_COMPRESSION 选项,那么Rebuild Index Task 会使用Create Index的DATA_COMPRESSION 选项设置来生成Rebuild Index的TSQL code。

二,Reorganize Index Task

Compact large objects                               

Deallocate space for tables and views when possible. This option uses ALTER INDEX LOB_COMPACTION = ON. This is only available for SQL Server 2005 databases.

技术分享

View T-SQL 查看后台的TSQL 脚本,示例Sql 脚本

ALTER INDEX [index_name] 
ON SchemeName.TableName
REORGANIZE 
WITH ( LOB_COMPACTION = ON )

WITH ( LOB_COMPACTION = { ON | OFF } )              

Specifies that all pages that contain large object (LOB) data are compacted. The LOB data types are image, text, ntext, varchar(max), nvarchar(max), varbinary(max), and xml. Compacting this data can improve disk space use. The default is ON. 

ON                  

All pages that contain large object data are compacted.

Reorganizing a specified clustered index compacts all LOB columns that are contained in the clustered index. Reorganizing a nonclustered index compacts all LOB columns that are nonkey (included) columns in the index.

When ALL is specified, all indexes that are associated with the specified table or view are reorganized, and all LOB columns that are associated with the clustered index, underlying table, or nonclustered index with included columns are compacted.

OFF                  

Pages that contain large object data are not compacted.

OFF has no effect on a heap.

The LOB_COMPACTION clause is ignored if LOB columns are not present.

 

三,Update Statistics Task

技术分享

View T-SQL 查看后台的TSQL 脚本,示例Sql 脚本

UPDATE STATISTICS 
SchemeName.TableName
WITH FULLSCAN,COLUMNS


 

参考文档:

https://msdn.microsoft.com/en-us/library/ms189738(v=sql.110).aspx

https://msdn.microsoft.com/en-us/library/ms178678(v=sql.110).aspx

https://msdn.microsoft.com/en-us/library/ms180074(v=sql.110).aspx

 

以上是关于Maintenance Plan Usage3:Task Usage (Maintain Index)的主要内容,如果未能解决你的问题,请参考以下文章

Maintenance Plan Usage4:Check Integrity and Shrink database

DAC Usage3:Monitor Data-tier Applications

预约系统 数据库设计

SqlServer存储过程

MySQL Error: PROCEDURE xmdk.query_all_plan can't return a result set in the given context

G - Eating Plan