关于重新组织和重新生成索引sp_RefreshIndex的介绍
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了关于重新组织和重新生成索引sp_RefreshIndex的介绍,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3365字,纯文字阅读大概需要5分钟。
内容图文
![关于重新组织和重新生成索引sp_RefreshIndex的介绍](/upload/InfoBanner/zyjiaocheng/587/c220a17d3e5949939a952101fb897206.jpg)
开始:
--------------------------------------------------------------------------------
在上周,客户反映一个系统问题,当处理大量数据的时候,出现网络超时。后来,我们跟踪测试,发现是由于索引碎片多而引起的网络超时。
解决方法,自然是重新组织和重新生成索引。在这里,我写了一个存储过程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
/* 调用方法:
.针对当前实例所有数据库: exec sys.sp_MSforeachdb 'use ?;exec sp_RefreshIndex'
.针对当前数据库: 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。
您可能感兴趣的文章:
- mssql 建立索引
- SQL2000 全文索引完全图解
- MSSQL 大量数据时,建立索引或添加字段后保存更改提示超时的解决方法
- SQL2005CLR函数扩展 - 关于山寨索引
- MSSQL自动重建出现碎片的索引的方法分享
- 理解Sql Server中的聚集索引
- Sql Server中的非聚集索引详细介
- 在SQL SERVER中导致索引查找变成索引扫描的问题分析
- 详解sqlserver查询表索引
- SQL2005重新生成索引的的存储过程 sp_rebuild_index
内容总结
以上是互联网集市为您收集整理的关于重新组织和重新生成索引sp_RefreshIndex的介绍全部内容,希望文章能够帮你解决关于重新组织和重新生成索引sp_RefreshIndex的介绍所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。