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

没有索引,喜欢同样的标签库没有书籍,找书,他们想预订比登天还难。中,尤其是在批量的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 重建索引脚本的更多相关文章

  1. oracle 重建索引以及导出所有的索引脚本(可以解决还原数据库文件时先还原数据,在重新用脚本创建索引)

    导出数据库备份文件 1. 备份服务器数据,采用并行方式,加快备份速度(文件日期根据具体操作日期修改) expdp jhpt/XXXX directory=databackup dumpfile=dpf ...

  2. [转]Oracle 重建索引的必要性

    http://blog.csdn.net/leshami/article/details/23763963 索引重建是一个争论不休被不断热烈讨论的议题.当然Oracle官方也有自己的观点,我们很多DB ...

  3. ORACLE关于索引是否需要定期重建争论的整理

    ORACLE数据库中的索引到底要不要定期重建呢? 如果不需要定期重建,那么理由是什么? 如果需要定期重建,那么理由又是什么?另外,如果需要定期重建,那么满足那些条件的索引才需要重建呢?关于这个问题,网 ...

  4. Oracle重建表索引及手工收集统计信息

    Oracle重建所有表的索引的sql: SELECT 'alter index ' || INDEX_NAME || ' rebuild online nologging;' FROM USER_IN ...

  5. Oracle分区索引

    索引与表类似,也可以分区: 分区索引分为两类: Locally partitioned index(局部分区索引) Globally partitioned index(全局分区索引) 下面就来详细解 ...

  6. Oracle对索引列同时使用多个聚合函数的性能问题

    Oracle某一数据表tkk715(数据量在一千万左右),对一个索引字段做获取最大值与最小值的聚合函数操作,响应时间较长(超过3秒): 将SQL改写为分别取最大.最小的聚合值,IO和响应时间显著下降到 ...

  7. SQL SERVER 生成ORACLE建表脚本

    /****** Object: StoredProcedure [dbo].[GET_TableScript_ORACLE] Script Date: 06/15/2012 13:07:16 **** ...

  8. SQL Server通过整理索引碎片和重建索引提高速度

    本文章转载:http://database.51cto.com/art/201108/282408.htm SQL Server数据库中,当索引碎片太多时,就会拖慢数据库查询的速度.这时我们可以通过整 ...

  9. 11G在线重建索引

    SQL> select count(*) from test_idx; COUNT(*) ---------- 19087751 SQL> select segment_name,segm ...

随机推荐

  1. 关于windows系统影子账户的问题

    在这之前,需要大家了解几个问题,一个是SID,一个是账号的F值. Windows账户的SID 在Windows系统中,系统会为每个用户账户建立一个唯一的安全标识符(Security Identifie ...

  2. zoj3471(状压dp)

    题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4257 题意:不超过10种气体,两两之间相互碰撞可以产生一定的能量,如 ...

  3. Remote Desktop Organizer – 管理组织远程桌面 - 小众软件

    http://www.appinn.com/remote-desktop-organizer/

  4. APS.NET Cookie

    Cookie 提供了一种在 Web 应用程序中存储用户特定信息(如历史记录或用户首选项)的方法. Cookie 是一小段文本.伴随着请求和响应在 Web server和client之间来回传输.Coo ...

  5. IIS总提示输入用户名和密码

    解决办法: 第一步,在"运行"中输入"gpedit.msc"调出组策略设置. 找到计算机配置-管理模块-windows 组件-internet控制面板-安全页- ...

  6. Memcached在.net中的应用

    一.MemCached下载 服务端下载:http://memcachedproviders.codeplex.com/ client下载:path=/trunk">http://sou ...

  7. ReactNavtive框架教程(2)

    , alignItems: 'center' } }); 标准的 CSS 属性.尽管用CSS比在IB设置UI样式的可视化要差.但总比在viewDidLoad()方法中用代码写要好一些. 然后增加下面代 ...

  8. Django - Django框架 简单介绍

    Django框架 简单介绍 本文地址: http://blog.csdn.net/caroline_wendy/article/details/29172271 1. 介绍 Django是一个开放源码 ...

  9. Android 自己的自动化测试(2)依据ID查找对象(java)

    前一篇文章是写 Android 自己的自动化测试(1)如何安装和卸载应用程序(java) ,以下再探索一下假设在普通java应用程序中,依据ID来查找对象 1.类库依赖: The library de ...

  10. C语言程序代写(qq:928900200)

    1cs3157 – Advanced ProgrammingSummer 2014, Project 1, 150 pointsJune 17, 2014Follow these step-by-st ...