datetime century date into pad dynamic cursor money percent sp job isnumeric isdate over update
SQLUSA.com
SQL 2008 GRAND SLAM ON 49 CD
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL Server Training SQL 2005 Scripts SQL 2008 Articles
SQL JOBS News Format Developer
How to check fragmentation for all the indexes?

Execute the following Microsoft SQL Server T-SQL script in Management Studio Query Editor to generate >DBCC SHOWCONTIG< commands for all the indexes in AdventureWorks database:

-- Find index fragmentation for a specific table

SELECT index_id, index_type_desc,

avg_fragmentation_in_percent

FROM sys.dm_db_index_physical_stats

(

  DB_ID('AdventureWorks2008'),

  OBJECT_ID('Sales.SalesOrderDetail'),

  NULL,

  NULL,

  NULL

);

/* index_id index_type_desc   avg_fragmentation_in_percent

1     CLUSTERED INDEX   0.242914979757085

2     NONCLUSTERED INDEX      0.735294117647059

3     NONCLUSTERED INDEX      3.08370044052863

6     NONCLUSTERED INDEX      0.797872340425532  */

------------

-- Microsoft SQL Server index fragmentation script generator

USE AdventureWorks;

 

SELECT   'dbcc showcontig (' + convert(VARCHAR,i.[object_id]) + ',' + -- table id 

          convert(VARCHAR,i.index_id) + ') -- ' + -- index id 

          s.name + -- schema name

          '.' + object_name(i.[object_id]) + -- table name 

          '.' + i.name -- index name

FROM     sys.schemas s

         JOIN sys.objects o

           ON (s.schema_id = o.schema_id)

         JOIN sys.indexes i

           ON (o.[object_id] = i.[object_id])

WHERE    o.TYPE = 'U'

         AND i.TYPE IN (1,2)

ORDER BY s.name,

         object_name(i.[object_id]),

         i.index_id

GO

/* Partial results

 

dbcc showcontig (642101328,1) --
-- Sales.SalesOrderDetail.PK_SalesOrderDetail_SalesOrderID_SalesOrderDetailID

dbcc showcontig (642101328,2) -- Sales.SalesOrderDetail.AK_SalesOrderDetail_rowguid

dbcc showcontig (642101328,3) -- Sales.SalesOrderDetail.IX_SalesOrderDetail_ProductID

dbcc showcontig (754101727,1) -- Sales.SalesOrderHeader.PK_SalesOrderHeader_SalesOrderID

dbcc showcontig (754101727,2) -- Sales.SalesOrderHeader.AK_SalesOrderHeader_rowguid

dbcc showcontig (754101727,3) --
-- Sales.SalesOrderHeader.AK_SalesOrderHeader_SalesOrderNumber

*/

Related article:

Index Fragmentation Report in SQL Server 2005 and 2008

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
Microsoft SQL Server 2012 Training Videos at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Accounting
Administrative
Advertising
Arts
Architecture
Banking
Business Intelligence
Career Jobs
Celebrity
Computer
Consulting
Customer Service
Education
Engineering
Entertainment
Entry Level
Executive
Federal
Finance
Government
Hardware
Healthcare
Hospital
Human Resources
Information Technology
Insurance
Internet
Job Openings
Laboratory
Law Enforcement
Legal
Logistics
Manufacturing
Marketing
Medical
Military
Nursing
Pharmaceutical
Physician
Public Relations
Publishing
Real Estate
Restaurant
Retail
Sales
Social Media
Software
SQL Database
Telecomm
Therapist
Training
Transportation
Truck Driver
Travel
Web
Work from Home

FREE SS SQL / BI OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011 Microsoft Community Contributor 2012

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts

JOIN US ON TWITTER

Copyright 2005-2012, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.