简体   繁体   English

在Oracle中加入子句和Where子句

[英]Join Clause and Where Clause in Oracle

Assume I have two tables, 假设我有两个表,

   Student                           Test

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

Now I have to print student name,test id and score of each student. 现在,我必须打印每个学生的学生姓名,测试ID和分数。

I know both of them produce the same results.But which one is better in terms of performance?Does the second one find cartesian product and then apply filter(where clause)? 我知道它们都产生相同的结果,但是哪个在性能上更好?第二个是否找到笛卡尔积,然后应用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

I'm not going to answer your question directly but rather provide you with a tool to solve such problems in the future. 我不会直接回答您的问题,而是为您提供将来解决此类问题的工具。

Oracle offers ways to see how queries are executed. Oracle提供了查看查询执行方式的方法。 You can see how the access to your tables is performed, how much time a query execution takes, whether indexes are used or not, etc. 您可以看到如何执行对表的访问,执行查询需要多少时间,是否使用索引等。

The commands are: 这些命令是:

EXPLAIN PLAN and SET AUTOTRACE . 解释计划设置自动跟踪

In your case, it would be as simple as this: 在您的情况下,它就像这样简单:

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;

Or using 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;

In case of EXPLAIN PLAN , the results are kept in a special table that you can query to see them. 如果使用EXPLAIN PLAN ,结果将保存在一个特殊的表中,您可以查询该表以查看它们。 Check the documentation I linked to in order to see what can be done with it. 查看我链接到的文档,以查看可以做什么。

Both queries will have equal execution plans (at least in MS SQL they do). 这两个查询将具有相等的执行计划(至少在MS SQL中如此)。 Second query will be optimized 第二个查询将被优化

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

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