25 union代替or --优化主题系列

当SQL语句中 or条件上面有一个为子查询 这个时候就可以用union代替or或者你发现执行计划中的filter有or 并且or后面跟上子查询EXISTS(select...)的时候就要注意 比如:

25 union代替or --优化主题系列

当然了 当你看到operation中的filter也应该要注意这些

看到filter后有orexists(select xx)  则改成union

 

示例如下(请自己动手实验):
create table test1 as select * from dba_objects;

createtable test2 as select * from dba_objects;

createindex idx1 on test1(object_id);

createindex idx2 on test1(owner);

createindex idx3 on test2(object_id);

createindex idx4 on test2(owner);

BEGIN

  DBMS_STATS.GATHER_TABLE_STATS(ownname          => 'SCOTT',

                                tabname          => 'TEST1',

                               estimate_percent => 100,

                                method_opt       => 'for  columns owner size 200',

                               no_invalidate    => FALSE,

                                degree           => 1,

                                cascade          => TRUE);

END;

/

BEGIN

  DBMS_STATS.GATHER_TABLE_STATS(ownname          => 'SCOTT',

                                tabname          => 'TEST2',

                               estimate_percent => 100,

                                method_opt       => 'for  columns owner size 200',

                               no_invalidate    => FALSE,

                                degree           => 1,

                                cascade          => TRUE);

END;

/

select* from test1 where owner='SCOTT' or object_id in(select object_id from test2where owner='SCOTT');

25 union代替or --优化主题系列

select* from test1 where owner='SCOTT'

union

select* from test1 where object_id in(select object_id from test2 whereowner='SCOTT');

25 union代替or --优化主题系列

逻辑读从184742到1221

25 union代替or --优化主题系列