Finding Fragmentation of a set of tables in the DB

Following query will give the fragmentation information of clustered indexes of all the tables in your database.

SELECT ‘SELECT  OBJECT_NAME(object_id), index_type_desc, avg_fragmentation_in_percent FROM sys.dm_db_index_physical_stats (DB_ID(N”’+db_name(db_id())+”’),
OBJECT_ID(N”’+OBJECT_NAME(id)+”’), NULL, NULL , NULL) WHERE alloc_unit_type_desc = ”IN_ROW_DATA” AND index_type_desc = ”CLUSTERED INDEX”’
FROM sys.sysindexes
WHERE  OBJECT_NAME(id) IN( ‘table1′,’table2’, ‘table3’, ‘table4’, ‘table5’, ‘table6’)
 AND name not like ‘_WA%’– Removing _WA_Sys_ like objects..
 order by OBJECT_NAME(id), indid

_WA_Sys_ are nothing but auto stats on table. Read my another blog to find out info about this.

Online MSSQL Classes @ http://tuitionaffordable.webstarts.com

INTERVIEW PREPARATION IN 30 MINs – Read Following wires for a quick interview preparation.

  1. SQL Server DBA “Interview Questions And Answers”
  2. SQL Azure Interview Questions and Answers Part – 1
  3. SQL Azure Interview Questions and Answers Part – 2
  4. Powershell Interview Questions and Answers

Regards,

Online Powershell Classes @ http://tuitionaffordable.webstarts.com

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s