2

我正在使用 Oracle SQL Developer,我正在尝试运行这样的查询

SELECT * FROM 
  (SELECT id1 CONTRACT_ID1, id2 CONTRACT_ID2 FROM contract_table)
  LEFT JOIN
  (SELECT id1 A_CONTRACT_ID, id2 A_SPECIAL_CONTRACT_ID from special_contract_table_a)
  on (CONTRACT_ID1 = A_CONTRACT_ID)
  LEFT JOIN
  (SELECT id1 B1_CONTRACT_ID, id2 B1_DOCUMENT_ID from document_table_b1)
  ON (CONTRACT_ID2 = B1_CONTRACT_ID)
  LEFT JOIN
  (SELECT id1 B2_CONTRACT_ID, id2 B2_DOCUMENT_ID from document_table_b2)
  ON (A_SPECIAL_CONTRACT_ID = B2_CONTRACT_ID)
  LEFT JOIN
  (SELECT id1 B3_CONTRACT_ID, id2 B3_DOCUMENT_ID from document_table_b3)
  ON (A_SPECIAL_CONTRACT_ID = B3_CONTRACT_ID)
  LEFT JOIN
  (SELECT page PAGE, id1 C1_DOCUMENT_ID, id2 C2_DOCUMENT_ID, id3 C3_DOCUMENT_ID from page_table_c)
  ON (C1_DOCUMENT_ID = B1_DOCUMENT_ID OR C2_DOCUMENT_ID = B2_DOCUMENT_ID OR C3_DOCUMENT_ID = B3_DOCUMENT_ID)
  LEFT JOIN
  (SELECT id2 B1_DOCUMENT_ID_2 from, status B1_DOCUMENT_STATUS from document_table_b1)
  ON (C1_DOCUMENT_ID = B1_DOCUMENT_ID_2)
  LEFT JOIN
  (SELECT id2 B2_DOCUMENT_ID_2 from, status B2_DOCUMENT_STATUS from document_table_b2)
  ON (C2_DOCUMENT_ID = B2_DOCUMENT_ID_2)
  LEFT JOIN
  (SELECT id2 B3_DOCUMENT_ID_2 from, status B3_DOCUMENT_STATUS from document_table_b3)
  ON (C3_DOCUMENT_ID = B3_DOCUMENT_ID_2)
;

所以基本上这样做是,我选择我的两个基本 ID,然后获取所有关联组的所有成员,然后获取每个成员的状态(并且状态存储在组声明中)。只要我使用SELECT * FROM. 如果我将其更改为SELECT CONTRACT_ID1, PAGE, B1_DOCUMENT_STATUS, B2_DOCUMENT_STATUS, B3_DOCUMENT_STATUS输出从 24 行折叠到 4 行,则仅显示document_table_b1.

知道如何解决这个问题吗?

附加信息:DB 版本是 Oracle 10g。Oracle SQL Developer 版本是 3.2.09。Autotrace 和 Explainplan 之间的比较显示了从ID_1(+) = ID_2(Explainplan) 到ID_1 = ID_2(Autotrace) 的某些访问谓词与两个 select 语句的差异。我高度怀疑查询优化器在这里搞砸了。我还没有找到修改其设置的方法。在这个方向上的任何输入都可能会有所帮助。

4

1 回答 1

2

哦,太难读了,为什么要把每个表都放到select语句中

SELECT a.id1 as CONTRACT_ID1
,ptc.page as PAGE
,b1.status as B1_DOCUMENT_STATUS
,b2.status as B2_DOCUMENT_STATUS
,b3.status as B3_DOCUMENT_STATUS
FROM 
   contract_table ct 
  LEFT JOIN special_contract_table_a scta  on scta.id1 = ct.id1
  LEFT JOIN document_table_b1 b1 ON b1.id1 = ct.id2
  LEFT JOIN document_table_b2 b2 ON b2.id1 = scta.id2 
  LEFT JOIN document_table_b3 b3 ON b3.id1 = scta.id2
  LEFT JOIN page_table_c ptc ON c.id1 = b1.id1 or c.id2= b2.id1 or c.id3 = b3.id1
  where (YoursPredicateHere)
  --dont understand why you are joining them again 
  --LEFT JOIN document_table_b1 b1_2 ON (C1_DOCUMENT_ID = B1_DOCUMENT_ID_2)
  --LEFT JOIN document_table_b2 b2_2 ON (C2_DOCUMENT_ID = B2_DOCUMENT_ID_2)
  --LEFT JOIN document_table_b3 b3_2 ON (C3_DOCUMENT_ID = B3_DOCUMENT_ID_2)
于 2013-01-25T09:46:42.407 回答