Hi,@Vijay Kumar
Welcome to Microsoft T-SQL Q&A Forum!
Please check this:
CREATE TABLE #IDXFRAG
( DbName sysname,
ObjName sysname,
IdxName sysname NULL,
-- columns below are exactly as generated by SYS.DM_DB_INDEX_PHYSICAL_STATS function
database_id smallint,
[object_id] int,
index_id int,
partition_number int,
index_type_desc nvarchar(60) COLLATE Latin1_General_CI_AI NULL,
alloc_unit_type_desc nvarchar(60) COLLATE Latin1_General_CI_AI NULL,
index_depth tinyint NULL,
index_level tinyint NULL,
avg_fragmentation_in_percent float NULL,
fragment_count bigint NULL,
avg_fragment_size_in_pages float NULL,
page_count bigint NULL,
avg_page_space_used_in_percent float NULL,
record_count bigint NULL,
ghost_record_count bigint NULL,
version_ghost_record_count bigint NULL,
min_record_size_in_bytes int NULL,
max_record_size_in_bytes int NULL,
avg_record_size_in_bytes float NULL,
forwarded_record_count bigint NULL
-- , primary key nonclustered (DbName, ObjName,[object_id],index_id,partition_number) -- IdxName is NULL if HEAP (index_id=0)
)
GO
IF NOT EXISTS (SELECT * FROM tempdb.sys.indexes WHERE object_id = OBJECT_ID(N'tempdb..#IDXFRAG') AND name = N'IDXFRAG_CI')
create index IDXFRAG_CI on #IDXFRAG (DbName,ObjName,IdxName)
-- tip: you can re-run in SSMS for different db's by
declare tblcur cursor for
select object_id, TblName=schema_name(schema_id)+'.'+name
from sys.tables where type = 'U'
order by TblName
declare @dbid smallint, @objid int, @TblName nvarchar(257)
select @dbid=db_id() --, @objid=OBJECT_ID('SEDOL_PRICE')
--select @dbid, @objid
open tblcur
fetch next from tblcur into @objid, @TblName
while @@fetch_status=0
begin
-- print @TblName -- DEBUG only
delete from #IDXFRAG where DbName=db_name() and [object_id]=@objid -- allow re-runs and multiple db's
insert into #IDXFRAG
select DbName=db_name(), ObjName=@TblName, IdxName=I.name,
database_id,s.[object_id],s.index_id, partition_number,
index_type_desc,alloc_unit_type_desc,
index_depth,index_level,
avg_fragmentation_in_percent,fragment_count,
avg_fragment_size_in_pages,page_count,
avg_page_space_used_in_percent,record_count,
ghost_record_count, version_ghost_record_count,
min_record_size_in_bytes,max_record_size_in_bytes,
avg_record_size_in_bytes,forwarded_record_count
from SYS.DM_DB_INDEX_PHYSICAL_STATS(@dbid,@objid,NULL,NULL,'SAMPLED') S
join sys.indexes I on I.object_id= @objid and I.index_id=S.index_id
--where S.avg_fragmentation_in_percent > 20 AND S.page_count > 8
fetch next from tblcur into @objid, @TblName
end
close tblcur
deallocate tblcur
go
select DbName, ObjName,IdxName, index_id, partition_number,index_type_desc, index_depth, index_level, avg_fragmentation_in_percent, fragment_count, avg_fragment_size_in_pages, page_count, avg_page_space_used_in_percent, record_count, forwarded_record_count --, ghost_record_count, version_ghost_record_count, min_record_size_in_bytes, max_record_size_in_bytes, avg_record_size_in_bytes
from #IDXFRAG
order by DbName,ObjName,IdxName -- IDXFRAG_CI
go
After you execute the statement, you can view the index fragmentation percentage of all tables in the current database.
Best regards,
Bert Zhou
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.