国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

SQLServer 表的索引碎片查詢和處理

瀏覽:125日期:2023-05-02 10:03:18
目錄
  • 1.查看索引的碎片率
  • 2.reorganize索引
  • 3.rebuild索引
  • 4.rebuild表上所有的索引
  • 5.rebuild數(shù)據(jù)庫中所有的索引  

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%或者,索引的頁數(shù)少于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數(shù)據(jù)庫中所有的索引  

USE [數(shù)據(jù)庫名]
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;

到此這篇關(guān)于SQLServer 表的索引碎片查詢和處理的文章就介紹到這了,更多相關(guān)SQLServer 表索引碎片內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!

標(biāo)簽: MsSQL
主站蜘蛛池模板: 免费看的一级片 | 性欧美一级毛片欧美片 | 国产精品99久久久久久小说 | 国产成人精品曰本亚洲77美色 | 午夜精品一区二区三区在线观看 | 高清毛片aaaaaaaaa片 | 91视频社区| 美国毛片亚洲社区在线观看 | 国产九九免费视频网站 | 国产精品大全国产精品 | 玖草在线视频 | 国产91一区二区在线播放不卡 | 一色屋成人免费精品网 | 亚洲一级片在线播放 | 久久久一本精品99久久精品66 | 自拍视频精品 | 伊人久久在线 | 国产一区二区三区国产精品 | 中文字幕 日韩在线 | 国产丝袜美腿高跟白浆 | 国产第三区 | 欧美一级特黄特黄毛片 | 免费观看欧美一级毛片 | 天天舔夜夜操 | 久久国产精品亚洲 | 国产一区亚洲欧美成人 | 一级视频免费观看 | 色拍自拍亚洲综合在线 | 国产精品私人玩物在线观看 | 蝴蝶成人世界第八影院 | 俄罗斯美女在线观看一区 | 99久久免费国产精精品 | 黑人巨大videos极度另类 | 国产欧美在线播放 | 亚洲国产精久久久久久久 | 韩国免费a级毛片 | 亚洲成a人v在线观看 | 国产男女猛烈无遮档免费视频网站 | 色天天躁夜夜躁天干天干 | 国内自拍视频在线播放 | 欧美日韩在线观看精品 |