Oracle:哪个查询更有效?

时间:2016-09-13 07:44:29

标签: oracle

查询1

select student.identifier,
       id_tab.reporter_name,
       non_id_tab.reporter_name
from student_table student
    inner join id_table id_tab on (student.is_NEW = 'Y'
                                  and student.reporter_id = id_tab.reporter_id
                                  and id_tab.name in('name1','name2'))
    inner join id_table non_id_tab on (student.non_reporter_id = non_id_tab.reporter_id)

查询2

select student.identifier,
id_tab.reporter_name,non_id_tab.reporter_name
from student_table student,
     id_table id_tab,
     id_table non_id_tab
    where student.is_NEW = 'Y'
    and student.reporter_id = id_tab.reporter_id
    and id_tab.name in('name1','name2')
    and student.non_reporter_id = non_id_tab.reporter_id

由于这两个查询产生完全相同的输出,我假设它们在语法上是相同的(如果我错了,请纠正我)。 我想知道它们中的任何一个是否比另一个更有效。

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

我会按如下方式重写它,仅将ON用于JOIN条件并将过滤器移至WHERE条件:

...
from student_table student
    inner join id_table id_tab on ( student.reporter_id = id_tab.reporter_id )
    inner join id_table non_id_tab on (student.non_reporter_id = non_id_tab.reporter_id)
where student.is_NEW = 'Y'
  and id_tab.name in('name1','name2')

这应该提供更可读的查询;但是,无论你如何编写它(ANSI连接都是非常优选的),你应该检查解释计划,以了解查询将如何执行。

答案 1 :(得分:0)

表现而言,应该没有区别。 Oracle优化器创建的执行计划没有区别。

可读性而言,在WHERE子句中连接表是一种旧样式(SQL89)。 从SQL92及更高版本开始,建议使用JOIN语法。

相关问题