SQL Server 2012 批量重建索引
关于索引的概念可以看看宋大牛的博客
T-SQL查询高级—SQL Server索引中的碎片和填充因子
整个数据库的索引很多,索引碎片多了,不可能一个个的去重建,都是重复性的工作,所以索性写了个存储过程,按范围需要重建索引。
- -- =============================================
- -- Author: <Limy>
- -- Create date: <2015-12-31>
- -- Description: <批量重建索引>
- -- EXEC pr_BatchRebuildIndex 'dbo','DatabaseLog','PK_DatabaseLog_DatabaseLogID',0
- -- =============================================
- alter proc pr_BatchRebuildIndex
- --@DataBase nvarchar(50)=null,--数据库名称,必输项
- @Scheme nvarchar(50)=null,--架构名称
- @TableName nvarchar(50)=null, --要重建索引的表名称,为空表示所有表
- @IndexName nvarchar(200)=null ,--要重建的索引名称
- @FragmentPercent decimal(10,5)=0 --碎片率为多少以上的药重建
- AS
- BEGIN
- DECLARE @Sql nvarchar(2000);
- DECLARE @RebuildSql nvarchar(2000);
- DECLARE @ERROR nvarchar(500);
- DECLARE @Tables table(TableName nvarchar(100), Indexid int,IndexName nvarchar(200), FragmentPercent decimal(10,5));
- ----判断数据库是否存在
- --IF DB_ID(@DataBase) is null
- --BEGIN
- -- RAISERROR('数据库不存在,请输入正确的数据库名称!',16,1);
- -- RETURN -1;
- --END
- --判断架构是否存在
- IF isnull(@Scheme,'')<>'' and SCHEMA_ID(@Scheme) is null
- BEGIN
- RAISERROR('架构不存在,请输入正确的架构名称!',16,1);
- RETURN -1;
- END
- --表存不存在
- IF isnull(@TableName,'')<>''
- BEGIN
- --有架构
- IF isnull(@Scheme,'')<>'' and SCHEMA_ID(@Scheme) is null
- BEGIN
- IF OBJECT_ID(@Scheme+'.'+@TableName) is null
- BEGIN
- SET @ERROR='表不存在,请输入正确的表名称!'
- RAISERROR(@ERROR,16,2);
- RETURN -1;
- END
- END
- --没有架构
- IF not exists(select 1 from sys.tables as A where A.name=@TableName )
- BEGIN
- SET @ERROR='表不存在,请输入正确的表名称!'
- RAISERROR(@ERROR,16,5);
- RETURN -1;
- END
- END
- --索引存不存在
- IF isnull(@IndexName,'')<>''
- BEGIN
- IF not exists(select 1 from sys.SysObjects where name=@IndexName and (xtype=N'UQ' OR xtype=N'PK') )
- BEGIN
- RAISERROR('索引不存在,请输入正确的索引名称!',16,3);
- RETURN -1;
- END
- END
- --如果表名为空表示所有表,如果索引为空表示所有索引
- --默认查询所有表,所有索引,所有百分比
- SET @Sql='SELECT D.name+''.''+object_name(a.object_id) [TableName] ,a.index_id ,B.name [IndexName] ,avg_fragmentation_in_percent
- FROM sys.dm_db_index_physical_stats ( DB_ID() , NULL , NULL, NULL, NULL ) AS A
- JOIN sys.indexes AS B ON a.object_id = b.object_id AND a.index_id = b.index_id
- inner JOIN sys.tables AS C ON a.object_id=C.object_id
- inner JOIN sys.schemas AS D ON C.schema_id=D.schema_id
- WHERE a.index_id > 0 '
- IF isnull(@Scheme,'') <>''
- BEGIN
- SET @Sql +=' and D.name= '''+@Scheme+''' ';
- END
- IF isnull(@TableName,'')<>''
- BEGIN
- SET @Sql +=' and object_name(a.object_id)= '''+@TableName+''' ';
- END
- IF isnull(@IndexName,'')<>''
- BEGIN
- SET @Sql +=' and B.name= '''+@IndexName+''' ';
- END
- IF isnull(@FragmentPercent,0)>0
- BEGIN
- SET @Sql +=' and avg_fragmentation_in_percent>= '+convert(nvarchar(10),@FragmentPercent)+' ';
- END
- INSERT INTO @Tables
- EXEC (@Sql)
- select '重建前',* from @Tables
- DECLARE cur cursor for
- select TableName, IndexName from @Tables
- OPEN cur
- DECLARE @tbName NVARCHAR(100),@IXName NVARCHAR(200)
- FETCH NEXT FROM cur INTO @tbName,@IXName
- WHILE(@@fetch_status=0)
- BEGIN
- SET @RebuildSql='alter index ['+@IXName+'] on '+@tbName+' rebuild' --要加上[],否则索引里有空格会报错
- EXEC (@RebuildSql)
- FETCH NEXT FROM cur INTO @tbName,@IXName
- END
- CLOSE cur
- DEALLOCATE cur
- --重建后查询
- delete @Tables
- INSERT INTO @Tables
- EXEC (@Sql)
- select '重建后',* from @Tables
- Print N'执行完毕!';
- return 0;
- END
本人才疏学浅,如有不合理之处,欢迎拍砖。
SQL Server 2012 批量重建索引的更多相关文章
- SQL Server 2012 列存储索引分析(翻译)
一.概述 列存储索引是SQL Server 2012中为提高数据查询的性能而引入的一个新特性,顾名思义,数据以列的方式存储在页中,不同于聚集索引.非聚集索引及堆表等以行为单位的方式存储.因为它并不要求 ...
- SQL Server 2012 列存储索引分析(转载)
一.概述 列存储索引是SQL Server 2012中为提高数据查询的性能而引入的一个新特性,顾名思义,数据以列的方式存储在页中,不同于聚集索引.非聚集索引及堆表等以行为单位的方式存储.因为它并不要求 ...
- [S]SQL SERVER数据库维护与重建索引
第一步:查看是否需要维护,查看扫描密度/Scan Density是否为100% declare @table_id int set @table_id=object_id('表名') dbcc sho ...
- SQL SERVER数据库维护与重建索引
第一步:查看是否需要维护,查看扫描密度/Scan Density是否为100% declare @table_id int set @table_id=object_id('表名') dbcc sho ...
- SQL Server 2012中快速插入批量数据的示例及疑惑
SQL Server 2008中SQL应用系列--目录索引 今天在做一个案例演示时,在SQL Server 2012中使用Insert语句插入1万条数据,结果遇到了一个奇怪的现象,现将过程分享出来,以 ...
- SQL Server 2012 案例教程(贾祥素)——学习笔记
第2章 SQL Server 2012概述 1.SQL(Structed Query Language),结构化查询语言. 2.SSMS(SQL Server Mangement Studio),SQ ...
- SQL Server 2012新增和改动DMV
SQL Server 2012新增和改动DMV 系统视图 说明 sys.dm_exec_query_stats (Transact-SQL) 添加了四列,以帮助排除长时间运行的查询所存在的问题. 可 ...
- SQL SERVER 2012 执行计划走嵌套循环导致性能问题的案例
开发人员遇到一个及其诡异的的SQL性能问题,这段完整SQL语句如下所示: declare @UserId INT declare @PSANo VAR ...
- SQL Server 2012提供的OFFSET/FETCH NEXT与Row_Number()对比测试 [T]
SQL Server 2008中SQL应用系列--目录索引 前些天看到一篇文章<SQL Server 2012 - Server side paging demo using OFFSET/FE ...
随机推荐
- 第一课 T语言关键字(版本TC5.0)
关键字 TC综合开发工具的语法里包含了大量的关键字,而且对TC综合开发工具支持的关键字都做了亮色处理,以便大家在写脚本的时候容易区分. 关键字在使用的时候,都有特定的作用,不能作为变量名.函数名等等使 ...
- 用自己的机器人和ubuntu PC实现通信和控制--26
原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 前提: 1.拥有一台能够采用手动或者自动移动的机器人移动平台. 2.在电机端需要安装高分辨率的霍尔编码器 ...
- Hardly Hard
You have been given the task of cutting out a quadrilateral slice of cake out of a larger, rectangul ...
- jquery轻松操作CSS样式
$(this).click(function(){ if($(this).hasClass(“zxx_fri_on”)){ $(this).removeClass(“zxx_fri_on”); ...
- Tutorial: Triplet Loss Layer Design for CNN
Tutorial: Triplet Loss Layer Design for CNN Xiao Wang 2016.05.02 Triplet Loss Layer could be a tri ...
- Performance Analysis Methodology
http://www.brendangregg.com/methodology.html The USE Method: for finding resource bottlenecks The TS ...
- java .net compartion
1, http://www-01.ibm.com/software/smb/na/J2EE_vs_NET_History_and_Comparison.pdf http://stackoverflow ...
- Oracle升级前备份和失败回退
一.升级前备份 1.软件备份[root@localhost backup]# su - oracle [oracle@localhost ~]$ cd $ORACLE_HOME[oracle@loca ...
- python数据库连接池
python数据库连接池 import psycopg2 import psycopg2.pool dbpool=psycopg2.pool.PersistentConnectionPool(1,1, ...
- 怎么给OCR文字识别软件设置正确的扫描分辨率
ABBYY FineReader 12是一款专业的OCR文字识别软件,可快速方便地将扫描纸质文档.PDF文件和数码相机的图像转换成可编辑.可搜索的文本,不仅支持对页扫描,还支持多页扫描,扫描分辨率的选 ...