To know which Tables need rebuild index in MSSQL


Some times we need to rebuild the index of the tables in MS-Sql database .In those case we consider the which tables real need the rebuild of the index.As per my knowledge below query is help to know which tables need  rebuild of the index .

Declare @dbid int
set @dbid=db_id()
Select convert(decimal(5,2), avg_fragmentation_in_percent) as  avg_fragmentation_percent,
object_name(d.object_id) as [table],
i.name as [name], d.index_type_desc,
alloc_unit_type_desc as [type],
index_depth, index_level,
fragment_count, page_count
from sys.dm_db_index_physical_stats( @dbid,null, -1, null, ‘SAMPLED’) d
inner join sys.indexes i on i.object_id=d.object_id and i.index_id=d.index_id
where  avg_fragmentation_in_percent>30

Advertisements
This entry was posted in Articles and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s