12C标量子查询中的BUG

在12C中,cbo会自动改写标量子查询了:

create table test as select * frm dba_objects;
create table test3 as select * from test;

select (select max(DATA_OBJECT_ID) from test b where b.object_id=a.object_id )  from test3  a;

该语句在11G中的plan为

 Plan Hash Value  : 

--------------------------------------------------------------------------
| Id  | Operation            | Name  | Rows  | Bytes   | Cost | Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |       | 94599 | 1229787 |  347 | 00:00:05 |
|   1 |   SORT AGGREGATE     |       |     1 |      26 |      |          |
| * 2 |    TABLE ACCESS FULL | TEST  |   870 |   22620 |  347 | 00:00:05 |
|   3 |   TABLE ACCESS FULL  | TEST3 | 94599 | 1229787 |  347 | 00:00:05 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
------------------------------------------
* 2 - filter("B"."OBJECT_ID"=:B1)

而在12C中被CBO改写成了left join

12C标量子查询中的BUG_第1张图片


如果把max改成rownum <=1,cbo不会进行改写

12C标量子查询中的BUG_第2张图片


如果同时用了max与rownum<=1 就会出现错误的plan和结果

SQL> explain plan for select (select max(b.DATA_OBJECT_ID) from test b where b.object_id=a.object_id and rownum=1)  from test3  a;

Explained.

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------------
Plan hash value: 730023338

-------------------------------------------------------------------------------------------
| Id  | Operation                      | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT               |          | 91583 |  2772K|   412   (1)| 00:00:01 |
|*  1 |  HASH JOIN RIGHT OUTER         |          | 91583 |  2772K|   412   (1)| 00:00:01 |
|   2 |   VIEW                         | VW_SSQ_1 |     1 |    26 |     2  (50)| 00:00:01 |
|   3 |    HASH GROUP BY               |          |     1 |     7 |     2  (50)| 00:00:01 |
|*  4 |     COUNT STOPKEY              |          |       |       |            |          |
|   5 |      TABLE ACCESS INMEMORY FULL| TEST     |     1 |     7 |     1   (0)| 00:00:01 |
|   6 |   TABLE ACCESS FULL            | TEST3    | 91583 |   447K|   410   (1)| 00:00:01 |
-------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("ITEM_1"(+)="A"."OBJECT_ID")
   4 - filter(ROWNUM=1)

19 rows selected.


当然,几乎没人同时用max和rownum的



你可能感兴趣的:(改写优化)