优化SQL查询

时间:2015-08-05 12:37:48

标签: sql oracle inner-join

我有一个SQL查询,它在两个表上执行INNER JOIN,每个表都有> 50M行。我希望通过减少基于其中一个表上的列连接的行来减少搜索连接所需的时间。

假设我的table1包含列A,B,C和table2,列A,D,E。我希望基于A列加入,但只有那些有价值的行#e;对于表2的E列。

我的SQL查询:

SELECT one.B, two.D
FROM table1 one
INNER JOIN table2 two WHERE two.E IN ('e')
ON one.A = two.A
WHERE one.B > 10
AND two.D IN ('...')

它给出错误:

ORA-00905: missing keyword

我哪里错了?我如何达到预期的效果?

3 个答案:

答案 0 :(得分:3)

SELECT one.B, two.D
FROM table1 one
INNER JOIN table2 two -- WHERE two.E IN ('e')  --> shouldn't use where here
ON one.A = two.A and two.E = 'e'
WHERE one.B > 10
AND two.D IN ('...')

代码中包含的评论。

答案 1 :(得分:0)

您也可以将条件放在Where子句

SELECT one.B, two.D
FROM table1 a
   JOIN table2 b
       ON b.A = a.A 
WHERE a.B > 10
   And b.E = 'e'
   AND b.D In ('...')

已删除第2条不正确的建议

答案 2 :(得分:0)

正如vkp指出的那样,WHERE使用不当。相反,您也可以创建子查询以包含where语句。那样:

INNER JOIN table2 two WHERE two.E IN ('e')

变为

INNER JOIN (select * from table2 WHERE E IN ('e')) two