[20170816]Join Elimination Bug.txt

[20170816]Join Elimination Bug.txt

https://jonathanlewis.wordpress.com/2017/08/14/join-elimination-bug/

--//自己重复测试1次.
1.环境:
SCOTT@book> @ &r/ver1

PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

rem     Script:         join_eliminate_bug_2.sql
rem     Author:         Jonathan Lewis
rem     Dated:          Dec 2012
 
drop table child purge;
drop table parent purge;
 
create table parent (
        id      number(4),
        name    varchar2(10),
        constraint par_pk primary key (id)
        deferrable initially immediate
)
;
 
create table child(
        id_p    number(4)      
                constraint chi_fk_par
                references parent,
        id      number(4),
        name    varchar2(10),
        constraint chi_pk primary key (id_p, id)
)
;
 
insert into parent values (1,'Smith');
insert into parent values (2,'Jones');
 
insert into child values(1,1,'Simon');
insert into child values(1,2,'Sally');
 
insert into child values(2,1,'Jack');
insert into child values(2,2,'Jill');
 
commit;
 
begin
        dbms_stats.gather_table_stats(user,'child');
        dbms_stats.gather_table_stats(user,'parent');
end;
/
 
set serveroutput off
 
select
        chi.*
from
        child   chi,
        parent  par
where
        par.id = chi.id_p
;

      ID_P         ID NAME
---------- ---------- --------------------
         1          1 Simon
         1          2 Sally
         2          1 Jack
         2          2 Jill

select * from table(dbms_xplan.display_cursor);
SCOTT@book> select * from table(dbms_xplan.display_cursor);
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  bf70xtmkt5wxv, child number 0
-------------------------------------
select         chi.* from         child   chi,         parent  par
where         par.id = chi.id_p
Plan hash value: 2406669797
---------------------------------------------------------------------------
| Id  | Operation         | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |       |       |       |     3 (100)|          |
|   1 |  TABLE ACCESS FULL| CHILD |     4 |    48 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------
14 rows selected.

--//可以发现取消了连接查询,仅仅查询child就知道结果,但是如果执行如下,延迟约束,问题就再现了.

On a single column join, with referential integrity in place, and no columns other than the primary key involved, the
optimizer eliminates table parent from the query. But if I now defer the primary key constraint on parent and duplicate
every row (which ought to duplicate the query result), watch what happens with the query:

set constraint par_pk deferred;
 
insert into parent (id,name) values (1,'Smith');
insert into parent (id,name) values (2,'Jones');
 
alter system flush shared_pool;
 
select
        chi.*
from
        child   chi,
        parent  par
where
        par.id = chi.id_p
;

      ID_P         ID NAME
---------- ---------- --------------------
         1          1 Simon
         1          2 Sally
         2          1 Jack
         2          2 Jill

SCOTT@book> select * from table(dbms_xplan.display_cursor);
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  bf70xtmkt5wxv, child number 0
-------------------------------------
select         chi.* from         child   chi,         parent  par
where         par.id = chi.id_p
Plan hash value: 2406669797
---------------------------------------------------------------------------
| Id  | Operation         | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |       |       |       |     3 (100)|          |
|   1 |  TABLE ACCESS FULL| CHILD |     4 |    48 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------
14 rows selected.

--//在这样的情况下存在错误,加入提示no_eliminate_join就能避免这个问题.

select  /*+ no_eliminate_join(@sel$1 par@sel$1) */
        chi.*
from
        child   chi,
        parent  par
where
        par.id = chi.id_p
;
      ID_P         ID NAME
---------- ---------- --------------------
         1          1 Simon
         1          1 Simon
         1          2 Sally
         1          2 Sally
         2          1 Jack
         2          1 Jack
         2          2 Jill
         2          2 Jill
8 rows selected.

SCOTT@book> @ &r/dpc '' outline
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  g7udbfpxa8wxw, child number 0
-------------------------------------
select  /*+ no_eliminate_join(@sel$1 par@sel$1) */         chi.* from
      child   chi,         parent  par where         par.id = chi.id_p
Plan hash value: 1687613841
------------------------------------------------------------------------------
| Id  | Operation          | Name   | E-Rows |E-Bytes| Cost (%CPU)| E-Time   |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |        |        |       |     3 (100)|          |
|   1 |  NESTED LOOPS      |        |      4 |    60 |     3   (0)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| CHILD  |      4 |    48 |     3   (0)| 00:00:01 |
|*  3 |   INDEX RANGE SCAN | PAR_PK |      1 |     3 |     0   (0)|          |
------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1
   2 - SEL$1 / CHI@SEL$1
   3 - SEL$1 / PAR@SEL$1
Outline Data
-------------
  /*+
      BEGIN_OUTLINE_DATA
      IGNORE_OPTIM_EMBEDDED_HINTS
      OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
      DB_VERSION('11.2.0.4')
      ALL_ROWS
      OUTLINE_LEAF(@"SEL$1")
      FULL(@"SEL$1" "CHI"@"SEL$1")
      INDEX(@"SEL$1" "PAR"@"SEL$1" ("PARENT"."ID"))
      LEADING(@"SEL$1" "CHI"@"SEL$1" "PAR"@"SEL$1")
      USE_NL(@"SEL$1" "PAR"@"SEL$1")
      END_OUTLINE_DATA
  */
Predicate Information (identified by operation id):
---------------------------------------------------
   3 - access("PAR"."ID"="CHI"."ID_P")

--//当然这样提交会报错的,破坏了唯一性约束.

SCOTT@book> commit ;
commit
*
ERROR at line 1:
ORA-02091: transaction rolled back
ORA-00001: unique constraint (SCOTT.PAR_PK) violated

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/267265/viewspace-2143607/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/267265/viewspace-2143607/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值