欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-24 13:35:18
For rebuilding index, here is also a script to figure out the fragmentation and decide whether rebuilding index is in need: When the avg_fragmentation ......

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.