zoukankan      html  css  js  c++  java
  • SQL Server 2005:索引碎片整理脚本(网摘)

     

    SQL Server 2005:索引碎片整理脚本。

    首先说明:该 SQL 索引碎片整理脚本,是从 SQL Server 2005 联机帮助上摘录下来,并且稍加整理而成的。

    该 SQL 索引碎片整理脚本,首先从 SQL Server 2005 系统管理视图 sys.dm_db_index_physical_stats 中,找出索引碎片程度大于 10% 的索引,然后根据索引碎片程度,分别来采取不同的方法来整理索引碎片。小于 30% 的使用 alter index reorganize;大于等于 30% 的使用 alter index rebuild。其中 reorganize 相当于 dbcc indexdefrag();rebuild 相当于 dbcc dbreindex()。

    SQL 碎片整理后,索引数据页在数据库文件中排列的更紧凑,可以大幅提高一些 SQL 查询的效率。DBA 可以每周进行一次碎片整理。另外要注意的是,不要在收缩数据库(dbcc shrinkfile, dbcc shrinkdatabase)前整理索引碎片。

    Using sys.dm_db_index_physical_stats in a script to rebuild or reorganize indexes
    The following example automatically reorganizes or rebuilds all partitions in a database that have an average fragmentation over 10 percent. Executing this query requires the [view database state] permission.

    --------------------------------------------------------------------------------
    -- ensure a USE  statement has been executed first.
    --------------------------------------------------------------------------------

    set nocount on

    declare @objectid         int
           ,@indexid          int
           ,@partitioncount   bigint
           ,@schemaname       sysname
           ,@objectname       sysname
           ,@indexname        sysname
           ,@partitionnum     bigint
           ,@partitions       bigint
           ,@frag             float
           ,@command          varchar(1000)

    select objectid     = object_id
          ,indexid      = index_id
          ,partitionnum = partition_number
          ,frag         = avg_fragmentation_in_percent
      into #work_to_do
      from sys.dm_db_index_physical_stats(db_id(), null, null , null, 'LIMITED')
    where avg_fragmentation_in_percent > 10.0
       and index_id > 0

    -- declare the cursor for the list of partitions to be processed.
    declare partitions cursor for
       select * from #work_to_do

    -- Open the cursor.
    open partitions

    -- Loop through the partitions.
    fetch next from partitions into @objectid, @indexid, @partitionnum, @frag

    while @@fetch_status = 0 begin
       select @objectname = o.name, @schemaname = s.name
         from sys.objects as o
                inner join sys.schemas as s
          on s.schema_id = o.schema_id
       where o.object_id = @objectid

       select @indexname = name
         from sys.indexes
        where object_id = @objectid
          and index_id = @indexid

       select @partitioncount = count (*)
        from sys.partitions
       where object_id = @objectid
         and index_id = @indexid

       -- 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding
       if @frag < 30.0 begin
           select @command = 'alter index ' + @indexname + ' on '
                           + @schemaname + '.' + @objectname + ' reorganize'

           if @partitioncount > 1
               select @command = @command + ' partition=' + convert(char, @partitionnum)
       end

       if @frag >= 30.0 begin
           select @command = 'alter index ' + @indexname +' on '
                           + @schemaname + '.' + @objectname + ' rebuild'

           if @partitioncount > 1
               select @command = @command + ' partition=' + convert(char, @partitionnum)
       end

       -- exec (@command)
       print 'Executed: ' + @command

       fetch next from partitions into @objectid, @indexid, @partitionnum, @frag
    end


    -- free resource
    close partitions
    deallocate partitions
    drop table #work_to_do
    《SQL Server 2005:索引碎片整理脚本》代码摘自 SQL Server 2005 Books Online。

    爱情是两个人的”饰“!
    欢迎光临我的时尚饰品店:http://shop36465575.taobao.com

    也许有意想不到的收获!!!
  • 相关阅读:
    3星|《失败课》:投资人写给创业者的经验谈,有点标题党
    3星|《给你一门人工智能入门生意经》:机器所知胜于其所能言传
    3星|《财经》2018年第5期:西伯利亚冻土层的猛犸象牙是合法的,一根能卖到数万美元
    3星|《增长黑客》:增长黑客是一个牵强的概念
    2星|《只管去做》:做年度计划的入门级介绍,信息浓度太低
    创业者融资过程中需要了解的大坑小坑:《风投的技术》,4星
    4星|吴军《见识》:李开复上级的工作经验、投资经验与人生忠告
    4星|《基因转》:从孟德尔、达尔文到人类胚胎转基因
    3星|《人机平台》:数字化时代的三大类新的再平衡:人脑与机器、产品与平台,以及核心与大众
    3星|《知识的边界》:知识存在于网络中,分歧永远存在
  • 原文地址:https://www.cnblogs.com/lfzwenzhu/p/1516022.html
Copyright © 2011-2022 走看看