SQL server数据库表碎片比例查询语句

for rebuilding index, here is also a script to figure out the fragmentation and decide whether rebuilding index is in need:

 

use [database_name]

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

 

when the avg_fragmentation_in_percent >30, please rebuild the index (alter index rebuild). if the 5 < avg_fragmentation_in_percent < 30, please reorgnize the index (alter index reorganize)

 

however, as you mentioned that it finished quickly, maybe you can manage it before you run the job each time. just arrange it as the preparation for you job. please update statistics each time you want to run the job.

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐