[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

时间: 2024-09-10 19:52:09

[20170816]Join Elimination Bug.txt的相关文章

[20131221]12c 优化 bug.txt

[20131221]12c 优化 bug.txt http://connormcdonald.wordpress.com/2013/12/20/the-challenge-of-optimization/ --仅仅自己重复测试看看! @ver BANNER                                                                               CON_ID ------------------------------------

[20140428]建立表空间的bug?.txt

[20140428]建立表空间的bug?.txt http://teymur-hajiyev.blogspot.com/2014/04/never-hurry-up-for-telling-it-is-bug-in.html SYS@test> @ver BANNER -------------------------------------------------------------------------------- Oracle Database 11g Enterprise Edi

9i index bug.txt 之2

9i index bug.txt 之2 1.接着以上的测试: SQL> SELECT object_id FROM dba_objects WHERE object_name = 'I_T1_A'; OBJECT_ID----------     45851         To then do a treedump of the index: SQL> ALTER SESSION SET EVENTS 'immediate trace name treedump level 45851';S

[20171220]toad plsql显示整形的bug.txt

[20171220]toad plsql显示整形的bug.txt --//下午有itpub网友反应,一个查询在sqlplus,pl/sql下不同.链接如下: --//http://www.itpub.net/thread-2095697-1-1.html --//我测试感觉是数据出现错误.直接那它的数据测试看看. 1.环境: SCOTT@book> @ &r/ver1 PORT_STRING                    VERSION        BANNER ---------

【SQL 学习】表连接--natural join 的一个bug

自然连接(NATURAL JOIN)是一种特殊的等价连接,它将表中具有相同名称的列自动进行记录匹配.自然连接不必指定任何同等连接条件.这篇文章讲的一个关于natural join 的bug!(由 dingjun123 提示!) SQL> conn store/yang 已连接. SQL> create table a as select * from all_objects; 表已创建. SQL> set timing on SQL> create table b as selec

join命令的用法

join命令的用法 j o i n用来将来自两个分类文本文件的行连在一起. 下面讲述j o i n工作方式.这里有两个文件f i l e 1和f i l e 2,当然已经分类.每个文件里都有一些元素与另一个文件相关.由于这种关系, j o i n将两个文件连在一起,这有点像修改一个主文件,使之包含两个文件里的共同元素. 文本文件中的域通常由空格或t a b键分隔,但如果愿意,可以指定其他的域分隔符.一些系统要求使用j o i n时文件域要少于2 0,为公平起见,如果域大于2 0,应使用D B M

[20160517]11GR2Cursor_Sharing=force的bug

[20160517]11GR2Cursor_Sharing=force的bug.txt --链接https://jonathanlewis.wordpress.com/2016/05/16/cursor_sharing-problem/,重复测试: 1.环境: SCOTT@book> @ &r/ver1 PORT_STRING         VERSION    BANNER ------------------- ---------- --------------------------

[20140718]12c-Partial Join Evaluation

[20140718]12c执行计划新特性- Partial Join Evaluation (PJE).txt --以前经常一些blogl讲什么使用in还是exists好的相关讨论,实际上11g以上的版本查询转换多数情况下会选择好的执行计划,只要建立好 --对应的约束,索引建立好,oracle多会选择好的执行计划. --12c在这些基础上引入了Partial Join Evaluation (PJE),能够进一步减少逻辑读,还是通过例子来说明问题: SCOTT@test01p> @ver BAN

oracle 9i index bug?

9i index bug.txt 1.建立表以及索引SQL> select * from v$version ;BANNER----------------------------------------------------------------Oracle9i Enterprise Edition Release 9.2.0.8.0 - ProductionPL/SQL Release 9.2.0.8.0 - ProductionCORE    9.2.0.8.0       Produ