我正在使用此博客http://technet.microsoft.com/en-us/library/ms189858(v=sql.110).aspx重新构建/重新组织我的索引。大部分时间它的工作除了少数情况。例如,
SELECT
dbschemas.[Name] AS 'Schema',
dbtables.[Name] AS 'Table',
dbindexes.[Name] AS 'Index',
indexstats.avg_fragmentation_in_percent,
indexstats.page_count
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) AS indexstats
INNER JOIN sys.TABLES dbtables
ON dbtables.[object_id] = indexstats.[object_id]
INNER JOIN sys.schemas dbschemas
ON dbtables.[schema_id] = dbschemas.[schema_id]
INNER JOIN sys.indexes AS dbindexes
ON dbindexes.[object_id] = indexstats.[object_id]
AND indexstats.index_id = dbindexes.index_id
WHERE indexstats.database_id = DB_ID()
ORDER BY indexstats.avg_fragmentation_in_percent DESC
会告诉我这个结果。
Schema Table Index avg_fragmentation_in_percent page_count
---------------------------------------------------------------------------------------------------------
dbo DealGallery NULL 83.3333333333333 7
dbo UserRewardActions NULL 77.2034742987327 56153
dbo Order PK__Order__3214EC275CFA57FE 50 2
dbo RetailersTranslations PK__Retailer__3214EC2736153FBA 50 2
dbo Batch PK__Batch__3214EC2786046795 50 2
dbo BatchProducts PK__BatchPro__3214EC2709FBECDC 50 2
dbo ProductDeals PK__ProductD__3214EC27CE27FF86 50 2
dbo PopTemplates PK__PopTempl__3214EC279BBD3055 21.0526315789474 19
dbo ELMAH_Error NULL 10.3310852237891 52113
重建上述索引无法正常工作。我正在使用,
ALTER INDEX ALL ON DealGallery
REBUILD WITH (FILLFACTOR = 80, SORT_IN_TEMPDB = ON,
STATISTICS_NORECOMPUTE = ON);
ALTER INDEX ALL ON DealGallery
REORGANIZE ;
也有人可以解释为什么它在Index列中显示为NULL?
答案 0 :(得分:1)
重建/重组工作正常。您正在阅读您的查询结果不正确。
dm_db_index_physical_stats fincation有index_id,显示堆和索引信息提示。
indexstats.index_id = 0 -- Heap or table
indexstats.index_id > 0 -- Indexes
下面的行将从查询中删除NULL
值。
--and indexstats.index_id > 0
SELECT dbschemas.[Name] AS 'Schema',
dbtables.[Name] AS 'Table',
dbindexes.[Name] AS 'Index',
indexstats.avg_fragmentation_in_percent,
indexstats.page_count
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) AS indexstats
INNER JOIN sys.TABLES dbtables
ON dbtables.[object_id] = indexstats.[object_id]
INNER JOIN sys.schemas dbschemas
ON dbtables.[schema_id] = dbschemas.[schema_id]
INNER JOIN sys.indexes AS dbindexes
ON dbindexes.[object_id] = indexstats.[object_id]
AND indexstats.index_id = dbindexes.index_id
WHERE indexstats.database_id = DB_ID()
and indexstats.index_id > 0
ORDER BY indexstats.avg_fragmentation_in_percent DESC