添加左连接会减少返回的行数

时间:2014-08-07 22:44:39

标签: sql oracle outer-join oracle12c

我正在尝试调试以下SQL,其最后一段是左连接。当我运行下面的查询时,我得到8行,但如果我删除最后一个左连接,我得到13个结果。我的理解是左连接永远不应该减少返回的行数,这怎么可能?

SELECT  *
  FROM
(
  SELECT   A.QUESTIONNAIRE_ID,
      D.QUESTION_ANSWER_ID   ,
      D.QUESTION_REPLICA_ID
    FROM ADMINISTRATIONS A,
      ADMINISTRATION_DETAILS D
    WHERE A.ADMINISTRATION_ID = 4326509
      AND A.ADMINISTRATION_ID =D.ADMINISTRATION_ID
)
T
  JOIN QUESTION_ANSWERS QA
  ON QA.QUESTION_ANSWER_ID=T.QUESTION_ANSWER_ID
  JOIN QUESTIONS Q
  ON Q.QUESTION_ID=QA.QUESTION_ID
  JOIN ANSWERS A
  ON A.ANSWER_ID=QA.ANSWER_ID
  JOIN PHRASES PA
  ON PA.PHRASE_ID=A.PHRASE_ID
  JOIN QUESTIONNAIRE_DETAILS QD
  ON QD.QUESTIONNAIRE_ID      = T.QUESTIONNAIRE_ID
    AND QD.QUESTION_ID        =QA.QUESTION_ID
    AND QD.QUESTION_REPLICA_ID=1
  LEFT JOIN QUESTIONNAIRE_DETAILS D
  ON D.QUESTIONNAIRE_ID      = T.QUESTIONNAIRE_ID
    AND D.QUESTION_ID        =QA.QUESTION_ID
    AND D.QUESTION_REPLICA_ID=T.QUESTION_REPLICA_ID
  LEFT JOIN ANSWER_VALUES AV
  ON AV.QUESTIONNAIRE_ID=T.QUESTIONNAIRE_ID
    AND AV.QUESTION_ID  =QA.QUESTION_ID
    AND AV.ANSWER_ID    =QA.ANSWER_ID
  LEFT JOIN ADMINISTRATION_DETAILS AD
  ON AD.ADMINISTRATION_ID     =4326509
    AND AD.QUESTION_ANSWER_ID =T.QUESTION_ANSWER_ID
    AND AD.QUESTION_REPLICA_ID=T.QUESTION_REPLICA_ID
  LEFT JOIN ADMINISTRATION_OPEN_ANSWERS O
  ON O.ADMINISTRATION_ID     =AD.ADMINISTRATION_ID
    AND O.QUESTION_ANSWER_ID =AD.QUESTION_ANSWER_ID
    AND O.QUESTION_REPLICA_ID=AD.QUESTION_REPLICA_ID
  LEFT JOIN --the offending left join that causes me to get 8 results instead of 13
    (
      SELECT 1 AS destination_id FROM dual
    )
    RD
  ON RD.DESTINATION_ID       = 0
    AND O.QUESTION_ANSWER_ID = 1328 --If I remove just this line, I get 13 results
    ;

似乎正在发生的事情是,正在强制执行检查,其中表O中没有条目的行被删除,但我找不到任何文档解释这是预期的行为。

我正在运行Oracle Database 12c版本12.1.0.1.0 - 64位生产

1 个答案:

答案 0 :(得分:1)

如果我用

回滚它,它看起来像是优化器中的错误
ALTER SESSION SET optimizer_features_enable='11.2.0.4';

它开始表现正常。

编辑:这是一个更持久的解决方案:

CREATE OR REPLACE TRIGGER revert_optimizer AFTER LOGON ON DATABASE
  DECLARE
    CMD VARCHAR2(100) := 'alter session set optimizer_features_enable="11.2.0.4"';
  BEGIN
    EXECUTE IMMEDIATE CMD;
  END revert_optimizer;