假设我有两张桌子,
Student Test
Id Name TestId Score StudentId
-- ---- ------ ---- ---------
1 Mark 774 100 1
2 Sam 774 89 2
3 John 775 78 3
现在我必须打印学生姓名,考试ID和每个学生的分数。
我知道它们都会产生相同的结果。但哪一个在性能方面更好?第二个是否找到笛卡尔积,然后应用滤波器(where子句)?
1.Select test.testid,student.name,test.score
from student
join test
on test.studentid=student.id
2.Select test.testid,student.name,test.score
from student,test
where test.studentid=student.id
答案 0 :(得分:1)
我不会直接回答你的问题,而是为你提供一个解决此类问题的工具。
Oracle提供了查看查询执行方式的方法。您可以看到如何执行对表的访问,查询执行所花费的时间,是否使用索引等等。
命令是:
在你的情况下,它会像这样简单:
EXPLAIN PLAN FOR
Select test.testid,student.name,test.score
from student
join test
on test.studentid=student.id;
EXPLAIN PLAN FOR
Select test.testid,student.name,test.score
from student,test
where test.studentid=student.id;
或使用autotrace:
Set autotrace on;
Select test.testid,student.name,test.score
from student
join test
on test.studentid=student.id;
Select test.testid,student.name,test.score
from student,test
where test.studentid=student.id;
如果是EXPLAIN PLAN
,结果将保存在一个特殊的表中,您可以查询它们。检查我链接到的文档,看看可以用它做什么。
答案 1 :(得分:-1)
两个查询都有相同的执行计划(至少在他们的MS SQL中)。第二个查询将被优化