Solved

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

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need some help to cast ntext to nvarchar SQL 2000 7 34
sql query to calculate avaerage 21 42
SQL View nearest date 5 38
T-SQL: New to using transactions 9 31
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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
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.

831 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