Skip to content
 

Find Table Fragmentation


DECLARE @Database 	VARCHAR(255)
DECLARE @TableName 	VARCHAR(255)
DECLARE @IndexName	VARCHAR(255) 

SET @Database 	= 'SQLServerPlanet'
SET @TableName	= 'Users'
SET @IndexName 	= NULL

SELECT
	avg_fragmentation_in_percent
	,page_count
FROM sys.dm_db_index_physical_stats
(
	DB_ID(@Database)
	,OBJECT_ID(@TableName)
	,OBJECT_ID(@IndexName)
	,NULL
	,NULL
)

Or you can still do it the old fashioned way. Just substitute the name of the table or index below. It should not be in quotes.

–Just the table
DBCC SHOWCONTIG (tablename)

–Table with the index
DBCC SHOWCONTIG (tablename, indexname)

–Do not block anything (run during prod hours)
DBCC SHOWCONTIG (tablename, indexname) WITH FAST –(2005)



Popular search terms:

post a comment OR Post Your Question on our ASK! Community!