我有2個mysql查詢,檢索相同的數據?,你認爲哪個更好用,爲什麼?考慮到標準和更好的代碼等。對不起,如果這是一個愚蠢的問題,但我是一隻好奇的貓!所以這裏有雲:什麼mysql查詢比較好?
查詢1:
SELECT *
FROM
((
SELECT u.username, u.picture, m.id, m.user_note, m.reply_id, m.reply_name, m.dt
FROM relationships r
JOIN notes m ON m.user_id = r.leader
JOIN user u ON r.leader = u.user_id
WHERE r.listener ='2'
)
UNION ALL
(
SELECT u.username, u.picture, m.id, m.user_note, m.reply_id, m.reply_name, m.dt
FROM notes m
JOIN user u ON m.user_id = u.user_id
WHERE u.user_id ='2'
)) d
WHERE d.dt < '2010-09-20_131830'
ORDER BY d.dt DESC
QUERY 2:
SELECT u.username, u.picture, m.id, m.user_note, m.reply_id, m.reply_name, m.dt
FROM notes m
INNER JOIN user u
ON m.user_id = u.user_id
WHERE (m.user_id = '2'
OR m.user_id IN (
SELECT r.leader
FROM relationships r
WHERE r.listener ='2'))
AND dt < '2010-09-20_131830'
ORDER BY dt DESC
爲什麼不嘗試爲每個查詢做一個EXPLAIN來確定MySQL認爲哪一個更高效,或者哪些更容易閱讀? – 2010-09-22 10:30:25
我用解釋,但我不明白他們在說什麼!加上第一個查詢花了'0.0013秒',而第二個查詢花了'0.0200'秒! – getaway 2010-09-22 10:34:12
參見例如這是一個快速介紹到'EXPLAIN':http://www.learn-mysql-tutorial.com/OptimizeQueries.cfm – Piskvor 2010-09-26 16:23:38