教您如何实现Oracle重建索引

数据库 Oracle
如果Oracle数据库下某些应用项目有大量的修改删除操作,就需要周期性的重建索引,提高Oracle数据库的查询效率。

Oracle重建索引操作大家经常会用到,下面就为您详细介绍Oracle重建索引方面的知识,供您参考,如果您对此方面感兴趣的话,不妨一看。

如果你管理的Oracle数据库下某些应用项目有大量的修改删除操作, 数据索引是需要周期性的重建的.

它不仅可以提高查询性能, 还能增加索引表空间空闲空间大小. 在ORACLE里大量删除记录后, 表和索引里占用的数据块空间并没有释放. Oracle重建索引可以释放已删除记录索引占用的数据块空间. 转移数据, 重命名的方法可以重新组织表里的数据.

下面是可以按ORACLE用户名生成Oracle重建索引的SQL脚本:

  1. SET ECHO OFF;   
  2.  
  3. SET FEEDBACK OFF;   
  4.  
  5. SET VERIFY OFF;   
  6.  
  7. SET PAGESIZE 0;   
  8.  
  9. SET TERMOUT ON;   
  10.  
  11. SET HEADING OFF;   
  12.  
  13. ACCEPT username CHAR PROMPT 'Enter the index username: ';   
  14.  
  15. spool /oracle/rebuild_&username.sql;   
  16.  
  17. SELECT   
  18.  
  19. 'REM +-----------------------------------------------+' || chr(10) ||   
  20.  
  21. 'REM | INDEX NAME : ' || owner || '.' || segment_name   
  22.  
  23. || lpad('|', 33 - (length(owner) + length(segment_name)) )   
  24.  
  25. || chr(10) ||   
  26.  
  27. 'REM | BYTES : ' || bytes   
  28.  
  29. || lpad ('|', 34-(length(bytes)) ) || chr(10) ||   
  30.  
  31. 'REM | EXTENTS : ' || extents   
  32.  
  33. || lpad ('|', 34-(length(extents)) ) || chr(10) ||   
  34.  
  35. 'REM +-----------------------------------------------+' || chr(10) ||   
  36.  
  37. 'ALTER INDEX ' || owner || '.' || segment_name || chr(10) ||   
  38.  
  39. 'REBUILD ' || chr(10) ||   
  40.  
  41. 'TABLESPACE ' || tablespace_name || chr(10) ||   
  42.  
  43. 'STORAGE ( ' || chr(10) ||   
  44.  
  45. ' INITIAL ' || initial_extent || chr(10) ||   
  46.  
  47. ' NEXT ' || next_extent || chr(10) ||   
  48.  
  49. ' MINEXTENTS ' || min_extents || chr(10) ||   
  50.  
  51. ' MAXEXTENTS ' || max_extents || chr(10) ||   
  52.  
  53. ' PCTINCREASE ' || pct_increase || chr(10) ||   
  54.  
  55. ');' || chr(10) || chr(10)   
  56.  
  57. FROM dba_segments   
  58.  
  59. WHERE segment_type = 'INDEX'   
  60.  
  61. AND owner='&username'   
  62.  
  63. ORDER BY owner, bytes DESC;   
  64.  
  65. spool off;   

如果你用的是WINDOWS系统, 想改变输出文件的存放目录, 修改spool后面的路径成:

spool c:\oracle\rebuild_&username.sql;

如果你只想对大于max_bytes的索引重建索引, 可以修改上面的SQL语句:

在AND owner='&username' 后面加个限制条件 AND bytes> &max_bytes

如果你想修改索引的存储参数, 在Oracle重建索引rebuild_&username.sql里改也可以. 比如把pctincrease不等于零的值改成是零. 生成的rebuild_&username.sql文件我们需要来分析一下, 它们是否到了需要重建的程度:

分析索引,看是否碎片严重

  1. SQL>ANALYZE INDEX &index_name VALIDATE STRUCTURE;   
  2.  
  3. col name heading 'Index Name' format a30   
  4.  
  5. col del_lf_rows heading 'Deleted|Leaf Rows' format 99999999   
  6.  
  7. col lf_rows_used heading 'Used|Leaf Rows' format 99999999   
  8.  
  9. col ratio heading '% Deleted|Leaf Rows' format 999.99999   
  10.  
  11. SELECT name,   
  12.  
  13. del_lf_rows,   
  14.  
  15. lf_rows - del_lf_rows lf_rows_used,   
  16.  
  17. to_char(del_lf_rows / (lf_rows)*100,'999.99999') ratio   
  18.  
  19. FROM index_stats where name = upper('&index_name');   

当删除的比率大于15 - 20% 时,肯定是需要索引重建的. 经过删改后的rebuild_&username.sql文件我们可以放到ORACLE的定时作业里:

比如一个月或者两个月在非繁忙时间运行. 如果遇到ORA-00054错误, 表示索引在的表上有锁信息, 不能重建索引. 那就忽略这个错误, 看下次是否成功. 对那些特别忙的表要区别对待, 不能用这里介绍的方法, 还要把它们的索引从rebuild_&username.sql里删去。
 

 

【编辑推荐】

全面解析Oracle文件系统

当前Oracle系统时间的查询方法

ORACLE系统表和数据字典视图

深度解析Oracle ERP系统模块

详解四大类Oracle索引扫描

   

 

责任编辑:段燃 来源: 互联网
相关推荐

2010-10-26 10:48:16

ORACLE备份

2010-11-18 16:27:37

2010-10-12 14:53:31

mysql索引优化

2010-11-16 09:18:39

oracle重建索引

2010-11-18 16:41:13

oracle死锁

2010-10-13 15:59:21

MySQL索引

2010-10-26 16:54:16

oracle全文索引

2010-10-28 11:29:18

ORACLE用户权限

2010-10-27 16:56:05

Oracle重复记录

2010-10-29 13:30:33

Oracle归档日志

2010-10-29 11:51:30

oracle用户名

2010-10-28 09:21:42

oracle中存图片

2010-11-29 13:28:55

sybase自动备份

2010-09-26 13:56:43

SQL远程查询

2010-09-28 11:07:37

SQL索引

2010-11-25 14:56:46

MySQL全文查询

2010-11-22 16:05:53

MySQL多表插入

2010-10-14 10:28:18

MySQL动态视图

2010-10-08 10:37:00

MYSQL添加新字段

2010-10-19 16:58:34

SQL Server日
点赞
收藏

51CTO技术栈公众号