在我们的 SQL Server 数据库中应该多久重建一次索引?

发布于 2024-08-21 13:26:49 字数 86 浏览 3 评论 0原文

目前我们的数据库大小为 10 GB,并且每月增长约 3 GB。我经常听说人们应该不时地重建索引,以提高查询执行时间。那么在给定场景中我应该多久重建一次索引呢?

Currently our database has size 10 GB and is growing by around 3 GB per month. Often I hear that one should from time to time rebuild the indexes, to improve the query execution time. So how often should I rebuild the indexes in the given scenario?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深海蓝天 2024-08-28 13:26:49

人们普遍认为,一旦索引碎片达到 5(有时 10%)以上,您就应该重新组织(“碎片整理”)索引,并且当超过 30%(至少这是我的数字)时,您应该完全重建它们。听说很多地方都提倡)。

Michelle Ufford(又名“SQL Fool”)有一个自动索引碎片整理脚本,它使用这些确切的限制来决定何时重组或重建索引。

另请参阅 Brad McGehee 关于重建索引的技巧,其中包含一些关于如何重建索引的好想法和技巧处理索引重建。


我在这里使用这个脚本(不记得我什么时候从谁那里得到这个脚本的:非常感谢!非常有用的东西)来显示给定数据库中所有索引的索引碎片:

SELECT 
    t.NAME 'Table name',
    i.NAME 'Index name',
    ips.index_type_desc,
    ips.alloc_unit_type_desc,
    ips.index_depth,
    ips.index_level,
    ips.avg_fragmentation_in_percent,
    ips.fragment_count,
    ips.avg_fragment_size_in_pages,
    ips.page_count,
    ips.avg_page_space_used_in_percent,
    ips.record_count,
    ips.ghost_record_count,
    ips.Version_ghost_record_count,
    ips.min_record_size_in_bytes,
    ips.max_record_size_in_bytes,
    ips.avg_record_size_in_bytes,
    ips.forwarded_record_count
FROM 
    sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'DETAILED') ips
INNER JOIN  
    sys.tables t ON ips.OBJECT_ID = t.Object_ID
INNER JOIN  
    sys.indexes i ON ips.index_id = i.index_id AND ips.OBJECT_ID = i.object_id
WHERE
    AVG_FRAGMENTATION_IN_PERCENT > 0.0
ORDER BY
    AVG_FRAGMENTATION_IN_PERCENT, fragment_count
    

There's a general consensus that you should reorganize ("defragment") your indices as soon as index fragmentation reaches more than 5 (sometimes 10%), and you should rebuild them completely when it goes beyond 30% (at least that's the numbers I've heard advocated in a lot of places).

Michelle Ufford (a.k.a. "SQL Fool") has an automated index defrag script, which uses those exact limits for deciding when to reorganize or rebuild an index.

Also see Brad McGehee's tips on rebuild indexes with some good thoughts and tips on how to deal with index rebuilding.


I use this script here (can't remember when I got this from - whoever it was: many thanks! Really helpful stuff) to display the index fragmentation on all your indices in a given database:

SELECT 
    t.NAME 'Table name',
    i.NAME 'Index name',
    ips.index_type_desc,
    ips.alloc_unit_type_desc,
    ips.index_depth,
    ips.index_level,
    ips.avg_fragmentation_in_percent,
    ips.fragment_count,
    ips.avg_fragment_size_in_pages,
    ips.page_count,
    ips.avg_page_space_used_in_percent,
    ips.record_count,
    ips.ghost_record_count,
    ips.Version_ghost_record_count,
    ips.min_record_size_in_bytes,
    ips.max_record_size_in_bytes,
    ips.avg_record_size_in_bytes,
    ips.forwarded_record_count
FROM 
    sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'DETAILED') ips
INNER JOIN  
    sys.tables t ON ips.OBJECT_ID = t.Object_ID
INNER JOIN  
    sys.indexes i ON ips.index_id = i.index_id AND ips.OBJECT_ID = i.object_id
WHERE
    AVG_FRAGMENTATION_IN_PERCENT > 0.0
ORDER BY
    AVG_FRAGMENTATION_IN_PERCENT, fragment_count
    
向地狱狂奔 2024-08-28 13:26:49

“当你需要的时候”和“当你可以的时候”!

例如...

  • 首先测试碎片,然后决定是否不执行任何操作、重组或重建。
    SQL Fool 的脚本执行此操作,例如,具有 @minFragmentation@rebuildThreshold 参数

  • 每天进行统计,但在周末进行索引。您的维护窗口是多少?

"When you need to" and "When you can"!

For example...

  • Test for fragmentation first and decide whether to do nothing, reorg or rebuild.
    SQL Fool's script does this, for example, has @minFragmentation and @rebuildThreshold parameters

  • Do statistics daily, say, but indexes at weekends. What is your maintenance window?

月亮是我掰弯的 2024-08-28 13:26:49

您应该经常重建索引,以便生产不会因索引降级而受到不利影响。我知道这似乎很模糊,但所有数据库都是不同的,并且以不同的方式使用。您只需要定期重建/碎片整理会产生写入操作(插入/更新)的索引 - 您的静态表或大部分只读表不需要太多重新索引。

您将需要使用 dbcc showcontig([Table]) 来检查索引的碎片级别,确定它们产生碎片的频率以及碎片实际的级别。

当索引碎片过多(超过 20%-30% 左右)时,使用 dbcc dbreindex([Table]) 完全重建索​​引,但如果找不到足够大的停机时间窗口且碎片级别为相对较低 (1%-25%),您应该使用 dbcc indexdefrag([Database], [Table], [Index]) 以“在线”方式对索引进行碎片整理。另请记住,您可以停止索引碎片整理操作并稍后重新启动,而不会丢失任何工作。

保持数据库及其索引“协调”需要进行一些监控才能真正了解何时以及什么内容需要重新索引。

You should rebuild indexes often enough so that production is not detrimentally affected by index degradation. I understand that this seems vague, but all databases are different and are used in different ways. You only need to regularly rebuild/defrag indexes that incur write operations (inserts/updates) – your static or mostly read only tables will not need much reindexing.

You will need to use dbcc showcontig([Table]) to check the fragmentation level of your indexes, determine how often they become fragmented and as to what level the fragmentation actually is.

Use dbcc dbreindex([Table]) to totally rebuild the indexes when they become too fragmented (above 20%-30% or so) but if you cannot find a large enough downtime window and the fragmentation level is relatively low (1%-25%), you should use dbcc indexdefrag([Database], [Table], [Index]) to defrag the index in an "online" fassion. Also keep in mind, that you can stop the index defrag operation and start it again at a later time without losing any work.

Keeping a database and its indexes "in tune" takes a bit of monitoring to really get a feel for when and what to reindex.

落日海湾 2024-08-28 13:26:49

考虑到数据库的大小,您可以轻松地每月重建一次索引。但随着大小的增加,例如大约 500 GB,您可以每月执行两次。

Given the size of your database, you can easily rebuild the indexes once per month. But as the size increases, say to around 500 GB, you could do it twice per month.

小…红帽 2024-08-28 13:26:49

Bacon Bits 评论中提到< /a> Ola Hallengren 的 SQL Server 维护解决方案  SQL Server 2008、SQL Server 2008 R2 支持 IndexOptimize 、SQL Server 2012、SQL Server 2014、SQL Server 2016、SQL Server 2017、SQL Server 2019、Azure SQL 数据库和 Azure SQL 数据库托管实例。
它在 https://github.com/olahallengren/sql-server-maintenance 上有 2K 颗星-解决方案

Michelle Ufford(又名“SQL Fool”)的自动索引碎片整理脚本< /a>,在接受的答案中建议,从概念上看似乎与 Ola Hallengren 的 SQL Server 维护解决方案相同, ;但最新版本是2011年的。

Mentioned in Bacon Bits comment Ola Hallengren's SQL Server Maintenance Solution  IndexOptimize is supported on SQL Server 2008, SQL Server 2008 R2, SQL Server 2012, SQL Server 2014, SQL Server 2016, SQL Server 2017, SQL Server 2019, Azure SQL Database, and Azure SQL Database Managed Instance.
It has 2K stars on https://github.com/olahallengren/sql-server-maintenance-solution.

Michelle Ufford (a.k.a. "SQL Fool") 's automated index defrag script, suggested in the accepted answer, conceptually seems does the same as  Ola Hallengren's SQL Server Maintenance Solution, but the latest version is in 2011.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文