zoukankan      html  css  js  c++  java
  • SQL索引碎片整理脚本

    原文发布时间为:2011-02-23 —— 来源于本人的百度文章 [由搬家工具导入]

    reindex是比较好的选择,速度快,但是他不能在线操作
    INDEXDEFRAG 比较慢,但是可以在线操作
    rebuild建议在碎片较少时采用。

    附上微软的重建索引脚本,从里面也可以看出微软根据碎片大小推荐的方式,不过这个要随每个不同的数据库而定。
    -- ensure a USE <databasename> statement has been executed first.
    SET NOCOUNT ON;
    DECLARE @objectid int;
    DECLARE @indexid int;
    DECLARE @partitioncount bigint;
    DECLARE @schemaname sysname;
    DECLARE @objectname sysname;
    DECLARE @indexname sysname;
    DECLARE @partitionnum bigint;
    DECLARE @partitions bigint;
    DECLARE @frag float;
    DECLARE @command varchar(8000);
    -- ensure the temporary table does not exist
    IF EXISTS (SELECT name FROM sys.objects WHERE name = 'work_to_do')
        DROP TABLE work_to_do;
    -- conditionally select from the function, converting object and index IDs to names.
    SELECT
        object_id AS objectid,
        index_id AS indexid,
        partition_number AS partitionnum,
        avg_fragmentation_in_percent AS frag
    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
            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>5 AND @frag <= 30.0
        BEGIN;
        SELECT @command = 'ALTER INDEX ' + @indexname + ' ON ' + @schemaname + '.' + @objectname + ' REORGANIZE';
        IF @partitioncount > 1
            SELECT @command = @command + ' PARTITION=' + CONVERT (CHAR, @partitionnum);
        EXEC (@command);
        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);
        EXEC (@command);
        END;
    PRINT 'Executed ' + @command;

    FETCH NEXT FROM partitions INTO @objectid, @indexid, @partitionnum, @frag;
    END;
    -- Close and deallocate the cursor.
    CLOSE partitions;
    DEALLOCATE partitions;

    -- drop the temporary table
    IF EXISTS (SELECT name FROM sys.objects WHERE name = 'work_to_do')
        DROP TABLE work_to_do;
    GO


    如果提示     '(' 附近有语法错误,则将上面的 DB_ID() 这个改成当前库的ID (当前库的ID,你可以用查询分析器 SELECTDB_ID() 查询出来)


    BOL的推荐:



    > 5% 且 < = 30%

    ALTER INDEX REORGANIZE

    > 30%

    ALTER INDEX REBUILD WITH (ONLINE = ON)*

  • 相关阅读:
    js输出
    mysql主从同步(4)-Slave延迟状态监控
    mysql主从同步(3)-percona-toolkit工具(数据一致性监测、延迟监控)使用梳理
    【故障】MySQL主从同步故障-Slave_SQL_Running: No
    什么是bgp线路
    F5负载均衡虚拟服务器配置FTP端口访问不了
    Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)
    东风本田/XR-V/2017款
    mysqldump导出数据库导入数据库
    .htaccess设置301跳转及常用技巧整理
  • 原文地址:https://www.cnblogs.com/handboy/p/7163997.html
Copyright © 2011-2022 走看看