---------------------------------------------------------------------------
SET ECHO OFF;
SET FEEDBACK OFF;
SET VERIFY OFF;
SET PAGESIZE 0;
SET TERMOUT ON;
SET HEADING OFF;
ACCEPT username CHAR PROMPT 'Enter the index username: ';
spool /oracle/rebuild_&username.sql;
SELECT
'REM +-----------------------------------------------+' || chr(10) ||
'REM | INDEX NAME : ' || owner || '.' || segment_name
|| lpad('|', 33 - (length(owner) + length(segment_name)) )
|| chr(10) ||
'REM | BYTES : ' || bytes
|| lpad ('|', 34-(length(bytes)) ) || chr(10) ||
'REM | EXTENTS : ' || extents
|| lpad ('|', 34-(length(extents)) ) || chr(10) ||
'REM +-----------------------------------------------+' || chr(10) ||
'ALTER INDEX ' || owner || '.' || segment_name || chr(10) ||
'REBUILD ' || chr(10) ||
'TABLESPACE ' || tablespace_name || chr(10) ||
'STORAGE ( ' || chr(10) ||
' INITIAL ' || initial_extent || chr(10) ||
' NEXT ' || next_extent || chr(10) ||
' MINEXTENTS ' || min_extents || chr(10) ||
' MAXEXTENTS ' || max_extents || chr(10) ||
' PCTINCREASE ' || pct_increase || chr(10) ||
');' || chr(10) || chr(10)
FROM dba_segments
WHERE segment_type = 'INDEX'
AND owner='&username'
ORDER BY owner, bytes DESC;
spool off;
-----------------------------------------------------------------------------
如果你用的是 WINDOWS 系统 , 想改变输出文件的存放目录 , 修改 spool 后面的路径成 :
spool c:/oracle/rebuild_&username.sql;
如果你只想对大于 max_bytes 的索引重建索引 , 可以修改上面的 SQL 语句 :
在 AND owner='&username' 后面加个限制条件 AND bytes> &max_bytes
如果你想修改索引的存储参数 , 在重建索引 rebuild_&username.sql 里改也可以 .
比如把 pctincrease 不等于零的值改成是零 .
如果你想把 index 从一个 TABLESPACE 转移到另外一个 TABLESPACE ,也可以修改其中的 tablespace_name