關(guān)于重新組織和重新生成索引sp_RefreshIndex的介紹_Mssql數(shù)據(jù)庫教程
推薦:SqlServer獲取存儲過程返回值的實例SqlServer獲取存儲過程返回值的實例,需要的朋友可以參考一下
開始:
--------------------------------------------------------------------------------
在上周,客戶反映一個系統(tǒng)問題,當(dāng)處理大量數(shù)據(jù)的時候,出現(xiàn)網(wǎng)絡(luò)超時。后來,我們跟蹤測試,發(fā)現(xiàn)是由于索引碎片多而引起的網(wǎng)絡(luò)超時。
解決方法,自然是重新組織和重新生成索引。在這里,我寫了一個存儲過程sp_RefreshIndex來實現(xiàn)。
存儲過程sp_RefreshIndex:
復(fù)制代碼 代碼如下:www.wf0088.com
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 -- 當(dāng)邏輯碎片百分比 > 5% 重新組織索引
,@Rebuild_Fragmentation_Percent smallint = 30 -- 當(dāng)邏輯碎片百分比 > 30% 重新生成索引
)
as
begin
/* 調(diào)用方法:
.針對當(dāng)前實例所有數(shù)據(jù)庫: exec sys.sp_MSforeachdb 'use ?;exec sp_RefreshIndex'
.針對當(dāng)前數(shù)據(jù)庫: exec sp_RefreshIndex
*/
--對系統(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 > @Reorganize_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))+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
調(diào)用方法:
復(fù)制代碼 代碼如下:www.wf0088.com
use master
go
exec sys.sp_MSforeachdb 'use ?;exec sp_RefreshIndex'
go
注:我們根據(jù)實際的環(huán)境,修改@Reorganize_Fragmentation_Percent 和 @Rebuild_Fragmentation_Percent 值。
存儲過程 sp_RefreshIndex 已在下面的環(huán)境測試通過:
SQL Server 2005 (SP4)/2008/2008R2/2012
擴(kuò)展:
--------------------------------------------------------------------------------
我們可以把上面的SQL代碼寫入Job。再通過SQL Agent 服務(wù),選擇一個月或兩個月執(zhí)行一次job。
分享:SQL普通表轉(zhuǎn)分區(qū)表的方法SQL普通表轉(zhuǎn)分區(qū)表的方法,需要的朋友可以參考一下
相關(guān)Mssql數(shù)據(jù)庫教程:
- sql 語句練習(xí)與答案
- 深入C++ string.find()函數(shù)的用法總結(jié)
- SQL Server中刪除重復(fù)數(shù)據(jù)的幾個方法
- sql刪除重復(fù)數(shù)據(jù)的詳細(xì)方法
- SQL SERVER 2000安裝教程圖文詳解
- 使用sql server management studio 2008 無法查看數(shù)據(jù)庫,提示 無法為該請求檢索數(shù)據(jù) 錯誤916解決方法
- SQLServer日志清空語句(sql2000,sql2005,sql2008)
- Sql Server 2008完全卸載方法(其他版本類似)
- sql server 2008 不允許保存更改,您所做的更改要求刪除并重新創(chuàng)建以下表
- SQL Server 2008 清空刪除日志文件(瞬間日志變幾M)
- Win7系統(tǒng)安裝MySQL5.5.21圖解教程
- 將DataTable作為存儲過程參數(shù)的用法實例詳解
Mssql數(shù)據(jù)庫教程Rss訂閱編程教程搜索
Mssql數(shù)據(jù)庫教程推薦
猜你也喜歡看這些
- CentOS6.4系統(tǒng)中Mysql數(shù)據(jù)庫卸載、安裝與配置
- 基于unique與primary約束的區(qū)別分析
- 服務(wù)器不支持 MySql 數(shù)據(jù)庫的解決方法
- MySQL筆記之基本查詢的應(yīng)用詳解
- SQL提示Login failed for user#039;sa#039;錯誤的解決方案
- 用MyEclipse配置DataBase Explorer(圖示)
- MySQL 關(guān)于表復(fù)制 insert into 語法的詳細(xì)介紹
- 解析MYSQL 數(shù)據(jù)庫導(dǎo)入SQL 文件出現(xiàn)亂碼的問題
- mysql 按照時間段來獲取數(shù)據(jù)的方法
- MySQL:數(shù)據(jù)庫知識點
- 相關(guān)鏈接:
- 教程說明:
Mssql數(shù)據(jù)庫教程-關(guān)于重新組織和重新生成索引sp_RefreshIndex的介紹。