Oracle用户名重建索引方法探究

数据库 Oracle
根据Oracle用户名重建索引可以提高程序查询性能,增加索引表空间的空闲度,有效利用重建索引还可以释放已删除记录索引的空间占用。

每个DBA都应该知道数据索引的重要性。特别是管理Oracle的DBA,很多应用项目会涉及到大量的修改删除操作, 数据索引是需要周期性的重建的。这里介绍的根据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

如果你想修改索引的存储参数,在重建索引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错误, 表示索引在的表上有锁信息, 不能根据Oracle用户名来重建索引。

那就忽略这个错误, 看下次是否成功。对那些特别忙的表要区别对待, 不能用这里介绍的方法,还要把它们的索引从rebuild_&username.sql里删去。

 

【编辑推荐】

  1. 全面讲解Oracle查询用户表空间
  2. 浅析Oracle用户权限表的管理方法
  3. Oracle数据库备份与恢复特性浅谈
  4. 使用Oracle外部表的五个限制
  5. 浅析Oracle中的表空间查询方法
责任编辑:佚名 来源: 百度空间
相关推荐

2009-08-05 13:32:07

Oracle按用户名重

2011-05-26 10:11:24

Oracle数据库索引

2010-09-27 14:48:12

SQL用户名

2010-10-29 11:51:30

oracle用户名

2009-10-21 17:13:32

Oracle用户名

2009-10-26 16:08:40

Oracle默认用户名

2010-11-16 09:18:39

oracle重建索引

2009-08-18 13:52:57

Ubuntu用户名密码

2019-08-26 19:24:55

Podman容器Linux

2013-01-04 17:51:28

Android开发SharedPrefe解析用户名

2022-06-24 08:48:47

用户名密码登录

2010-11-16 09:49:22

Oracle重建索引

2010-09-27 15:43:47

SQL语句

2010-02-25 16:09:15

Fedora驱动程序

2010-10-29 13:50:21

oracle日志文件

2020-03-27 08:51:54

oracle数据库用户名

2011-07-22 15:01:28

MongoDB权限管理

2020-07-11 09:26:16

数据泄露黑客网络攻击

2009-10-22 16:25:53

Oracle UNDO

2014-09-11 09:25:19

点赞
收藏

51CTO技术栈公众号