简体   繁体   中英

ORACLE left outer join issue (with empty table?)

I'm wondering why this query:

select l.objectID LEFT_CODE, r.object_code RIGHT_CODE
from OGGETTO_ORGANIZZATIVO l left outer join
    ANAG_OGGETTO_ORGANIZZATIVO r on l.objectID = r.objectID
WHERE r.objectID IS NULL;

returns some rows, while this other query

select l.objectID LEFT_CODE
from OGGETTO_ORGANIZZATIVO l left outer join
    ANAG_OGGETTO_ORGANIZZATIVO r on l.objectID = r.objectID
WHERE r.objectID IS NULL;

doesn't return nothing. My problem is insert in right table all the rows present in left table but not in right table; at the moment the right table is empty.

It seems that if I am not referring the right field in the select clause, oracle will not manage the outer join.

Thanks in advance. Paolo

[UPDATE] First Query

select l.codice_oggetto_SAP
from oggetto_organizzativo l left outer join
  ANAG_OGGETTO_ORGANIZZATIVO_SAP r
    ON l.codice_oggetto_sap = r.codice_oggetto_SAP
WHERE r.codice_oggetto_sap is null

Execution Plan在此处输入图片说明

second query

select l.codice_oggetto_SAP, r.codice_oggetto_SAP
from oggetto_organizzativo l left outer join
  ANAG_OGGETTO_ORGANIZZATIVO_SAP r
    ON l.codice_oggetto_sap = r.codice_oggetto_SAP
WHERE r.codice_oggetto_sap is null

Execution Plan在此处输入图片说明

the first execution plan statement (NULL IS NOT NULL) seems to me curious...

[SECOND UPDATE] @toddlermenot,

But why this WORKS???:

CREATE TABLE LEFT_TBL
   (    "OBJECTID" VARCHAR2(20 BYTE)  ) ;

CREATE TABLE RIGHT_TBL
   (    "OBJECTID" VARCHAR2(20 BYTE)  ) ;

INSERT ALL
  INTO LEFT_TBL (OBJECTID) VALUES ('AAA')
  INTO LEFT_TBL (OBJECTID) VALUES ('BBB')
  INTO LEFT_TBL (OBJECTID) VALUES ('CCC')  
SELECT * from DUAL;

SELECT l.objectID
FROM LEFT_TBL l LEFT OUTER JOIN RIGHT_TBL r
  ON L.OBJECTID = r.OBJECTID
WHERE r.OBJECTID IS NULL;

witt this execution plan

Plan hash value: 2059691840

--------------------------------------------------------------------------------
| Id  | Operation          | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |           |     3 |    72 |     5   (0)| 00:00:01 |
|*  1 |  HASH JOIN ANTI    |           |     3 |    72 |     5   (0)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| LEFT_TBL  |     3 |    36 |     3   (0)| 00:00:01 |
|   3 |   TABLE ACCESS FULL| RIGHT_TBL |     1 |    12 |     2   (0)| 00:00:01 |
--------------------------------------------------------------------------------

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

   1 - access("L"."OBJECTID"="R"."OBJECTID")

Note
-----
   - dynamic sampling used for this statement (level=2)

Could be and Index problem?

You have posted the explain plan which might differ from the actual execution plan . Difference is explain plan is just what optimizer (CBO) thinks it will follow during the actual execution of the query which might deviate from the real execution plan .

With that said, If your explain plans are to be believed, then this definitely seems to be an optimizer bug as I hypothesized in my comment. For some reason in your first query, CBO is taking an incorrect shortcut and ignores the LEFT OUTER join (the right table is NOT accessed at all) and uses the strange NULL is NOT NULL predicate as the final filter which is always going to be FALSE meaning that your final result set is always going to be empty.

To resolve this, you might want to raise a ticket with Oracle support and patch your database to get this bug fixed.

EDIT: If the statistics on both the tables are out of date, gather stats on both tables and try rerunning the query. CBO might take the correct plan after the stats are collected. Statistics being out of date is still however not an excuse for CBO to choose an incorrect execution plan. You might want to do this after you raise the ticket to Oracle Support, so that this incorrect behavior is still reproducible when they work on it.

Bug 10269193 - Wrong results with outer join and CASE expression optimization (CASE need not to be present) or LIKE operator (Doc ID 10269193.8)

Product (Component)

Oracle Server (Rdbms)

Range of versions believed to be affected

Versions >= 11.2 but BELOW 12.1

Symptoms:

Related To:

  • Internal Error May Occur (ORA-600)
  • Wrong Results
  • ORA-600 [kkfdjoi:kkfdnpart_DIM1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM