我正在使用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-Version是Oracle 10g。 Oracle SQL Developer版本是3.2.09。
Autotrace和Explainplan之间的比较显示了使用两个select语句从ID_1(+) = ID_2
(Explainplan)到ID_1 = ID_2
(Autotrace)的一些Access谓词的差异。
我非常怀疑查询优化器搞砸了。我还没有找到修改其设置的方法。在该方向上的任何输入可能会有所帮助。
答案 0 :(得分:2)
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)