【资料图】
SELECT object_name(ips.object_id) AS TableName, ips.index_id, name AS IndexName, avg_fragmentation_in_percent,db_name(ips.database_id) AS DatabaseName FROM sys.dm_db_index_physical_stats (Db_id(DB_NAME()) , NULL , NULL , NULL , NULL) AS ips INNER JOIN sys.indexes AS SI ON ips.object_id = SI.object_id AND ips.index_id = SI.index_id WHERE ips.avg_fragmentation_in_percent > 5 AND SI.index_id <> 0
索引的碎片率低于5%或者,索引的页数少于1000,可以忽略;
索引碎片率在5%-30%之间的,建议reorganize;
索引碎片率大于30%的,建议rebuild。
alter index [索引名] on [dbo].[表名] reorganize;
alter index [索引名] on [dbo].[表名] rebuild;
alter index all on [dbo].[表名] rebuild;
USE [数据库名] GO DECLARE @NoOfPartitions BIGINT; DECLARE @objectid INT; DECLARE @indexid INT; DECLARE @idxname NVARCHAR(255); DECLARE @objname NVARCHAR(255); DECLARE @partitionnum BIGINT; DECLARE @schemaname NVARCHAR(255); DECLARE @partitions BIGINT; DECLARE @frag FLOAT; DECLARE @statement VARCHAR(8000); -- checking existance of the table that we create for temporary purpose IF OBJECT_ID("defrag_work", "U") IS NOT NULL DROP TABLE defrag_work; -- Copy the fragmented indexes data into defrag_work table -- All the indexes that has fragmentation < 5 are getting stored into our work table SELECT [object_id] AS objectid , index_id AS indexid , partition_number AS partition_no , avg_fragmentation_in_percent AS frag INTO defrag_work FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, "LIMITED") WHERE avg_fragmentation_in_percent >5.0 and index_id > 0; -- cursor to process the list of partitions DECLARE partitions CURSOR FOR SELECT * FROM defrag_work; -- Open the cursor. OPEN partitions; -- Looping through the partitions FETCH NEXT FROM partitions INTO @objectid, @indexid, @partitionnum, @frag; WHILE @@FETCH_STATUS = 0 BEGIN; SELECT @objname= QUOTENAME(so.name) , @schemaname = QUOTENAME(ss.name) FROM sys.objects AS so JOIN sys.schemas AS ss ON ss.schema_id = so.schema_id WHERE so.object_id = @objectid; SELECT @idxname = QUOTENAME(name) FROM sys.indexes WHERE object_id = @objectid AND index_id = @indexid; SELECT @NoOfPartitions = COUNT(*) FROM sys.partitions WHERE object_id = @objectid AND index_id = @indexid; /* Let"s say N = fragmentation percentage N <= 5 = IGNORE 5 < N < 30 = REORGANIZE N > 30 = REBUILD */ IF (@frag < 30.0) -- @frag > 5 is already filtered in our first query, so we need that condition here BEGIN; SELECT @statement = "ALTER INDEX " + @idxname + " ON " + @schemaname + "." + @objname + " REORGANIZE"; IF @NoOfPartitions > 1 SELECT @statement = @statement + " PARTITION=" + CONVERT (CHAR, @partitionnum); EXEC (@statement); END; IF @frag >= 30.0 BEGIN; SELECT @statement = "ALTER INDEX " + @idxname + " ON " + @schemaname + "." + @objname + " REBUILD"; IF @NoOfPartitions > 1 SELECT @statement = @statement + " PARTITION=" + CONVERT (CHAR, @partitionnum); EXEC (@statement); END; PRINT "Executed " + @statement; FETCH NEXT FROM partitions INTO @objectid, @indexid, @partitionnum, @frag; END; -- Close and deallocate the cursor. CLOSE partitions; DEALLOCATE partitions; -- drop the table IF OBJECT_ID("defrag_work", "U") IS NOT NULL DROP TABLE defrag_work;
到此这篇关于SQLServer 表的索引碎片查询和处理的文章就介绍到这了,更多相关SQLServer 表索引碎片内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
下一篇:最后一页
近日,上海市人力资源和社会保障局、市邮政管理局联合印发《关于做好本市基层快递网点优先参加工伤保险工作的通知》,全面推进基层快递网点
7月18日,北京三元食品股份有限公司(以下简称三元食品)发布《关于公司控股子公司首农畜牧与现代牧业设立合资公司的公告》。公告显示,为巩
7月19日,派特尔(836871)将于7月22日在北交所上市,同日从新三板摘牌。资料显示,派特尔IPO发行价为5 6元 股,发行市盈率为13 63倍,拟发行
2022年7月18日,耐看娱乐控股有限公司递表港交所主板,招商证券国际及中泰国际为联席保荐人。公司曾于2022年1月3日首次递交上市申请,但目
2022年7月19日,赛恩斯环保股份有限公司(下称赛恩斯)回复科创板IPO二轮问询。在二轮问询中,上交所主要关注赛恩斯运营服务、应收账款、客户
X 关闭
X 关闭
作为国脉科技(002093)的实控人之一,陈国鹰欲再推旗下企业慧翰微电子股份有限公司(以下简称慧翰股份)上市,该公司此前科创板IPO曾走到注册[+更多]
7月18日,上海证券交易所(以下简称上交所)在官网发布关于对威龙葡萄酒股份有限公司及有关责任人予以监管警示的决定称,经查明,威龙葡萄酒[+更多]
信托系公募基金的团队阵营正在缩减。7月18日,来自全国产权行业信息化综合服务平台发布的信息显示,中融信托以底价约15 04亿元的价格公开挂[+更多]
7月18日,福石控股(证券代码:300071)控股股东杭州福石资产管理有限公司向台州金控资产管理有限公司质押股份1700万股,用于自身资金需求。[+更多]