通过重建索引可以释放已删除记录索引占用数据块空间来转移数据, 重命名思路方法可以重新组织表里数据
按Oracle用户名生成重建索引SQL脚本
---------------------------------------------
SET ECHO OFF;
SET FEEDBACK OFF;
SET VERIFY OFF;
SET PAGESIZE 0;
SET TERMOUT _disibledevent=> AND owner='&username'
ORDER BY owner, s DESC;
spool off;
---------------------------------------------
假如你用是WINDOWS系统, 想改变输出文件存放目录, 修改spool后面路径成:
spool c:\oracle\rebuild_&username.sql;
如果你只想对大于max_s索引重建索引, 可以修改上面SQL语句:
在AND owner='&username' 后面加个限制条件 AND s> &max_s
如果你想修改索引存储参数, 在重建索引rebuild_&username.sql里改也可以
比如把pctincrease不等于零值改成是零.
生成rebuild_&username.sql文件我们需要来分析下, 它们是否到了需要重建程度:
分析索引观察下是否碎片特别严重
SQL>ANALYZE INDEX &index_name VALIDATE STRUCTURE;
col name heading 'Index Name' format a30
col del_lf_rows heading 'Deleted|Leaf Rows' format 99999999
col lf_rows_used heading 'Used|Leaf Rows' format 99999999
col ratio heading '% Deleted|Leaf Rows' format 999.99999
SELECT name,
del_lf_rows,
lf_rows - del_lf_rows lf_rows_used,
to_char(del_lf_rows / (lf_rows)*100,'999.99999') ratio
FROM index_stats where name = upper('&index_name');
当删除比率大于15 - 20% 时肯定是需要索引重建
经过删改后rebuild_&username.sql文件我们可以放到Oracle定时作业里:
比如个月或者两个月在非繁忙时间运行
如果遇到ORA-00054, 表示索引在表上有锁信息, 不能重建索引
那就忽略这个, 观察下次是否成功
对于那些特别忙表要不能用这里上面介绍思路方法, 我们需要将它们索引从rebuild_&username.sql里删去
最新评论