所以我有一个简单的查询如下:
Select DISTINCT A.col1 as col1
FROM Table A join Table B on(A.col2 = B.col2)
JOIN table C on(C.col1 = A.col3)
UNION
SELECT DISTINCT A.col1 as col1
FROM Table A
JOIN Table B on (A.col2 = B. col2) where A.col4 != 'somevalue';
因为它是可见的每次都在同一列上执行表A和表B连接。我无法找到一种可以利用这些细节并使查询更快的方法
答案 0 :(得分:0)
尝试将连接转换为EXISTS
和子查询逻辑,如下所示:
SELECT DISTINCT A.col1 AS col1
FROM TABLE A
WHERE EXISTS
(SELECT 1 FROM TABLE B WHERE A.col2 = B.col2
)
AND EXISTS
(SELECT 1 FROM TABLE C WHERE A.col1 = C.col3
)
UNION
SELECT DISTINCT A.col1 AS col1
FROM TABLE A
WHERE EXISTS
(SELECT 1 FROM TABLE B WHERE A.col2 = B.col2
)
AND A.col4 != 'somevalue');
答案 1 :(得分:0)
这应该是等价的,但它没有多大意义:
Select DISTINCT A.col1 as col1
FROM Table A
JOIN Table B on (A.col2 = B.col2)
WHERE EXISTS ( SELECT * FROM C WHERE C.col1 = A.col3 )
OR A.col4 != 'somevalue';