In doing research for index maintenance, I found some significant differences between the page counts reported by sys.dm_db_index_physical_stats
and sys.allocation_units
in some of the clustered indexes on one of our servers.
For one table, allocation_units
reports 6706 data_pages and 15203 used_pages, while dm_db_index_physical_stats
reports 4079 leaf pages and 15 non-leaf pages.
I don't know when the last time any maintenance was done on the indexes. It is possible that they have not been reorganized or rebuilt since they were initially loaded to the server. Rebuilding the index does sync the stats.
I'd be interested in knowing what caused it to get out of sync and if the extra space reported by allocation_units
is actually reserved on the disk.
The query I used the following query for allocation_units
. It returned 1 row of IN_ROW_DATA
.
SELECT *
FROM
sys.indexes idx
INNER JOIN sys.partitions part
ON idx.object_id = part.object_id
AND idx.index_id = part.index_id
INNER JOIN sys.allocation_units alloc
ON part.partition_id = alloc.container_id
WHERE
idx.type = 1
AND idx.object_id = OBJECT_ID('dbo.Table')
Best Answer
Page counts shown by
sys.dm_db_index_physical_stats
andsys.allocation_units
can vary significantly when a variety of DDL modifications are made to the object concerned.From the Microsoft Docs page on
sys.sp_spaceused
sys.dm_db_index_physical_stats
has the following definition (in SQL Server 2016):As you can see, it uses the internal table
INDEXANALYSIS
, whereassys.allocation_units
uses a different source,ALUCOUNT
, as shown in its definition:These sources vary in their level of consistency as you can see in the output of your queries. If you require absolute certainty about the count of pages used by each, you should do the following:
EXEC sys.sp_spaceused
with the@updateusage = 'updateusage'
option, orDBCC UPDATEUSAGE
.CHECKPOINT
to ensure all modifications have been written to disk.sys.dm_db_index_physical_stats
andsys.allocation_units
; the counts should now be the same.