Solved

Rebuilding indexes and fragmentation.  Why do I see different fragmentation levels with these scripts?

Posted on 2013-12-18
2
448 Views
Last Modified: 2013-12-18
Why does the first script show me indexes over 1000 page count that have hardly any fragmentation but when I run the 2nd script it shows me a list of indexes that are heavily fragmented that also have over 1000 pages.

What is the different between these two scripts?

DECLARE @DatabaseID int

SET @DatabaseID = DB_ID()

SELECT DB_NAME(@DatabaseID) AS DatabaseName,
       schemas.[name] AS SchemaName,
       objects.[name] AS ObjectName,
       indexes.[name] AS IndexName,
       objects.type_desc AS ObjectType,
       indexes.type_desc AS IndexType,
       dm_db_index_physical_stats.partition_number AS PartitionNumber,
       dm_db_index_physical_stats.page_count AS [PageCount],
       dm_db_index_physical_stats.avg_fragmentation_in_percent AS AvgFragmentationInPercent
FROM sys.dm_db_index_physical_stats (@DatabaseID, NULL, NULL, NULL, 'LIMITED') dm_db_index_physical_stats
INNER JOIN sys.indexes indexes ON dm_db_index_physical_stats.[object_id] = indexes.[object_id] AND dm_db_index_physical_stats.index_id = indexes.index_id
INNER JOIN sys.objects objects ON indexes.[object_id] = objects.[object_id]
INNER JOIN sys.schemas schemas ON objects.[schema_id] = schemas.[schema_id]
WHERE objects.[type] IN('U','V')
AND objects.is_ms_shipped = 0
AND indexes.[type] IN(1,2,3,4)
AND indexes.is_disabled = 0
AND indexes.is_hypothetical = 0
AND dm_db_index_physical_stats.alloc_unit_type_desc = 'IN_ROW_DATA'
AND dm_db_index_physical_stats.index_level = 0
AND dm_db_index_physical_stats.page_count >= 1000



SELECT SO.name AS TABLE_NAME,
       SI.name AS INDEX_NAME,
       SO.type_desc,
       DMV.index_type_desc,
       DMV.alloc_unit_type_desc,
       DMV.index_depth,
       DMV.index_level,
       DMV.page_count,
       DMV.avg_fragmentation_in_percent,
       DMV.partition_number
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'LIMITED') as DMV
LEFT OUTER JOIN sys.objects AS SO
ON DMV.OBJECT_ID = SO.OBJECT_ID
LEFT OUTER JOIN sys.indexes AS SI
ON DMV.OBJECT_ID = SI.OBJECT_ID
AND DMV.INDEX_ID = SI.INDEX_ID
WHERE alloc_unit_type_desc = 'IN_ROW_DATA'
      AND page_count > 1000
      AND avg_fragmentation_in_percent > 20
0
Comment
Question by:NBF
2 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 39727586
At first glance, your filters are different.  In the first, you filter sys.indexes by [type] IN (1,2,3,4), which would exclude HEAP (0) for example.  Similarly, in the second query, you filter the average fragmentation percent to those greater than 20 but do not filter the same in the first query.

In other words, look at the filters and make sure they are consistent with what you want to see.
0
 

Author Closing Comment

by:NBF
ID: 39727593
Bingo.  Not sure why I was overlooking the HEAP filter.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now