2、存在外键时监控索引情形
SQL> create table ctb(id number,name varchar2(30),deptno number);
Table created.
--为子表添加外键约束
SQL> alter table ctb add constraint ctb_fk foreign key(deptno) references ptb(deptno)
2 on delete set null;
Table altered.
--为子表填充数据
SQL> begin
2 for i in 1 .. 1000000
3 loop
4 insert into ctb values(i,'name_'||to_char(i),10);
5 end loop;
6 commit;
7 end;
8 /
PL/SQL procedure successfully completed.
--基于外键创建索引
SQL> create index i_ctb_fk_deptno on ctb(deptno) nologging;
Index created.
--收集子表的统计信息
SQL> exec dbms_stats.gather_table_stats('SCOTT','CTB',cascade=>true);
PL/SQL procedure successfully completed.
SQL> @dba_table_info
Enter Schema (i.e. SCOTT) : SCOTT
Enter Schema (i.e. EMP) : CTB
+----------------------------------------------------------------------------+
| TABLE INFORMATION |
+----------------------------------------------------------------------------+
Owner Table Name Tablespace Last Analyzed # of Rows
--------------- --------------- ---------------------------- -------------------- ------------
SCOTT CTB GOEX_SYSTEM_TBL 22-MAR-2013 17:26:02 1,000,731
+----------------------------------------------------------------------------+
| CONSTRAINTS |
+----------------------------------------------------------------------------+
Constraint
Constraint Name Type Column Name Search Conditio R / Constraint Name Delete Rule Status
------------------ ----------- ------------------ --------------- -------------------- ----------- ---------
CTB_FK Referential DEPTNO SCOTT.PTB_PK SET NULL ENABLED
--上面的统计信息中列出的行数为1,000,731比实际要多
SQL> select count(*) from ctb;
COUNT(*)
----------
1000000
SQL> select * from ctb where rownum<3;
ID NAME DEPTNO
---------- ---------- ----------
1045 name_1045 10
1046 name_1046 10
--开启监控外键索引
SQL> alter index i_ctb_fk_deptno monitoring usage;
Index altered.
SQL> select index_name,monitoring,used from v$object_usage where index_name='I_CTB_FK_DEPTNO';
INDEX_NAME MON USE
------------------------------ --- ---
I_CTB_FK_DEPTNO YES NO
--开启autotrace
SQL> set autot trace stat;
SQL> delete from ptb where deptno=20; 从主表删除一条记录
1 row deleted.
Statistics
----------------------------------------------------------
172 recursive calls
7 db block gets
31 consistent gets --->此时一致读为31
1 physical reads
780 redo size
1114 bytes sent via SQL*Net to client
1184 bytes received via SQL*Net from client
4 SQL*Net roundtrips to/from client
4 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> commit;
Commit complete.
-- Author : Robinson
-- Blog : http://blog.csdn.net/robinson-0612
SQL> set autot off;
--下面的查询可以看出外键索引没有被使用到
SQL> select index_name,monitoring,used from v$object_usage where index_name='I_CTB_FK_DEPTNO';
INDEX_NAME MON USE
------------------------------ --- ---
I_CTB_FK_DEPTNO YES NO
