2015-10-18 47 views
0

我想了解連接查詢的過程。瞭解連接查詢

SELECT template.* 
FROM template_item AS template 
JOIN items ON items.id = template.id 
WHERE items.user_id = 1 AND template.type != 'short'; 

SELECT template.* 
FROM template_item AS template 
JOIN items ON items.id = template.id AND items.user_id = 1 
WHERE template.type != 'short'; 

哪一個更好?

在第一個查詢中,隨着「連接」的進行,它將獲取所有項目並在「where」之後應用過濾器。對 ?

對不起,我英語不好,我是法國人。

+0

檢查執行計劃 – lad2025

+0

爲什麼不得到解釋計劃?我會認爲這些可能是相同的 –

+1

https://dev.mysql.com/doc/refman/5.0/en/explain.html – Drew

回答

0
SELECT template.* 
FROM template_item AS template 
JOIN items ON items.id = template.id AND items.user_id = 1 
WHERE template.type != 'short'; 

可能是更好的一個,因爲它在「加盟」本身否定列。

嘗試

EXPLAIN SELECT template.* 
    FROM template_item AS template 
    JOIN items ON items.id = template.id AND items.user_id = 1 
    WHERE template.type != 'short'; 

EXPLAIN SELECT template.* 
FROM template_item AS template 
JOIN items ON items.id = template.id 
WHERE items.user_id = 1 AND template.type != 'short'; 

見「節數rows'.First查詢的差異將是有效的,當它遇到大的數據。希望這可以幫助。

+0

謝謝,有什麼區別? –

+0

「EXPLAIN」的輸出是什麼?如果您覺得它有用,請將其標記爲答案 –

+0

感謝您的幫助,並感謝您糾正我的文章。 –