set nocount on print db_name() declare @tab_name sysname set @tab_name='wfpuser_a0113' --内部碎片 select '【内部】碎片' as frag_type ,ob.name as object_name ,ix.index_id,ix.name as index_name ,ps.index_level ,ps.partition_number ,ps.record_count ,ps.page_count ,ps.fragment_count ,str(ps.avg_fragmentation_in_percent,16,2) as [avg_frag(%)] ,str(ps.avg_fragment_size_in_pages,16,1) as [avg_frag_pages] ,ps.forwarded_record_count ,ps.avg_record_size_in_bytes ,str(ps.avg_page_space_used_in_percent,16,2) as [avg_page_space_used(%)] from sys.dm_db_index_physical_stats(db_id(),object_id(@tab_name),default, default, 'detailed') ps inner join sys.indexes ix on ix.object_id = ps.object_id and ix.index_id = ps.index_id inner join sys.objects ob on ob.object_id=ix.object_id where ob.is_ms_shipped=0 union all --外部碎片 select '外部碎片' as frag_type ,ob.name as object_name ,ix.index_id,ix.name as index_name ,ps.index_level ,ps.partition_number ,ps.record_count ,ps.page_count ,ps.fragment_count ,str(ps.avg_fragmentation_in_percent,16,2) as [avg_frag(%)] ,str(ps.avg_fragment_size_in_pages,16,1) as [avg_frag_pages] ,ps.forwarded_record_count ,ps.avg_record_size_in_bytes ,str(ps.avg_page_space_used_in_percent,16,2) as [avg_page_space_used(%)] from sys.dm_db_index_physical_stats(db_id(),object_id(@tab_name),default, default, 'limited') ps inner join sys.indexes ix on ix.object_id = ps.object_id and ix.index_id = ps.index_id inner join sys.objects ob on ob.object_id=ix.object_id where ob.is_ms_shipped=0 order by object_name,index_id,frag_type