我有返回相同的數據的兩個不同的查詢:查詢區別:分組JOIN VS直列JOIN
+---------+-------------------------------+----------+
| title | body | username |
+---------+-------------------------------+----------+
| Welcome | You got a cool forum here | john |
| Welcome | Great topics. | boyd |
| Welcome | Nice to have you as members | cris |
| Looking | I have the time and knowlegde | boyd |
| Looking | I'm fully qualified for this | joe |
+---------+-------------------------------+----------+
查詢我:
SELECT posts.title,comments.body,users.username
FROM posts
LEFT JOIN (
users INNER JOIN comments ON users.id = comments.user_id
) ON posts.id = comments.post_id
查詢II:
SELECT posts.title,comments.body,users.username
FROM posts
LEFT JOIN comments ON posts.id = comments.post_id
INNER JOIN users ON users.id = comments.user_id
而且這些是我的表格:
USERS
+----+----------+
| id | username |
+----+----------+
| 1 | john |
| 2 | boyd |
| 3 | ella |
| 4 | cris |
| 5 | joe |
| 6 | esses |
| 18 | test2 |
+----+----------+
POSTS
+----+-----------------------+
| id | title |
+----+-----------------------+
| 1 | Welcome |
| 2 | Looking for moderator |
+----+-----------------------+
COMMENTS
+---------+---------+------------------------------------------+
| post_id | user_id | body |
+---------+---------+------------------------------------------+
| 1 | 1 | You got a cool forum here |
| 1 | 2 | Great topics. |
| 1 | 4 | Nice to have you as members |
| 2 | 2 | I have the time and knowlegde to do this |
| 2 | 5 | I'm fully qualified for this job |
+---------+---------+------------------------------------------+
我的問題是:這兩個查詢之間的真正區別是什麼?
編輯:這是EXPLAIN EXTENDED結果:
查詢我:
- EXPLAIN EXTENDED
+----+-------------+----------+--------+---------------+---------+---------+----------------------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+--------+---------------+---------+---------+----------------------+------+----------+-------------+
| 1 | SIMPLE | posts | ALL | NULL | NULL | NULL | NULL | 2 | 100.00 | NULL |
| 1 | SIMPLE | comments | ALL | NULL | NULL | NULL | NULL | 5 | 100.00 | Using where |
| 1 | SIMPLE | users | eq_ref | PRIMARY | PRIMARY | 4 | app.comments.user_id | 1 | 100.00 | NULL |
+----+-------------+----------+--------+---------------+---------+---------+----------------------+------+----------+-------------+
- SHOW警告
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note | 1003 | /* select#1 */ select `app`.`posts`.`title` AS `title`,`app`.`comments`.`body` AS `body`,`app`.`users`.`username` AS `username` from `app`.`posts` left join (`app`.`users` join `app`.`comments`) on(((`app`.`posts`.`id` = `app`.`comments`.`post_id`) and (`app`.`users`.`id` = `app`.`comments`.`user_id`))) where 1 |
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
查詢II:
- EXPLAIN EXTENDED
+----+-------------+----------+--------+---------------+---------+---------+----------------------+------+----------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+--------+---------------+---------+---------+----------------------+------+----------+----------------------------------------------------+
| 1 | SIMPLE | comments | ALL | NULL | NULL | NULL | NULL | 5 | 100.00 | Using where |
| 1 | SIMPLE | users | eq_ref | PRIMARY | PRIMARY | 4 | app.comments.user_id | 1 | 100.00 | NULL |
| 1 | SIMPLE | posts | ALL | PRIMARY | NULL | NULL | NULL | 2 | 100.00 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+----------+--------+---------------+---------+---------+----------------------+------+----------+----------------------------------------------------+
- SHOW警告
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note | 1003 | /* select#1 */ select `app`.`posts`.`title` AS `title`,`app`.`comments`.`body` AS `body`,`app`.`users`.`username` AS `username` from `app`.`posts` join `app`.`comments` join `app`.`users` where ((`app`.`posts`.`id` = `app`.`comments`.`post_id`) and (`app`.`users`.`id` = `app`.`comments`.`user_id`)) |
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
我不認爲有任何區別。 – Barmar
運行EXPLAIN EXTENDED ...;其次是SHOW WARNINGS;在這兩個查詢 – Strawberry
@草莓,@巴爾馬爾。我只是添加了解釋擴展結果。它看起來像第一個查詢執行少一個額外的查詢。 – boyd