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

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.

需要了解更多数据库技术:SQL server数据库表碎片比例查询语句,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/625266.html

(0)
上一篇 2021年5月25日
下一篇 2021年5月25日

精彩推荐