2012-07-26 66 views
0

條款假設我有兩個表,加入條款和凡在甲骨文

Student       Test 

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

現在我必須打印學生姓名,考號和每個學生的分數。

我知道他們都產生相同results.But哪一個是在性能方面更好呢?難道,第二個發現笛卡爾乘積,然後應用過濾器(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

是的,它是一個笛卡爾乘積與過濾器! – 2012-07-26 06:56:14

+4

唯一的區別是語法。查詢以相同的方式執行。連接語法通常是可取的,因爲它保持條件接近表格。一旦添加更多連接,可讀性的差異就會增加。 – Andomar 2012-07-26 07:02:01

回答

1

我不會直接回答你的問題,而是爲你提供一個工具來解決將來的這些問題。

Oracle提供的方式看到查詢是如何執行的。你可以看到如何訪問到你的表執行,查詢執行多少時間,是否使用索引或沒有等

的命令是:

EXPLAIN PLANSET AUTOTRACE

在你的情況,這將是如此簡單:

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情況下,結果被保存在一個特殊的表,你可以查詢以查看它們。查看我鏈接到的文檔以查看可以使用它做什麼。

-1

兩個查詢都享有平等的執行計劃(至少在MS SQL他們這樣做)。第二個查詢將被優化

+0

爲什麼要優化第二個查詢,而不是第一個? – Ollie 2012-07-26 11:56:20