?
Solved

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

Posted on 2013-12-18
2
Medium Priority
?
470 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

752 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