经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MS SQL Server » 查看文章
SQL server数据库表碎片比例查询语句
来源:cnblogs  作者:【舍予】  时间:2018/12/24 10:28:23  对本文有异议

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

 

  1. use [database_name]
  2.  
  3. SELECT dbschemas.[name] as 'Schema',
  4.  
  5. dbtables.[name] as 'Table',
  6.  
  7. dbindexes.[name] as 'Index',
  8.  
  9. indexstats.avg_fragmentation_in_percent,
  10.  
  11. indexstats.page_count
  12.  
  13. FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS indexstats
  14.  
  15. INNER JOIN sys.tables dbtables on dbtables.[object_id] = indexstats.[object_id]
  16.  
  17. INNER JOIN sys.schemas dbschemas on dbtables.[schema_id] = dbschemas.[schema_id]
  18.  
  19. INNER JOIN sys.indexes AS dbindexes ON dbindexes.[object_id] = indexstats.[object_id]
  20.  
  21. AND indexstats.index_id = dbindexes.index_id
  22.  
  23. WHERE indexstats.database_id = DB_ID()
  24.  
  25. 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.

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号