环球新动态:SQLServer 表的索引碎片查询和处理

2023-05-02 06:00:54    来源:脚本之家


【资料图】

目录
1.查看索引的碎片率2.reorganize索引3.rebuild索引4.rebuild表上所有的索引5.rebuild数据库中所有的索引  

1.查看索引的碎片率

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。

2.reorganize索引

alter index [索引名] on [dbo].[表名] reorganize;

3.rebuild索引

alter index [索引名] on [dbo].[表名] rebuild;

4.rebuild表上所有的索引

alter index all on [dbo].[表名] rebuild;

5.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 表索引碎片内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

关键词:

相关新闻
相关新闻
v 环球新动态:SQLServer 表的索引碎片查询和处理 2023-05-02
v 消防操作员报考条件(消防操作员报考条件)-看点 2023-05-02
v 韩国爱上后母电影在线观看-韩国爱上后母电影 环球最新 2023-05-02
v 视讯!无人驾驶插秧机——95后农机小老板的秘密武器 2023-05-02
v 世界视讯!耳朵痛怎么办快速缓解小方法_耳朵痛怎么办 2023-05-02
v 治疗尿路感染的药物有哪些_治疗尿路感染的药物 2023-05-01
v 红警2怎么局域网联机 红警2怎么局域网联机win10 2023-05-01
v 动态焦点:世界首艘93000立方米超大型液化气船将于上海交付 2023-05-01
v 桃源交警:暖心“罚单 ” 人性化执法 2023-05-01
v 美联储鹰派突袭!欧洲央行加息、中国刺激计划围攻 FXEmpire分析师:金价偏下行恐触及1956美元 焦点速讯 2023-05-01
v 高盛:美联储加息周期的结束可能无法刺激股市走高_天天速看 2023-05-01
v 五一劳动节,这个不打烊的窗口带动了一批人、温暖了一座城 2023-05-01
v 【环球新视野】人物语言描写的句子摘抄_人物语言描写的句子 2023-05-01
v 小学语文教育随笔因材施教_小学语文教育随笔 2023-05-01
v cf卡英雄武器_cf卡装备 2023-05-01
v 创意移轴短片丨今天,一起可可爱爱忙忙碌碌 2023-05-01
v 桑蚕丝的特点怎么介绍_桑蚕丝的特点 2023-05-01
v 西塘古镇订民宿不买门票无法入住?景区称门票需单独购买,有民宿称可代为购买再计入房费 世界看热讯 2023-05-01
v 世界今热点:我国四大海域5月1日12时进入休渔期农业农村部、中国海警局、公安部同步启动2023年海洋伏季休渔专项执法行动 2023-05-01
v 阿里旺旺2020_阿里旺旺旺遍天下-当前快讯 2023-05-01
v 异能迅雷下载 2023-05-01
v 今日上网课的感受作文_上网没意思! 世界热门 2023-05-01
v 生的干地瓜干的做法_地瓜干的做法 2023-05-01
v 三水乙酸钠商品报价动态(2023-05-01)_环球滚动 2023-05-01
v 广场舞火火的姑娘分解动作视频 广场舞火火的姑娘分解动作_全球播报 2023-05-01
v 世界热消息:风衣蝴蝶结的系法图解_风衣腰带蝴蝶结打法步骤详解 2023-05-01
v 环球讯息:深港“第一口岸”今年累计客流破千万 2023-05-01
v 奥拉星手游狼王平民打法_要怎么打 2023-05-01
v 游客在景区公厕过夜?黄山回应!多地景区“限流”……_世界热头条 2023-05-01
v 环球视讯!官宣!石川佳纯正式退役 马龙头号迷妹 最后一战不敌陈梦 2023-05-01

种植基地网 版权所有©未经书面授权禁止复制或建立镜像
京ICP备2022022245号-33
联系我们: 435 226 40@qq.com