Solved

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

Posted on 2013-12-18
2
458 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 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

697 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