Answers:
要在2005/2008中检查碎片,可以使用以下脚本。您需要设置@DB
和@Table
值。如果您将它们定义为,NULL
那么它将在所有数据库和/或所有表上运行。如果执行单个数据库,请确保在该数据库的上下文(USE MyDB
)中执行。
SELECT
object_name(IPS.object_id) AS [Table Name],
SI.name AS [Index Name],
CASE IPS.Index_type_desc
WHEN 'CLUSTERED INDEX' THEN 'Clustered'
ELSE 'Non-Clustered'
END AS 'Index Type',
IPS.avg_fragmentation_in_percent as 'Avg Fragmentation (%)',
IPS.avg_fragment_size_in_pages as 'Avg Frag Size (pages)',
IPS.page_count as 'Page Count',
IPS.forwarded_record_count as 'Forwarded Records',
--IPS.avg_page_space_used_in_percent as 'Avg Page Space Used (%)',
--IPS.record_count as 'Record Count',
--IPS.ghost_record_count as 'Ghost Record Count',
IPS.fragment_count as 'Fragment Count'
FROM sys.dm_db_index_physical_stats
(
db_id(@DB),
OBJECT_ID(@Table),
NULL,
NULL ,
'LIMITED'
) as IPS
JOIN sys.indexes as SI WITH (nolock)
ON IPS.object_id = SI.object_id
AND IPS.index_id = SI.index_id
ORDER BY 1,3,5
对于自动收缩,您只需检查master.sys.databases
:
select * from master.sys.databases
where is_auto_shrink_on = 1
sys
数据库中具有物理统计信息吗?