SQL2005重新生成索引的的存储过程 sp_rebuild_index <font color=red>原创</font>
作者:yourber 时间:2024-01-14 21:00:47
公司运营着的网站,流量很大,网站是交互式的,经常在过了三四个月的时候索引生成的碎片就很多,由于很大一部分页面没有生成静态,这就导致网站在打开的速度上会变慢。
以前都是手工右击索引重新生成,但是索引太多,操作起来费时费力,索引在网上找了个存储过程,自己整理了一下,执行的时候只需要选择相应的数据库,运行exec sp_rebuild_index即可,如下。
USE [master]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[sp_rebuild_index]
(
@Rebuild_Fragmentation_Percent smallint = 5 -- 当逻辑碎片百分比 > 5% 重新生成索引
)
as
begin
/* 调用方法:
1.针对当前实例所有数据库: exec sys.sp_MSforeachdb 'use ?;exec sp_rebuild_index'
2.针对当前数据库: exec sp_rebuild_index
*/
--对系统数据库不作重新组织索引和重新生成索引
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 > @Rebuild_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))+' rebuild;' as [sql]
,N'重新生成索引:' +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 > @Rebuild_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
print @sql
exec(@sql)
print @str
fetch next from cur_x into @sql,@str
end
close cur_x
deallocate cur_x
end
你可能在执行过程中会遇到如下错误
消息 195,级别 15,状态 10,过程 sp_rebuild_index,第 24 行
'object_schema_name' 不是可以识别的 内置函数名称。
不要担心,那是由于没有安装SQL Server SP4补丁造成的,安装一下补丁即可。
标签:SQL2005,索引,存储过程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Django框架创建mysql连接与使用示例
2024-01-18 10:38:26
eWebEditor不支持IE8/IE7的解决方法
2010-02-28 10:27:00
![](https://img.aspxhome.com/file/UploadPic/20102/28/4270100787_166776e513_o-72s.jpg)
iPhone应用设计趋势[译]
2009-11-27 19:52:00
MySQL下载安装配置详细教程 附下载资源
2024-01-29 10:15:41
![](https://img.aspxhome.com/file/2023/7/119727_0s.png)
SQL SERVER触发器详解
2024-01-22 01:50:00
![](https://img.aspxhome.com/file/2023/9/115299_0s.jpg)
Python实现周期性抓取网页内容的方法
2023-04-12 01:33:36
python模块中pip命令的基本使用
2023-09-28 22:44:20
![](https://img.aspxhome.com/file/2023/0/100260_0s.png)
Python数据结构与算法之图的广度优先与深度优先搜索算法示例
2022-05-05 07:34:53
python中使用sys模板和logging模块获取行号和函数名的方法
2022-07-19 14:58:22
asp 存储过程分页代码第1/2页
2011-04-03 10:39:00
![](https://img.aspxhome.com/file/UploadPic/20112/5/201125112228776s.jpg)
从品牌网站看交互设计
2009-08-18 12:39:00
![](https://img.aspxhome.com/file/UploadPic/20098/18/1-31s.jpg)
Dreamweaver 4.0使用技巧集锦
2007-11-08 12:47:00
python3.x实现base64加密和解密
2023-12-17 00:30:33
如何利用Python实现一个论文降重工具
2021-02-04 08:11:28
![](https://img.aspxhome.com/file/2023/3/108133_0s.png)
Mysql入门系列:MYSQL日志文件维护
2008-11-24 13:10:00
![](https://img.aspxhome.com/file/UploadPic/200811/20081129183218725.jpg)
mysql时区查看与设置方法
2024-01-20 02:01:46
js自动闭合html标签(自动补全html标记)
2023-08-25 07:06:35
Go获取与设置环境变量的方法详解
2023-06-21 04:35:10
python openCV实现摄像头获取人脸图片
2021-08-16 08:21:03
![](https://img.aspxhome.com/file/2023/5/68475_0s.jpg)
Python+matplotlib实现计算两个信号的交叉谱密度实例
2023-05-12 23:29:55
![](https://img.aspxhome.com/file/2023/7/134737_0s.png)