----------------------索引----------------------------------
c:/>create table e3 as select * from emp where 1=0;
表已创建。
c:/>alter table e3 add constraint pk_e3 primary key(empno);
表已更改。
--查看索引在表上及所在列信息
c:/>select index_name,index_type,table_name,uniqueness from user_indexes;
c:/>select index_name,table_name,column_name from user_ind_columns order by 2,3;
c:/>select height,blocks,br_blks,lf_blks,lf_rows,del_lf_rows from index_stats;
HEIGHT BLOCKS BR_BLKS LF_BLKS LF_ROWS DEL_LF_ROWS
---------- ---------- ---------- -------- ---------- -----------
(高度) 1 (索引总块数)16 (树干块数) 0 (叶子块数)1 (叶子内行数)0 (叶子中被删行数)0
c:/>select * from e3;
未选定行
c:/>insert into e3 select * from emp;
已创建12行。
c:/>select height,blocks,br_blks,lf_blks,lf_rows,del_lf_rows from index_stats;
HEIGHT BLOCKS BR_BLKS LF_BLKS LF_ROWS DEL_LF_ROWS
---------- ---------- ---------- ---------- ---------- -----------
1 16 0 1 0 0
c:/>analyze index pk_e3 validate structure;
索引已分析
c:/>select height,blocks,br_blks,lf_blks,lf_rows,del_lf_rows from index_stats;
HEIGHT BLOCKS BR_BLKS LF_BLKS LF_ROWS DEL_LF_ROWS
---------- ---------- ---------- ---------- ---------- -----------
1 16 0 1 12 0
c:/>update e3 set empno=7777 where empno=7900;
已更新 1 行。
c:/>analyze index pk_e3 validate structure;
索引已分析
c:/>select height,blocks,br_blks,lf_blks,lf_rows,del_lf_rows from index_stats;
HEIGHT BLOCKS BR_BLKS LF_BLKS LF_ROWS DEL_LF_ROWS
---------- ---------- ---------- ---------- ---------- -----------
1 16 0 1 13 1
--表中只有12条记录,但索引叶子中却有13行,其中一行是被删时留下的。
c:/>begin
2 for i in 1..547 loop
3 insert into e3 values(20+i,'test','clerk',7777,sysdate,2000,500,10);
4 end loop;
5 end;
6 /
PL/SQL 过程已成功完成。
c:/>select count(1) from e3;
COUNT(1)
----------
559
c:/>select height,blocks,br_blks,lf_blks,lf_rows,del_lf_rows from index_stats;
HEIGHT BLOCKS BR_BLKS LF_BLKS LF_ROWS DEL_LF_ROWS
---------- ---------- ---------- ---------- ---------- -----------
1 16 0 1 13 1
c:/>analyze index pk_e3 validate structure;
索引已分析
c:/>select height,blocks,br_blks,lf_blks,lf_rows,del_lf_rows from index_stats;
HEIGHT BLOCKS BR_BLKS LF_BLKS LF_ROWS DEL_LF_ROWS
---------- ---------- ---------- ---------- ---------- -----------
2 16 1 3 559 0
c:/>alter index pk_e3 coalesce; --组合
c:/>alter index pk_e3 rebuild; --重建
组合和重建的区别:合并不释放段所拥有的空间,不处理正在变化的行,就是说有事物的时候也可以合并。合并只
是合并枝干内的叶子,如果叶子属于不同的枝干则分别独立合并,合并不能改变索引的结构。不会改变索引的表空间和索引类型。
重建只能在没有事物的情况下进行,如是有未提交的事物,则报错。
alter index pk_e3 rebuild
ERROR ORA_000054:resource busy and acquire with nowait specified
alter index pk_e3 rebuild online --挂起
select sid,requets from v$lock where request <>0 --查看sid
select event from v$session_wait where sid= --查询等待会话事件
c:/>alter index scott.pk_e3 monitoring usage; --启用监视索引
索引已更改。
c:/>conn scott/tiger
已连接。
c:/>select * from v$object_usage; --查看是否被使用
INDEX_NAME TABLE_NAME MON USE
------------------------------ ------------------------------ --- ---
START_MONITORING END_MONITORING
------------------- -------------------
PK_E3 E3 YES NO
02/11/2010 09:52:28
c:/>alter index scott.pk_e3 nomonitoring usage; --取消监视
--查询约束信息
c:/>select constraint_name,table_name,deferrable,deferred,status,validated from user_constraints;
--为表创建延迟唯一约束 在Commit时候去检查约束 默认为immediate,执行语句时候检查约束
c:/>create table te as select * from emp;
c:/>alter table te add constraint u_empno unique(empno) initially deferred deferrable;
alter table te disable novalidate constraint pk_empno;
alter table te disable validate constraint pk_empno;
alter table te enable novalidate constraint pk_empno;
alter table te enable validate constraint pk_empno;
----------------------------------------------------------------------------------