[20170816]Join Elimination Bug.txt

简介: [20170816]Join Elimination Bug.txt https://jonathanlewis.wordpress.com/2017/08/14/join-elimination-bug/ --//自己重复测试1次.

[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

目录
相关文章
|
Oracle 关系型数据库 OLAP
[20180224]expdp query 写法问题.txt
[20180224]expdp query 写法问题.txt --//如果使用expdp/impdp导入导出,如果参数复杂,最好的建议使用参数文件.避免各种问题.通过简单的例子说明问题.
1215 0
|
SQL Perl 关系型数据库
[20171220]toad plsql显示整形的bug.txt
toad 显示 整形 异常
1335 0
|
Oracle 关系型数据库 Linux
[20171031]rman merge例子2.txt
[20171031]rman merge例子2.txt --//以前做的测试 [20170626]rman merge例子.txt --//链接 http://blog.
1009 0
|
Oracle 关系型数据库 Linux
[20170626]rman merge例子.txt
[20170626]rman merge例子.txt 1.环境: SCOTT@book> @ &r/ver1 PORT_STRING                    VERSION        BANNER -----------------...
934 0
|
算法 索引 关系型数据库
[20170601]distinct的优化.txt
[20170601]distinct的优化.txt 1.环境: SCOTT@book> @ &r/ver1 PORT_STRING          VERSION    BANNER -------------------- ---------- ...
1095 0
|
关系型数据库 Oracle
[20160730]hint 冲突.txt
[20160730]hint 冲突.txt --昨天别人优化加提示无效,问我为什么无效?我一般认为这种情况称为hint 冲突. --通过例子来说明,我测试会使用ordered,我一般不喜欢使用ordered提示,通过例子来说明.
776 0
|
移动开发 索引
[20160318]push_pred hint使用疑惑.txt
[20160318]push_pred hint使用疑惑.txt --前几天看的帖子,链接如下: http://www.itpub.net/thread-2054898-1-1.
1273 0
|
Oracle 关系型数据库 索引
[20160118]提示index_join.txt
[20160118]提示index_join.txt --生产系统优化,遇到1例使用index_join提示的异常情况,通过例子来说明: 1.环境: 1.环境: SCOTT@book> @ &r/ver1 PORT_STRING               ...
927 0
|
索引
[20150814]使用use_concat提示.txt
[20150814]使用use_concat提示.txt SCOTT@test> @ver1 PORT_STRING                    VERSION        BANNER ---------------------------...
958 0

热门文章

最新文章