本文只讨论Oracle中最常见的索引,即是B-tree索引。本文中涉及的数据库版本是Oracle8i。
一. 查看系统表中的用户索引
在Oracle中,SYSTEM表是安装数据库时自动建立的,它包含数据库的全部数据字典,存储过程、包、函数和触发器的定义以及系统回滚段。
一般来说,应该尽量避免在SYSTEM表中存储非SYSTEM用户的对象。因为这样会带来数据库维护和管理的很多问题。一旦SYSTEM表损坏了,只能重新生成数据库。我们可以用下面的语句来检查在SYSTEM表内有没有其他用户的索引存在。
| select count(*) from dba_indexes where tablespace_name = 'SYSTEM' and owner not in ('SYS','SYSTEM') / |
| select segment_name, count(*) from dba_extents where segment_type='INDEX' and wner=UPPER('&owner') group by segment_name / |
| select substr(segment_name,1,20) "SEGMENT NAME", bytes, count(bytes) from dba_extents where segment_name in ( select index_name from dba_indexes where tablespace_name=UPPER('&表空间')) group by segment_name,bytes order by segment_name / |
| select count(distinct 第一列||'%'||第二列)/count(*) from 表名 / |
| analyze table 表名 compute statistics / |
| select distinct_keys from user_indexes where table_name='表名' and index_name='索引名' / |
| select num_rows from user_tables where table_name='表名' / |
| select i.distinct_keys/t.num_rows from user_indexes i, user_tables t where i.table_name='表名' and i.index_name='索引名' and i.table_name=t.table_name / |
| select column_name, num_distinct from user_tab_columns where table_name='表名' / |
| validate index 用户名.索引名 / |
| select name, del_lf_rows, lf_rows, round((del_lf_rows/(lf_rows+0.0000000001))*100) "Frag Percent" from index_stats / |
| alter index 用户名.索引名 rebuild tablespace 表空间名 storage(initial 初始值 next 扩展值) nologging / |
| alter index用户名.索引名 coalesce / |
| analyze index 用户名.索引名 delete statistics / |
| select index_name from dba_indexes where tablespace_name='SYSTEM' and owner not in ('SYS','SYSTEM') / |
| 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') / |
| 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 / |
| 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 / |
| 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 / |
| select round(bytes/(1024*1024),2) free(M) from sm$ts_free where tablespace_name='表空间名' / |
| alter index 索引名 rebuild tablespace 索引表空间名 storage(initial 初始值 next 扩展值) nologging / |
| select * from dba_extents where segment_name='索引名' / |
| select * from dba_ind_columns where index_name like '表名%' / |
| select * from '表名%' where ...... / |
| select 'alter tablespace '||tablespace_name||' coalesce;' from dba_free_space_coalesced where percent_blocks_coalesced!=100 / |
| alter tablespace 表空间名 coalesce / |
转:http://redstar1212000.blog.163.com/blog/static/28523302009124937175/
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15149581/viewspace-676129/ ,如需转载,请注明出处,否则将追究法律责任。