The Problem

索引一直是优化查询性能的不二法门。其中一个最直接的问题便是当审查一个低性能查询语句时,检查索引是否在正确的地方或者加索引没有。运行一个batchjob查看索引碎片,必要时采取步骤优化索引碎片是日常维护程序中不可缺少的。

今天的主题便是如何判定数据库中的索引碎片和优化措施

我们经常会用到sys.dm_db_index_physical_stats表来查看索引信息

示例:

USE AdventureWorks2014
GO DECLARE @database_name VARCHAR(100) = 'AdventureWorks2014';
SELECT SD.name AS database_name ,
SO.name AS object_name ,
SI.name AS index_name ,
IPS.index_type_desc ,
IPS.page_count ,
IPS.avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats(NULL, NULL, NULL, NULL, NULL) IPS
INNER JOIN sys.databases SD ON SD.database_id = IPS.database_id
INNER JOIN sys.indexes SI ON SI.index_id = IPS.index_id
INNER JOIN sys.objects SO ON SO.object_id = SI.object_id
AND IPS.object_id = SO.object_id
WHERE alloc_unit_type_desc = 'IN_ROW_DATA'
AND index_level = 0
AND SD.name = @database_name
ORDER BY IPS.avg_fragmentation_in_percent DESC;

当知道索引碎片信息以后,我们应该如何优化呢?或者在任何数据库中查看索引碎片和优化呢?

The Solution

优化索引有两种方案

  • Index Rebuild:

    When an index is rebuilt , it is completely replaced with a new copy of the index, built from scratch as though
    it were just newly created. In SQL Server Standard edition, this is an offline operation, meaning that it can
    cause contention while running.

  • Index Reorganization: 

    Reorganizing an index results in cleanup at the leaf level, reordering pages and reapplying the fill factor as
    necessary. This operation is always online, regardless of the edition of SQL Server you are running and can
    be interrupted at any time with no ill effects.

现在我们通过百分比来判定索引是rebulid还是reorganization

IF OBJECT_ID('dbo.index_maintenance_demo','P') IS NOT NULL
BEGIN
DROP PROCEDURE dbo.index_maintenance_demo;
END SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO CREATE PROCEDURE dbo.index_maintenance_demo
@reorganization_percentage TINYINT = 10 ,
@rebuild_percentage TINYINT = 35 ,
@print_results_only BIT = 1
AS
BEGIN
DECLARE @sql_command NVARCHAR(MAX) = '';
DECLARE @parameter_list NVARCHAR(MAX) = '@reorganization_percentage TINYINT, @rebuild_percentage TINYINT'
DECLARE @database_name NVARCHAR(MAX);
DECLARE @database_list TABLE
(
database_name NVARCHAR(MAX) NOT NULL
);
INSERT INTO @database_list
( database_name
)
SELECT name
FROM sys.databases
WHERE databases.name NOT IN ( 'msdb', 'master', 'TempDB',
'model','ReportServer$SQL2014' );
CREATE TABLE #index_maintenance
(
database_name NVARCHAR(MAX) ,
schema_name NVARCHAR(MAX) ,
object_name NVARCHAR(MAX) ,
index_name NVARCHAR(MAX) ,
index_type_desc NVARCHAR(MAX) ,
page_count BIGINT ,
avg_fragmentation_in_percent FLOAT ,
index_operation NVARCHAR(MAX)
); SELECT @sql_command = @sql_command + '
USE [' + database_name + '] INSERT INTO #index_maintenance
( database_name ,
schema_name ,
object_name ,
index_name ,
index_type_desc ,
page_count ,
avg_fragmentation_in_percent ,
index_operation
)
SELECT CAST(SD.name AS NVARCHAR(MAX)) AS database_name ,
CAST(SS.name AS NVARCHAR(MAX)) AS schema_name ,
CAST(SO.name AS NVARCHAR(MAX)) AS object_name ,
CAST(SI.name AS NVARCHAR(MAX)) AS index_name ,
IPS.index_type_desc ,
IPS.page_count ,
-- Be sure to filter as much as possible...this can return a lot of data if you dont filter by database and table.
IPS.avg_fragmentation_in_percent ,
CAST(CASE WHEN IPS.avg_fragmentation_in_percent >= @rebuild_percentage
THEN ''REBUILD''
WHEN IPS.avg_fragmentation_in_percent >= @reorganization_percentage
THEN ''REORGANIZE''
END AS NVARCHAR(MAX)) AS index_operation
FROM sys.dm_db_index_physical_stats(NULL, NULL, NULL, NULL, NULL) IPS
INNER JOIN sys.databases SD ON SD.database_id = IPS.database_id
INNER JOIN sys.indexes SI ON SI.index_id = IPS.index_id
INNER JOIN sys.objects SO ON SO.object_id = SI.object_id
AND IPS.object_id = SO.object_id
INNER JOIN sys.schemas SS ON SS.schema_id = SO.schema_id
WHERE alloc_unit_type_desc = ''IN_ROW_DATA''
AND index_level = 0
AND SD.name = ''' + database_name + '''
AND IPS.avg_fragmentation_in_percent >= @reorganization_percentage
AND SI.name IS NOT NULL -- Only review index, not heap data.
AND SO.is_ms_shipped = 0 -- Do not perform maintenance on system objects
ORDER BY SD.name ASC;'
FROM @database_list
WHERE database_name IN ( SELECT name FROM sys.databases ); EXEC sp_executesql @sql_command, @parameter_list,
@reorganization_percentage, @rebuild_percentage; SELECT @sql_command = '';
SELECT @sql_command = @sql_command + '
USE ' + QUOTENAME(database_name) + '
ALTER INDEX ' + QUOTENAME(index_name) + ' ON ' + QUOTENAME(schema_name) + '.' + QUOTENAME(object_name) + ' '
+ index_operation + ';'
FROM #index_maintenance; SELECT *
FROM #index_maintenance
ORDER BY avg_fragmentation_in_percent DESC; IF @print_results_only = 1
BEGIN
PRINT @sql_command;
END
ELSE
BEGIN
EXEC sp_executesql @sql_command;
END DROP TABLE #index_maintenance;
END
GO

然后运行SP得到以下结果

EXEC dbo.index_maintenance_demo @reorganization_percentage = 10, @rebuild_percentage = 35,
@print_results_only = 1;

打印出来的SQL如下:

USE [AdventureWorks2014]
ALTER INDEX [PK_ProductCostHistory_ProductID_StartDate] ON [Production].[ProductCostHistory] REBUILD;
USE [AdventureWorks2014]
ALTER INDEX [AK_ProductDescription_rowguid] ON [Production].[ProductDescription] REBUILD;
USE [AdventureWorks2014]
ALTER INDEX [PK_DatabaseLog_DatabaseLogID] ON [dbo].[DatabaseLog] REBUILD;
USE [AdventureWorks2014]
ALTER INDEX [PK_ProductInventory_ProductID_LocationID] ON [Production].[ProductInventory] REBUILD;
USE [AdventureWorks2014]
ALTER INDEX [PK_ProductListPriceHistory_ProductID_StartDate] ON [Production].[ProductListPriceHistory] REBUILD;
USE [AdventureWorks2014]
ALTER INDEX [PK_SpecialOfferProduct_SpecialOfferID_ProductID] ON [Sales].[SpecialOfferProduct] REBUILD;
USE [AdventureWorks2014]
ALTER INDEX [AK_SpecialOfferProduct_rowguid] ON [Sales].[SpecialOfferProduct] REBUILD;
USE [AdventureWorks2014]
ALTER INDEX [PK_StateProvince_StateProvinceID] ON [Person].[StateProvince] REBUILD;
USE [AdventureWorks2014]
ALTER INDEX [PK_ProductModelProductDescriptionCulture_ProductModelID_ProductDescriptionID_CultureID] ON [Production].[ProductModelProductDescriptionCulture] REBUILD;
USE [AdventureWorks2014]
ALTER INDEX [AK_BillOfMaterials_ProductAssemblyID_ComponentID_StartDate] ON [Production].[BillOfMaterials] REORGANIZE;

有了这个SP,以后我们的维护工作就轻便了许多。

T-SQL Recipes之Index Defragmentation的更多相关文章

  1. Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0

    1.错误描述 [ERROR:]2015-05-05 16:35:50,664 [异常拦截] org.hibernate.exception.GenericJDBCException: error ex ...

  2. java.sql.SQLException:Column Index out of range,0<1

    1.错误描述 java.sql.SQLException:Column Index out of range,0<1 2.错误原因 try { Class.forName("com.m ...

  3. java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).

    java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2). java. ...

  4. java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

    java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). at co ...

  5. java.io.IOException: java.sql.SQLException: ORA-01502: index 'BTO.PK_xxxxx' or partition of such index is in unusable state

    最近由于数据库的全备出问题了,所以一直在观察. 刚好发现很多不需要的数据,就删了几百个G的数据吧. 今天突然就报这个问题. java.io.IOException: java.sql.SQLExcep ...

  6. sql server中index的REBUILD和REORGANIZE的区别及工作方式

    sql server中index的REBUILD和REORGANIZE 转自:https://www.cnblogs.com/flysun0311/archive/2013/12/05/3459451 ...

  7. ylb:SQL 索引(Index)

    ylbtech-SQL Server: SQL Server-SQL 索引(Index) SQL 索引(Index). ylb:索引(Index) 返回顶部 --=================== ...

  8. SQL Server 索引(index) 和 视图(view) 的简单介绍和操作

    --索引(index)和视图(view)-- --索引(index)----概述: 数据库中的索引类似于书籍的目录,他以指针形式包含了表中一列或几列组合的新顺序,实现表中数据库的逻辑排序.索引创建在数 ...

  9. sql server中index的REBUILD和REORGANIZE

    参考文献: http://technet.microsoft.com/en-us/library/ms188388.aspx 正文 本文主要讲解如何使用alter index来rebuild和reor ...

随机推荐

  1. Open Xml 读取Excel中的图片

      在我的一个项目中,需要分析客户提供的Excel, 读出其中的图片信息(显示在Excel的第几行,第几列,以及图片本身). 网络上有许多使用Open Xml插入图片到Word,Excel的文章, 但 ...

  2. 服务器通过微信公众号Token验证测试的代码(Python版)

    我在阿里云租了一个云服务器,然后想把这个作为我的微信公众号的后台,启用微信公众号开发者需要正确的响应微信服务器的Token验证,为此把这个验证的Python代码贴出来,只要在服务器上运行这段代码,注意 ...

  3. ng-repeat 里 使用ng-show ng-hide出现闪动

    在项目中使用ng-repeat在表格中循环输出一组数据的时候,需要对表格中的每一列(每一列为数组的一个元素--对象)添加控制按钮. 列表样式 我的期望是 初始化 ----每一行不可编辑,保存按钮是隐藏 ...

  4. No.23

    腓利比书3:19:"他们的结局就是沉沦,他们的神就是自己的肚腹,他们以自己的羞辱为荣耀,专以地上的事为念". 谨记!

  5. PSD文件在MAC上和在WINDOWS上的大小有本质区别

    因为偷懒在MAC上的美工,发我的PSD文件,我就直接在上面做了= =后来不知道为什么无论我怎么合并图层.PSD的大小永远都是107M....然后忍无可忍重新画就从107M变成2M.....MAC为什么 ...

  6. kettle转换和作业插件开发及调试

    这是一篇几年前写下的文档,最近打算根据这篇文档重写一下kettle插件的教程.结果各种理由,一推再推.今天索性将这篇文档发布出来,分享给大家,例子等有空再补上.这是一篇基于kettle3.2基础上完成 ...

  7. maven发布项目到私服-snapshot快照库和release发布库的区别和作用及maven常用命令

    maven发布项目到私服-snapshot快照库和release发布库的区别和作用及maven常用命令 在日常的工作中由于各种原因,会出现这样一种情况,某些项目并没有打包至mvnrepository. ...

  8. ZOJ 3871 Convex Hull(计算几何、凸包)

    题意:给n个点,|x[i]|,|y[i]| <= 1e9.求在所有情况下的子集下(子集点数>=3),凸包的面积和. 这题主要有几个方面,一个是凸包的面积,可以直接用线段的有向面积和求得,这 ...

  9. eclipse开发servlet,HttpServletRequest报红叉解决方案

    eclipse开发servlet,HttpServletRequest报红叉解决方案 今天突然间有兴致,想打一会代码,于是开发一个Servlet,代码和配置路径都没问题,HttpServlet居然报错 ...

  10. 解决 odoo.py: error: option --addons-path: The addons-path 'local-addons/' does not seem to a be a valid Addons Directory!

    情况说明 odoo源文件路径-/odoo-dev/odoo/: 我的模块插件路径 ~/odoo-dev/local-addons/my-module 在my-module中创建了__init__.py ...