重新组织和重新生成索引sp_RefreshIndex
开始:
在上周,客户反映一个系统问题,当处理大量数据的时候,出现网络超时。后来,我们跟踪测试,发现是由于索引碎片多而引起的网络超时。
解决方法,自然是重新组织和重新生成索引。在这里,我写了一个存储过程sp_RefreshIndex来实现。
存储过程sp_RefreshIndex:
use master go if object_id('sp_RefreshIndex') Is not null Drop Proc sp_RefreshIndex Go create proc sp_RefreshIndex ( @Reorganize_Fragmentation_Percent smallint = 5 -- 当逻辑碎片百分比 > 5% 重新组织索引 ,@Rebuild_Fragmentation_Percent smallint = 30 -- 当逻辑碎片百分比 > 30% 重新生成索引 ) as begin /* 调用方法: 1.针对当前实例所有数据库: exec sys.sp_MSforeachdb 'use ?;exec sp_RefreshIndex' 2.针对当前数据库: exec sp_RefreshIndex */ --对系统数据库不作重新组织索引和重新生成索引 if (db_name() in ('master','model','msdb','tempdb')) return; --如果逻辑碎片(索引中的无序页)的百分比 <= 5% ,就不作重新组织索引和重新生成索引 if not exists(select 1 from sys.dm_db_index_physical_stats(db_id(),null,null,null,null) a where a.index_id>0 and a.avg_fragmentation_in_percent > @Reorganize_Fragmentation_Percent) return print replicate('-',60)+char(13)+char(10)+replicate(' ',14)+N'对数据库 '+quotename(db_name())+N' 进行索引优化'+replicate(' ',20)+char(13)+char(10) declare @sql nvarchar(2000),@str nvarchar(2000) declare cur_x cursor for select 'alter index '+quotename(a.name)+' on '+quotename(object_schema_name(a.object_id))+'.'+quotename(object_name(a.object_id))+case when b.avg_fragmentation_in_percent<=@Rebuild_Fragmentation_Percent then ' reorganize;'else ' rebuild;'end as [sql] ,case when b.avg_fragmentation_in_percent<=@Rebuild_Fragmentation_Percent then N'重新组织索引:' else N'重新生成索引:'end +quotename(object_schema_name(a.object_id))+'.'+quotename(object_name(a.object_id))+'.'+quotename(a.name) as [str] from sys.indexes a inner join sys.dm_db_index_physical_stats(db_id(),null,null,null,null) b on b.object_id=a.object_id and b.index_id=a.index_id where a.index_id>0 and b.avg_fragmentation_in_percent > @Reorganize_Fragmentation_Percent order by object_name(a.object_id),a.index_id open cur_x fetch next from cur_x into @sql,@str while (@@fetch_status = 0) begin exec(@sql) print @str fetch next from cur_x into @sql,@str end close cur_x deallocate cur_x end go exec sp_ms_marksystemobject 'sp_RefreshIndex' go
调用方法:
use master go exec sys.sp_MSforeachdb 'use ?;exec sp_RefreshIndex' go
注:我们根据实际的环境,修改@Reorganize_Fragmentation_Percent 和 @Rebuild_Fragmentation_Percent 值。
存储过程 sp_RefreshIndex 已在下面的环境测试通过:
SQL Server 2005 (SP4)/2008/2008R2/2012
扩展:
我们可以把上面的SQL代码写入Job。再通过SQL Agent 服务,选择一个月或两个月执行一次job。
如果要了解重新组织和重新生成索引的具体帮助内容,可以参考:
http://msdn.microsoft.com/zh-cn/library/ms189858%28v=sql.105%29.aspx
http://msdn.microsoft.com/zh-cn/library/ms188388%28v=sql.105%29.aspx
http://msdn.microsoft.com/zh-cn/library/ms188917%28v=sql.105%29.aspx
分类:
SQL Server
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
2009-12-27 使用SQL语句直接保存图片