外键没有索引对性能的影响
大家都知道关于外键上没有索引会产生锁的出现,这里不再累述,本文只是就外键没有索引的情况下,Oracle在执行DML语句的时候,CBO为了保证数据的一致性而进行的隐形调用,而这些调用恰恰是反应在Oracle的DML语句执行的性能上,但是这些调用在你使用trace上是无法通过执行计划上看到的。
建立环境:
drop table tmp_f purge; drop table tmp_p purge; create table tmp_f as select rownum id,rownum num,a.* from all_objects a where 1=0; create table tmp_p(pk number primary key,id number); alter table tmp_f add constraint fk_tmp_f foreign key (id) references tmp_p(pk); insert into tmp_p select rownum ,rownum from all_objects a ; insert into tmp_f select rownum id,rownum num,a.* from all_objects a ; |
这里tmp_p表有
SQL> select count(*) from tmp_p; COUNT(*) ---------- 56129 COUNT(*) ---------- 1010307 SQL> select table_name,blocks,num_rows from user_tables; TABLE_NAME BLOCKS NUM_ROWS ------------------------------ ---------- ---------- TMP_P 118 56128 TMP_F 16217 1012057 SQL> |
1、在从表的外键没有索引的时候,delete主表上的一个数据。
insert into tmp_p values (100001,100001); commit SQL> delete from tmp_p where pk =100001; 已删除 1 行。 执行计划 ---------------------------------------------------------- Plan hash value: 797653082 ------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time ------------------------------------------------------------------------------- | 0 | DELETE STATEMENT | | 1 | 5 | 1 (0)| 00:00: | 1 | DELETE | TMP_P | | | | |* 2 | INDEX UNIQUE SCAN| SYS_C0034921 | 1 | 5 | 1 (0)| 00:00: ------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("PK"=100001) 统计信息 ---------------------------------------------------------- 6 recursive calls 7 db block gets 15504 consistent gets 0 physical reads 644 redo size 393 bytes sent via SQL*Net to client 323 bytes received via SQL*Net from client 4 SQL*Net roundtrips to/from client 1 sorts (memory) 0 sorts (disk) 1 rows processed |
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/222350/viewspace-1009122/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/222350/viewspace-1009122/