SQL server数据库表碎片比例查询语句
程序员文章站
2022-06-25 08:22:54
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.
下一篇: 即兴随笔之数据库
推荐阅读
-
SQLServer中用T—SQL命令查询一个数据库中有哪些表的sql语句
-
查询数据库有哪些表,有多少张表 sql语句
-
SQL Server数据库按百分比查询出表中的记录数
-
数组数据通过sql语句转为数据库表衔接到from或join后进行直接或关联查询
-
sql server实现在多个数据库间快速查询某个表信息的方法
-
不同数据库Oracle MySQL SQL Server DB2 infomix sybase分页查询语句
-
SQLServer中用T—SQL命令查询一个数据库中有哪些表的sql语句
-
sql server 在多个数据库间快速查询 某个 表 的信息以及某个 列
-
sql语句查询数据库中的表名/列名/主键/自动增长值实例
-
SQL Server使用Merge语句当源表数据集为空时,无法进行查询的问题