SQLServer 表的索引碎片查詢和處理
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%或者,索引的頁(yè)數(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ù)庫(kù)中所有的索引
USE [數(shù)據(jù)庫(kù)名]
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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
sqlserver 數(shù)據(jù)類型轉(zhuǎn)換小實(shí)驗(yàn)
sql實(shí)驗(yàn)驗(yàn)證數(shù)據(jù)類型轉(zhuǎn)換實(shí)現(xiàn)sql語(yǔ)句2009-02-02
SQL查詢連續(xù)登陸7天以上的用戶的方法實(shí)現(xiàn)
本文主要介紹了SQL查詢連續(xù)登陸7天以上的用戶的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
sql分組后二次匯總(處理表重復(fù)記錄查詢和刪除)的實(shí)現(xiàn)方法
這篇文章主要介紹了sql分組后二次匯總的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-02-02
揭秘SQL Server 2014有哪些新特性(4)-原生備份加密
SQL Server原聲備份加密對(duì)數(shù)據(jù)安全提供了非常好的解決方案。使用原生備份加密基本不會(huì)增加備份文件大小,并且打破了使用透明數(shù)據(jù)加密后幾乎沒(méi)有壓縮率的窘境。2014-08-08
c語(yǔ)言http請(qǐng)求解析表單內(nèi)容
c語(yǔ)言解析http請(qǐng)求表單內(nèi)容,基于C語(yǔ)言的CGI開(kāi)發(fā),大家參考使用吧2013-12-12
修復(fù)斷電等損壞的SQL 數(shù)據(jù)庫(kù)
修復(fù)斷電等損壞的SQL 數(shù)據(jù)庫(kù),不論因?yàn)槟姆N原因,大家都可以測(cè)試下,試試。2009-08-08
SSMS中出現(xiàn)兩個(gè)相同的服務(wù)器名稱的問(wèn)題解決
在SSMS的【連接到服務(wù)器】頁(yè)面,有時(shí)候可能會(huì)出現(xiàn)多個(gè)相同的服務(wù)器名稱本文主要介紹了SSMS中出現(xiàn)兩個(gè)相同的服務(wù)器名稱的問(wèn)題解決,感興趣的可以了解一下2024-05-05
sqlserver設(shè)置主鍵的實(shí)現(xiàn)步驟
在SQLServer中,定義表的主鍵非常容易,本文主要介紹了sqlserver設(shè)置主鍵的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
如何查看SQLSERVER中某個(gè)查詢用了多少TempDB空間
最近幫助客戶做sql優(yōu)化的過(guò)程中,發(fā)現(xiàn)客戶的TempDB存在非常大的壓力,經(jīng)過(guò)排查是發(fā)現(xiàn)某些語(yǔ)句對(duì)TempDB的巨量使用所導(dǎo)致。那么如何才能排查出來(lái)呢,我們來(lái)探討下。2014-08-08

