科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道Oracle数据库中索引的维护

Oracle数据库中索引的维护

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文只讨论Oracle中最常见的索引,即是B-tree索引。本文中涉及的数据库版本是Oracle8i。 一. 查看系统表中的用户索引 在Oracle中,SYSTEM表是安装数据库时自动建立的,它包含数据库的全部数据字典。

作者:中国IT实验室 来源:中国IT实验室 2007年10月7日

关键字: ORACLE

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共2页)

 

四. 确定索引的实际碎片

随着数据库的使用,不可避免地对基本表进行插入,更新和删除,这样导致叶子行在索引中被删除,使该索引产生碎片。插入删除越频繁的表,索引碎片的程度也越高。碎片的产生使访问和使用该索引的I/O成本增加。碎片较高的索引必须重建以保持最佳性能。

(1)利用验证索引命令对索引进行验证。

这将有价值的索引信息填入index_stats表。

validate index 用户名.索引名

(2)查询index_stats表以确定索引中删除的、未填满的叶子行的百分比。

select name, del_lf_rows, lf_rows,
       round((del_lf_rows/(lf_rows+0.0000000001))*100) "Frag Percent"
from index_stats

(3)如果索引的叶子行的碎片超过10%,考虑对索引进行重建。

alter index 用户名.索引名 rebuild
  tablespace 表空间名
  storage(initial 初始值 next 扩展值)
  nologging

(4)如果出于空间或其他考虑,不能重建索引,可以整理索引。

 alter index用户名.索引名 coalesce

(5)清除分析信息

analyze index 用户名.索引名 delete statistics

五. 重建索引

(1)检查需要重建的索引

根据以下几方面进行检查,确定需要重建的索引。

第一,查看SYSTEM表空间中的用户索引

为了避免数据字典的碎片出现,要尽量避免在SYSTEM表空间出现用户的表和索引。

select index_name from dba_indexes 
where tablespace_name="SYSTEM" and owner not in ("SYS","SYSTEM")

第二,确保用户的表和索引不在同一表空间内

表和索引对象的第一个规则是把表和索引分离。把表和相应的索引建立在不同的表空间中,最好在不同的磁盘上。这样可以避免在数据管理和查询时出现的许多I/O冲突。

set linesize 120
col "OWNER" format a20
col "INDEX" format a30
col "TABLE" format a30
col "TABLESPACE" format a30
select i.owner "OWNER", i.index_name "INDEX", t.table_name "TABLE", 
       i.tablespace_name "TABLESPACE"
from dba_indexes i, dba_tables t
where i.owner=t.owner
  and i.table_name=t.table_name
  and i.tablespace_name=t.tablespace_name
  and i.owner not in ("SYS","SYSTEM")
  / 

第三,查看数据表空间里有哪些索引

用户的默认表空间应该不是SYSTEM表空间,而是数据表空间。在建立索引时,如果不指定相应的索引表空间名,那么,该索引就会建立在数据表空间中。这是程序员经常忽略的一个问题。应该在建索引时,明确的指明相应的索引表空间。

col segment_name format a30
select owner, segment_name, sum(bytes) 
from dba_segments
where tablespace_name="数据表空间名"
and segment_type="INDEX"
group by owner,segment_name
/

第四,查看哪个索引被扩展了超过10次

随着表记录的增加,相应的索引也要增加。如果一个索引的next extent值设置不合理(太小),索引段的扩展变得很频繁。索引的extent太多,检索时的速度和效率就会降低。

set linesize 100
col owner format a10
col segment_name format a30
col tablespace_name format a30
select count(*), owner, segment_name, tablespace_name 
from dba_extents
where segment_type="INDEX" 
  and owner not in ("SYS","SYSTEM")
group by owner,segment_name,tablespace_name
having count(*) >10
order by count(*) desc
/

(2)找出需要重建的索引后,需要确定索引的大小,以设置合理的索引存储参数。

set linesize 120
col "INDEX" format a30
col "TABLESPACE" format a20
select owner "OWNER", segment_name "INDEX", tablespace_name "TABLESPACE", 
       bytes "BYTES/COUNT", sum(bytes) "TOTAL BYTES", 
       round(sum(bytes)/(1024*1024),0) "TOTAL M",
       count(bytes) "TOTAL COUNT"
  from dba_extents
  where segment_type="INDEX" 
    and segment_name in ("索引名1","索引名2", ......)
  group by owner,segment_name,segment_type,tablespace_name,bytes
  order by owner,segment_name
  /

(3)确定索引表空间还有足够的剩余空间

确定要把索引重建到哪个索引表空间中。要保证相应的索引表空间有足够的剩余空间。

select round(bytes/(1024*1024),2) free(M)
  from sm$ts_free
  where tablespace_name="表空间名"
/ 

(4)重建索引

重建索引时要注意以下几点:

a.如果不指定tablespace名,索引将建在用户的默认表空间。
b.如果不指定nologging,将会写日志,导致速度变慢。由于索引的重建没有恢复的必要,所以,可以不写日志。
c.如果出现资源忙,表明有进程正在使用该索引,等待一会再提交。

alter index 索引名 rebuild
  tablespace 索引表空间名
  storage(initial 初始值 next 扩展值)
  nologging
  / 

(5)检查索引

对重建好的索引进行检查。

select * from dba_extents where segment_name="索引名"

(6)根据索引进行查询,检查索引是否有效

使用相应的where条件进行查询,确保使用该索引。看看使用索引后的效果如何。

select * from dba_ind_columns where index_name like "表名%"

然后,根据相应的索引项进行查询。

select * from "表名%" where ......

(6)找出有碎片的表空间,并收集其碎片。

重建索引后,原有的索引被删除,这样会造成表空间的碎片。

select "alter tablespace "||tablespace_name||" coalesce;"
  from dba_free_space_coalesced
  where percent_blocks_coalesced!=100
/ 

整理表空间的碎片。

alter tablespace 表空间名 coalesce

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件