校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > 關于重新組織和重新生成索引sp_RefreshIndex的介紹

關于重新組織和重新生成索引sp_RefreshIndex的介紹

熱門標簽:北京400電話辦理多少錢 美團地圖標注商戶認證注冊 榕城市地圖標注 浙江穩定外呼系統供應商 電銷外呼系統軟件功能 怎么給高德做地圖標注 承德地圖標注公司名需要花錢嗎 咸陽電腦外呼系統運營商 慶陽地圖標注

開始:

--------------------------------------------------------------------------------

在上周,客戶反映一個系統問題,當處理大量數據的時候,出現網絡超時。后來,我們跟蹤測試,發現是由于索引碎片多而引起的網絡超時。

解決方法,自然是重新組織和重新生成索引。在這里,我寫了一個存儲過程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的介紹》相關的同類信息!
  • 本頁收集關于關于重新組織和重新生成索引sp_RefreshIndex的介紹的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 中江县| 黔南| 油尖旺区| 辉南县| 靖州| 乡宁县| 孟州市| 弋阳县| 疏勒县| 南郑县| 黄平县| 蒲江县| 额尔古纳市| 光泽县| 叙永县| 崇州市| 沾化县| 石嘴山市| 大连市| 武威市| 双辽市| 金沙县| 武汉市| 晋州市| 资溪县| 西峡县| 吕梁市| 固镇县| 南部县| 邯郸市| 股票| 土默特左旗| 平南县| 页游| 东乌珠穆沁旗| 广饶县| 永清县| 娄烦县| 新密市| 稷山县| 和静县|