SQL2005重新生成索引的的存儲過程 sp_rebuild_index 原創(chuàng)
公司運(yùn)營著的網(wǎng)站,流量很大,網(wǎng)站是交互式的,經(jīng)常在過了三四個月的時候索引生成的碎片就很多,由于很大一部分頁面沒有生成靜態(tài),這就導(dǎo)致網(wǎng)站在打開的速度上會變慢。
以前都是手工右擊索引重新生成,但是索引太多,操作起來費(fèi)時費(fèi)力,索引在網(wǎng)上找了個存儲過程,自己整理了一下,執(zhí)行的時候只需要選擇相應(yīng)的數(shù)據(jù)庫,運(yùn)行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 -- 當(dāng)邏輯碎片百分比 > 5% 重新生成索引 ) as begin /* 調(diào)用方法: 1.針對當(dāng)前實例所有數(shù)據(jù)庫: exec sys.sp_MSforeachdb 'use ?;exec sp_rebuild_index' 2.針對當(dāng)前數(shù)據(jù)庫: exec sp_rebuild_index */ --對系統(tǒng)數(shù)據(jù)庫不作重新組織索引和重新生成索引 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'對數(shù)據(jù)庫 '+quotename(db_name())+N' 進(jìn)行索引優(yōu)化'+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
你可能在執(zhí)行過程中會遇到如下錯誤
消息 195,級別 15,狀態(tài) 10,過程 sp_rebuild_index,第 24 行
'object_schema_name' 不是可以識別的 內(nèi)置函數(shù)名稱。
不要擔(dān)心,那是由于沒有安裝SQL Server SP4補(bǔ)丁造成的,安裝一下補(bǔ)丁即可。
相關(guān)文章
SQL2005CLR函數(shù)擴(kuò)展-深入環(huán)比計算的詳解
環(huán)比就是本月和上月的差值所占上月值的比例。在復(fù)雜的olap計算中我們經(jīng)常會用到同比環(huán)比等概念,要求的上個維度的某個字段的實現(xiàn)語句非常簡練,比如ssas的mdx語句類似[維度].CurrentMember.Prevmember就可以了2013-06-06SqlServer2005 自動備份并存儲另一電腦上的存儲過程函數(shù)
SqlServer2005自動備份并存儲另一電腦上的存儲函數(shù),想要備份的朋友可以參考下。2011-06-06sql2005創(chuàng)建遠(yuǎn)程登錄帳戶的sql語句
有時候我們需要創(chuàng)建遠(yuǎn)程登錄賬號,這里簡單分享下,方便需要的朋友2013-04-04詳解刪除SQL Server 2005 Compact Edition數(shù)據(jù)庫
這篇文章主要介紹了詳解刪除SQL Server 2005 Compact Edition數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下2017-06-06MSSQL 大量數(shù)據(jù)時,建立索引或添加字段后保存更改提示超時的解決方法
一般我們都喜歡用數(shù)據(jù)庫管理器的UI來對數(shù)據(jù)表結(jié)構(gòu)進(jìn)行更改,然后自然而然地點(diǎn)"保存"按鈕進(jìn)行保存,但數(shù)據(jù)量比較大的時候,用這招往往會出現(xiàn)"無法創(chuàng)建索引“IX_索引名”。 超時時間已到。在操作完成之前超時時間已過或服務(wù)器未響應(yīng)。2011-08-08Sql server 2005安裝時ASP.Net版本注冊要求警告的解決方法
這篇文章主要介紹了Sql server 2005安裝時ASP.Net版本注冊要求警告的解決方法,需要的朋友可以參考下2015-01-01SQL2005CLR函數(shù)擴(kuò)展-繁簡轉(zhuǎn)換的實現(xiàn)代碼
本篇文章是對繁簡轉(zhuǎn)換的實現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06SQLServer 2005數(shù)據(jù)庫連接字符串 連接sql2005必備資料
現(xiàn)在好多朋友開始用sqlserver 2005了,因為跟sql2000有區(qū)別,所以這里整理了下,需要的朋友可以參考下。2010-04-04