You are on page 1of 5

Journey to SQL Authority with Pinal Dave

SQL, SQL Server, MySQL, Big Data and NoSQL

JANUARY 10, 2008 BY PINAL DAVE

SQL SERVER – 2005 – Display Fragmentation


Information of Data and Indexes of
Database Table

One of my friend involved with large business of medical transcript invited me for SQL Server
improvement talk last weekend. I had great time talking with group of DBA and developers. One of
the topic which was discussed was how to find out Fragmentation Information for any table in one
particular database. For SQL Server 2000 it was easy to find using DBCC SHOWCONTIG
command. DBCC SHOWCONTIG has some limitation for SQL Server 2000.

SQL Server 2005 has sys.dm_db_index_physical_stats dynamic view which returns size and
fragmentation information for the data and indexes of the specified table or view. You can run
following T-SQL for any database to know detailed information of the database.
SELECT *
FROM sys.dm_db_index_physical_stats(DB_ID(), OBJECT_ID('test_contig'),
NULL, NULL , 'DETAILED')

Above query returns lots of information, most of the time we only need to know Tablename,
IndexName and Percentage of Fragmentation. Following query returns only three most important
details mentioned earlier. I have added an extra condition where results are filtered where average
fragmentation is greater than 20%.

SELECT OBJECT_NAME(i.OBJECT_ID) AS TableName,


i.name AS IndexName,
indexstats.avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'DETAILED')
indexstats
INNER JOIN sys.indexes i ON i.OBJECT_ID = indexstats.OBJECT_ID
AND i.index_id = indexstats.index_id
WHERE indexstats.avg_fragmentation_in_percent > 20
The results will help DBA to make necessary reports.

Reference : Pinal Dave (http://blog.SQLAuthority.com (http://blog.SQLAuthority.com))

About these ads (http://en.wordpress.com/about-these-ads/)

This entry was posted in Pinal Dave, SQL, SQL Authority, SQL DMV, SQL Index, SQL
Performance, SQL Query, SQL Scripts, SQL Server, SQL Server DBCC, SQL System Table, SQL
Tips and Tricks, T SQL, Technology. Bookmark the permalink.

16 thoughts on “SQL SERVER – 2005 – Display


Fragmentation Information of Data and Indexes of
Database Table”

J. Saraboji | February 7, 2008 at 5:31 pm


Hello sir,

I am new in cube.
Can u give some examples about cube?

Thanks
JSaraboji.

Reply

jenniffer | February 21, 2008 at 2:57 am


thanks for the script, it has help greatly, now i know which tables have to be defrag… many
thanks!!!

Reply
AHMED | February 21, 2008 at 3:23 am
so then all the tables in that query should be defragmented correct?

Reply

jessica | February 21, 2008 at 11:16 pm


when i ran dbcc showcontig (tablename, indexname)

i got different results, why?


i am using sql server 2005

Reply

SQL_Girl | February 27, 2008 at 3:39 pm


My two main questions regarding this DMV are the following:

1) Using the DETAILED option includes the non-zero index-levels as well (non-leaf pages).
According to many source, this should also be actioned, seeing that this indicates “logical”
fragmentation (meaning that the leaf pages are not in the order of the non-leaf keys any more). I
cannot find anything conclusive on this. Where as the index level=0 occurrences, indicates extent
fragmentation. We are using this this dmv, to check fragmentation-levels first, and based on
that, do the necessary re-index/reorg. Currently we are using the LIMITED option (which only
shows leaf-level), but we seem to be missing indexes that needs to be re-indexed/re-orged.

2) Secondly, the stats that this function return, does not seem to be reliable. I ran the function
prior to an ALTER…. REBUILD, and then again thereafter. There were hardly any differences
in the stats. Is it now a case of the stats being unreliable, or the REBUILD not being effective?

I do not want to be in the situation where we miss indexes because of unreliable stats. We
already suspect that this could be the case.

Can you please comment on this.

Reply

senior dba | February 27, 2008 at 10:15 pm


i agree the stats of this script seem unreliable.

Reply

Rahul | November 21, 2008 at 8:26 pm


Hello,
pl any can tell me what is exact justification report for doing defragmentation in sql 2005
i m and understanding the result of showcontig commands output . it shows Scan Density,
Pages plese tell me what is this and how should i know that fragmentation is increases on what
factos
pl help
Thanks

Reply

Rahul | November 21, 2008 at 8:27 pm


Hello,
pl any can tell me what is exact justification report for doing defragmentation in sql 2005
i m not understanding the result of showcontig commands output . it shows Scan Density, Pages
plese tell me what is this and how should i know that fragmentation is increases on what factos

pl help
Thanks

Reply

Anup Kaluskar | August 11, 2010 at 8:15 pm


Hi Pinal

I am trying to troubleshoot some fragmentation. I saw the script you posted at the beginning
which identifies which tables are fragmented etc.

This is very useful as when you query against ‘sys.dm_db_index_physical_stats’ it only displays
object id’s.

I ran the script & gave the following error “Msg 195, Level 15, State 10 Line 1
‘OBJECTNAME’ is not a recognised built-in function name’.

I would appreciate if you could help me how this error can be resolved.

If anyone else is able to run this script, pls let me know.

Thanks
Anup..

Reply

Anup Kaluskar | August 11, 2010 at 8:51 pm


Hi Pinal

I had a typo in object_name which is why it failed. The script ran fine.

Cheers
Anup..

Reply

rakshith | March 2, 2012 at 6:02 pm


can anyone plz explain me use of index with example of create and display index querys…
Reply
Pingback: SQL SERVER – Cleaning Up SQL Server Indexes – Defragmentation, Fillfactor –
Video « SQL Server Journey with SQL Authority

Shailendra Pali | August 31, 2012 at 10:39 am


How does fragmentation effects the performance or other things?
Quick respose is much appriciated..

Reply

mel | November 12, 2012 at 1:18 pm


syntax error, why is that?

Reply
Pingback: SQL SERVER – Weekly Series – Memory Lane – #011 « SQL Server Journey with
SQL Authority

Lokesh | July 2, 2013 at 9:41 pm


hi…am create a index in sql 2008 but how can i show index values ? plz help me?

Reply

Blog at WordPress.com. | Customized Misty Lake Theme.

You might also like