DMV sys.dm_db_index_physical_stats: Detailed vs. Limited

  • When I run the following two queries.  I get drastically different results.   I don't understand why. 

    select o.name, p.object_id, i.name, p.index_id, p.index_type_desc, p.avg_fragmentation_in_percent, p.fragment_count, p.avg_fragment_size_in_pages, p.page_count
    from sys.dm_db_index_physical_stats (7, NULL, NULL, NULL, 'DETAILED') p
    JOIN sys.objects AS o ON p.[object_id] = o.[object_id]
    JOIN sys.indexes AS i ON o.[object_id] = i.[object_id] AND p.index_id = i.index_id
    where avg_fragmentation_in_percent > 0
    order by avg_fragmentation_in_percent desc

    VS.

    select o.name, p.object_id, i.name, p.index_id, p.index_type_desc, p.avg_fragmentation_in_percent, p.fragment_count, p.avg_fragment_size_in_pages, p.page_count
    from sys.dm_db_index_physical_stats (7, NULL, NULL, NULL, 'LIMITED') p
    JOIN sys.objects AS o ON p.[object_id] = o.[object_id]
    JOIN sys.indexes AS i ON o.[object_id] = i.[object_id] AND p.index_id = i.index_id
    where avg_fragmentation_in_percent > 0
    order by avg_fragmentation_in_percent desc

  • Did you Binoogle that DMV? Did you read Books Online about it??

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

  • One scans everything. The other only looks at the root page. Of course the results will be different. Read the documentation for more details.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • I have read the documentation.  But I don't understand it.  I am a "involuntary DBA".   And I have a hard time following most Microsoft documentation.  I tend to look for forum or blog postings that translate it into a "For Dummies" version.

    In my research, looking for an explanation I can follow, I came across this from Paul S. Randal, who has some legit credentials for his expertise.   

    https://www.sqlskills.com/blogs/paul/inside-sys-dm_db_index_physical_stats/

    In here he says,  
    "
    The DETAILED mode does two things:

    • Calculate fragmentation by doing a LIMITED mode scan
    • Calculate all other statistics by reading all pages at every level of the index

    "
    So, then why would the fragmentation calculations be different between LIMITED and DETAILED?   If you look at the fields I'm pulling, fragmentation is all I'm looking at.

  • Barring an explanation I can follow...

    Which should I use for deciding what indexes need reorg/rebuild? 

    Some sites suggest that Detailed is more accurate, but other sites just say it is different / not necessarily better.  

  • Paul is one of the greatest. You'll probably never go wrong reading his stuff and following his advice.

    The difference is in the amount of data being accessed. The full scan of all pages gives you a more complete picture, while putting a much higher cost on the server. The limited scan gives you a less complete picture at a much lower cost. More often than not, LIMITED is adequate, if imperfect.

    On a side-note, fragmentation is seldom a major issue for people. Usually spending time ensuring more accurate statistics works out better than attempting to eliminate fragmentation.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Okay.  Thanks for pointing me in that direction.   Stats mgmt was right after fragmentation on my "to-learn" list. 

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply