0
我有2個SQL語句產生相同的結果,但想知道哪一個選擇?選擇使用較少的加載時間的SQL語句
可以說1有3個表:
- 供應商
- supplier_status
- supplier_contact
聲明1)
SELECT a.*, b.status_name
(SELECT c.name FROM contact c
WHERE c.supplier_id = a.supplier_id
ORDER BY c.contact_id DESC limit 1) AS contact_name
FROM supplier a LEFT JOIN supplier_status b
ON a.status_id = b.status_id
聲明2)
SELECT a.*, b.status_name, c.name AS contact_name
FROM supplier a LEFT JOIN supplier_status b
ON a.status_id = b.status_id
LEFT JOIN (SELECT name, supplier_id
FROM contact
ORDER BY contact_id DESC
) c ON a.supplier_id = c.supplier_id
GROUP BY a.supplier_id
那麼這取決於你想要你的代碼看起來像什麼。你想要我們的意見? – ouflak
也許這只是我,但我認爲你想要完成什麼的解釋將有助於獲得良好的答案。 –
用'EXPLAIN'運行它們來預置查詢,根據[EXPLAIN輸出]的文檔檢查結果(http://dev.mysql.com/doc/refman/5.7/en/explain-output.html) – axiac