繁体   English   中英

在Oracle中加入子句和Where子句

[英]Join Clause and Where Clause in Oracle

假设我有两个表,

   Student                           Test

Id    Name                   TestId   Score  StudentId
--    ----                   ------   ----  ---------  
1    Mark                      774     100       1   
2     Sam                      774      89       2  
3    John                      775      78       3

现在,我必须打印每个学生的学生姓名,测试ID和分数。

我知道它们都产生相同的结果,但是哪个在性能上更好?第二个是否找到笛卡尔积,然后应用filter(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

我不会直接回答您的问题,而是为您提供将来解决此类问题的工具。

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;

或使用自动跟踪:

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 ,结果将保存在一个特殊的表中,您可以查询该表以查看它们。 查看我链接到的文档,以查看可以做什么。

这两个查询将具有相等的执行计划(至少在MS SQL中如此)。 第二个查询将被优化

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM