Oracle 重建索引脚本

Oracle 重建索引脚本

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

      该指数是一个有力的武器,以提高数据库的查询性能。

没有索引,喜欢同样的标签库没有书籍,找书,他们想预订比登天还难。中,尤其是在批量的DML的情形下会产生对应的碎片。以及B树高度会发生对应变化。因此能够对这些变化较大的索引进行重构以提高性能。N久曾经Oracle建议我们定期重建那些高度为4。已删除的索引条目至少占有现有索引条目总数的20%的这些表上的索引。但Oracle如今强烈建议不要定期重建索引。

详细能够參考文章:Oracle 重建索引的必要性

虽然如此重建索引还是有必要的。仅仅是不建议定期。本文给出了重建索引的脚本供大家參考。

 
1、重建索引shell脚本

robin@SZDB:~/dba_scripts/custom/bin> more rebuild_unbalanced_indices.sh 
# +-------------------------------------------------------+
# +    Rebulid unblanced indices                          |
# +    Author : Leshami                                   | 
# +    Parameter : No                                     |
# +    Blog : http://blog.csdn.net/leshami                | 
# +-------------------------------------------------------+

#!/bin/bash 
# --------------------
# Define variable
# --------------------

if [ -f ~/.bash_profile ]; then
. ~/.bash_profile
fi

DT=`date +%Y%m%d`;             export DT
RETENTION=1
LOG_DIR=/tmp
LOG=${LOG_DIR}/rebuild_unbalanced_indices_${DT}.log
DBA=Leshami@12306.cn

# ------------------------------------
# Loop all instance in current server
# -------------------------------------
echo "Current date and time is : `/bin/date`">>${LOG}

for db in `ps -ef | grep pmon | grep -v grep |grep -v asm |awk '{print $8}'|cut -c 10-`
do
    echo "$db"
    export ORACLE_SID=$db
    echo "Current DB is $db" >>${LOG}
    echo "===============================================">>${LOG}
    $ORACLE_HOME/bin/sqlplus -S /nolog @/users/robin/dba_scripts/custom/sql/rebuild_unbalanced_indices.sql>>${LOG}
done;

echo "End of rebuilding index for all instance at : `/bin/date`">>${LOG}
# -------------------------------------
# Check log file 
# -------------------------------------
status=`grep "ORA-" ${LOG}`
if [ -z $status ];then
    mail -s "Succeeded rebuilding indices on `hostname`  !!!" ${DBA} <${LOG}
else
    mail -s "Failed rebuilding indices on `hostname`  !!!" ${DBA} <${LOG}
fi

# ------------------------------------------------
# Removing files older than $RETENTION parameter 
# ------------------------------------------------

find ${LOG_DIR} -name "rebuild_unb*" -mtime +$RETENTION -exec rm {} \;

exit

2、重建索引调用的SQL脚本

robin@SZDB:~/dba_scripts/custom/sql> more rebuild_unbalanced_indices.sql 
conn / as sysdba
set serveroutput on;
DECLARE
   resource_busy               EXCEPTION;
   PRAGMA EXCEPTION_INIT (resource_busy, -54);
   c_max_trial        CONSTANT PLS_INTEGER := 10;
   c_trial_interval   CONSTANT PLS_INTEGER := 1;
   pmaxheight         CONSTANT INTEGER := 3;
   pmaxleafsdeleted   CONSTANT INTEGER := 20;

   CURSOR csrindexstats
   IS
      SELECT NAME,
             height,
             lf_rows AS leafrows,
             del_lf_rows AS leafrowsdeleted
        FROM index_stats;

   vindexstats                 csrindexstats%ROWTYPE;

   CURSOR csrglobalindexes
   IS
      SELECT owner,index_name, tablespace_name
        FROM dba_indexes
       WHERE partitioned = 'NO'
        AND owner IN ('GX_ADMIN');

   CURSOR csrlocalindexes
   IS
      SELECT index_owner,index_name, partition_name, tablespace_name
        FROM dba_ind_partitions
       WHERE status = 'USABLE'
        AND index_owner IN ('GX_ADMIN');

   trial                       PLS_INTEGER;
   vcount                      INTEGER := 0;
BEGIN
   trial := 0;

   /* Global indexes */
   FOR vindexrec IN csrglobalindexes
   LOOP
      EXECUTE IMMEDIATE
         'analyze index ' || vindexrec.owner ||'.'|| vindexrec.index_name || ' validate structure';

      OPEN csrindexstats;

      FETCH csrindexstats INTO vindexstats;

      IF csrindexstats%FOUND
      THEN
         IF    (vindexstats.height > pmaxheight)
            OR (    vindexstats.leafrows > 0
                AND vindexstats.leafrowsdeleted > 0
                AND (vindexstats.leafrowsdeleted * 100 / vindexstats.leafrows) >
                       pmaxleafsdeleted)
         THEN
            vcount := vcount + 1;
            DBMS_OUTPUT.PUT_LINE (
               'Rebuilding index ' || vindexrec.owner ||'.'|| vindexrec.index_name || '...');

           <<alter_index>>
            BEGIN
               EXECUTE IMMEDIATE
                     'alter index '
                  || vindexrec.owner ||'.'
                  || vindexrec.index_name
                  || ' rebuild'
                  || ' parallel nologging compute statistics'
                  || ' tablespace '
                  || vindexrec.tablespace_name;
            EXCEPTION
               WHEN resource_busy OR TIMEOUT_ON_RESOURCE
               THEN
                  DBMS_OUTPUT.PUT_LINE (
                     'alter index - busy and wait for 1 sec');
                  DBMS_LOCK.sleep (c_trial_interval);

                  IF trial <= c_max_trial
                  THEN
                     GOTO alter_index;
                  ELSE
                     DBMS_OUTPUT.PUT_LINE (
                           'alter index busy and waited - quit after '
                        || TO_CHAR (c_max_trial)
                        || ' trials');
                     RAISE;
                  END IF;
               WHEN OTHERS
               THEN
                  DBMS_OUTPUT.PUT_LINE ('alter index err ' || SQLERRM);
                  RAISE;
            END;
         END IF;
      END IF;

      CLOSE csrindexstats;
   END LOOP;

   DBMS_OUTPUT.PUT_LINE ('Global indices rebuilt: ' || TO_CHAR (vcount));
   vcount := 0;
   trial := 0;

   /* Local indexes */
   FOR vindexrec IN csrlocalindexes
   LOOP
      EXECUTE IMMEDIATE
            'analyze index '
         || vindexrec.index_owner||'.'
         || vindexrec.index_name
         || ' partition ('
         || vindexrec.partition_name
         || ') validate structure';

      OPEN csrindexstats;

      FETCH csrindexstats INTO vindexstats;

      IF csrindexstats%FOUND
      THEN
         IF    (vindexstats.height > pmaxheight)
            OR (    vindexstats.leafrows > 0
                AND vindexstats.leafrowsdeleted > 0
                AND (vindexstats.leafrowsdeleted * 100 / vindexstats.leafrows) >
                       pmaxleafsdeleted)
         THEN
            vcount := vcount + 1;
            DBMS_OUTPUT.PUT_LINE (
               'Rebuilding index ' || vindexrec.index_owner||'.'|| vindexrec.index_name || '...');

           <<alter_partitioned_index>>
            BEGIN
               EXECUTE IMMEDIATE
                     'alter index '
                  || vindexrec.index_owner||'.'
                  || vindexrec.index_name
                  || ' rebuild'
                  || ' partition '
                  || vindexrec.partition_name
                  || ' parallel nologging compute statistics'
                  || ' tablespace '
                  || vindexrec.tablespace_name;
            EXCEPTION
               WHEN resource_busy OR TIMEOUT_ON_RESOURCE
               THEN
                  DBMS_OUTPUT.PUT_LINE (
                     'alter partitioned index - busy and wait for 1 sec');
                  DBMS_LOCK.sleep (c_trial_interval);

                  IF trial <= c_max_trial
                  THEN
                     GOTO alter_partitioned_index;
                  ELSE
                     DBMS_OUTPUT.PUT_LINE (
                           'alter partitioned index busy and waited - quit after '
                        || TO_CHAR (c_max_trial)
                        || ' trials');
                     RAISE;
                  END IF;
               WHEN OTHERS
               THEN
                  DBMS_OUTPUT.PUT_LINE (
                     'alter partitioned index err ' || SQLERRM);
                  RAISE;
            END;
         END IF;
      END IF;

      CLOSE csrindexstats;
   END LOOP;

   DBMS_OUTPUT.PUT_LINE ('Local indices rebuilt: ' || TO_CHAR (vcount));
END;
/
exit;

3、输入日志样本
Current date and time is : Sun Apr 20 02:00:02 HKT 2014
Current DB is SYBO2 ===============================================
Rebuilding index GX_ADMIN.SYN_OUT_DATA_TBL_PK…
Rebuilding index GX_ADMIN.IDX_TDBK_SPLNK_PARENT_REF…
Rebuilding index GX_ADMIN.IDX_TDBK_SPLNK_CHILD_REF…
Rebuilding index GX_ADMIN.PK_TRADE_BROKER_TBL…
Rebuilding index GX_ADMIN.IDX_TDBK_INPUT_DATE…
    …………….

 

4、后记
a、假设同一台server上有多个实例,且每一个实例有同样的schema。此脚本会轮巡全部实例并依据analyze结果来rebuild。 
a、大家应依据须要作对应调整。如脚本的路径信息等。

b、须要改动对应的schema name。

d、可依据系统环境调整对应的并行度。

 

5、相关參考
   
Oracle 聚簇因子(Clustering factor) 
    Oracle 索引监控(monitor index)
    Oracle 索引监控与外键索引 
    收集统计信息导致索引被监控 
    Oracle 监控索引的使用率
    NULL 值与索引(一)
    NULL 值与索引(二)
    函数使得索引列失效

    Oracle 索引质量分析

    Oracle 重建索引的必要性

    Oracle 牛鹏社    

版权声明:本文博主原创文章,博客,未经同意不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116963.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 徐州优译翻译宗旨:凭借优异的翻译质量,做最专业的外贸助手!

    徐州优译翻译宗旨:凭借优异的翻译质量,做最专业的外贸助手!

    2021年7月23日
    95
  • nginx负载均衡原理简介_nginx负载均衡配置详解

    nginx负载均衡原理简介_nginx负载均衡配置详解nginx负载均衡中常见的算法及原理有哪些?Nginx可以基于ngx_http_upstream_module模块提供服务器分组转发、权重分配、状态监测、调度算法等高级功能httpupstream配置参数#自定义一组服务器,配置在http块内upstreamname{server………..}#示例upstreambackend{serverbackend1.example.comweight=5;server127.0.

    2022年9月25日
    6
  • 在html中使用fontIcon 的图标

    在html中使用fontIcon 的图标听说fontIcon显现图形比img要高。 然后它是以矢量图的相似呈现,放大不会失真。 在今天开发中发现,公司有人使用了这项技术。。所以也就花时间学习了一下。。在学过程中看了几篇文章之后完成的。。这里也推荐下:1http://www.chinaui.com/Knowledge/20120907/14021209040007.shtml(里面有demo,和更重要的图

    2025年8月22日
    4
  • BH1750光照传感器超详细攻略(从原理到代码讲解,看完你就懂了)

    BH1750光照传感器超详细攻略(从原理到代码讲解,看完你就懂了)BH1750FVI是一款数字型光强度传感器集成芯片,内部由光敏二极管、运算放大器、ADC采集、晶振等组成。PD二极管通过光生伏特效应将输入光信号转换成电信号,经运算放大电路放大后,由ADC采集电压,然后通过逻辑电路转换成16位二进制数存储在内部的寄存器中(注:进入光窗的光越强,光电流越大,电压就越大,所以通过电压的大小就可以判断光照大小,但是要注意的是电压和光强虽然是一一对应的,但不是成正比的,所以这个芯片内部是做了线性处理的,这也是为什么不直接用光敏二极管而用集成IC的原因)。

    2022年6月2日
    141
  • CPLD和FPGA的区别和联系「建议收藏」

    CPLD和FPGA的区别和联系「建议收藏」CPLD和CPU接口:1、CPLD大部分是做为CPU的扩展,替CPU完成外部引脚资源的扩展、输入输出时序管理、部分软件功能实现,肯少单独用CPLD。2、初学者使用CPLD时,可以在IO连线中串联一个51欧姆的电阻,方便测量,更重要的是能保护CPLD的IO,更深一步是能改善高速信号的振铃、信号反射。提高信号完整性。参考:FPGA和CPLD对比与入门FPGA与CPLD的区别…

    2022年6月4日
    34
  • C# 中 Struct 与 Class 的区别,以及两者的适用场合

    C# 中 Struct 与 Class 的区别,以及两者的适用场合

    2021年7月30日
    59

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号