drop table test purge;
create table test as select * from dba_objects;
update test set object_id =rownum ;
set timing on
set linesize 1000
set autotrace on
执行
select count(*) from test;
select count(object_id) from test;
发现耗时是一样的,难道他们的效率其实是一样的吗?
我们在列object_id上创建索引试试看
create index idx_object_id on test(object_id);
然后再执行
select count(*) from test;
select count(object_id) from test;
select * from dept where deptno NOT IN ( select deptno from emp ) ;
select * from dept where not exists ( select deptno from emp where emp.deptno=dept.deptno) ;
我们发现,exist确实比in的效率高啊。这个说法貌似是成立的啊。
但是我们再执行下面的语句
select * from dept where deptno NOT IN ( select deptno from emp where deptno is not null) and deptno is not null;
select * from dept where deptno NOT IN ( select deptno from emp ) ;
select * from dept where not exists ( select deptno from emp where emp.deptno=dept.deptno) ;
create table tab_big as select * from dba_objects where rownum<=30000;
create table tab_small as select * from dba_objects where rownum<=10;
set autotrace traceonly
set linesize 1000
set timing on
select count(*) from tab_big,tab_small ;
select count(*) from tab_small,tab_big ;