我有两个表格,如下所示:
CREATE TABLE `user_group` (
`user_group_id` INT(11) NOT NULL AUTO_INCREMENT,
`user_id` INT(11) NOT NULL,
`group_id` INT(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`user_group_id`),
KEY `user_id` (`user_id`),
KEY `group_id` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `group` (
`group_id` INT(11) NOT NULL AUTO_INCREMENT,
`division_id` INT(11) NOT NULL DEFAULT '0',
`office_id` INT(11) NOT NULL DEFAULT '0',
`dept_id` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`group_id`),
KEY `division_id` (`division_id`),
KEY `office_id` (`office_id`),
KEY `dept_id` (`dept_id`),
KEY `division_dept` (`dept_id`,`division_id`),
KEY `office_dept` (`dept_id`,`office_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
以下是一些示例数据(这些表的实际行数分别约为20,000和1,000,000):
+----------+-------------+-----------+---------+
| group_id | division_id | office_id | dept_id |
+----------+-------------+-----------+---------+
| 120 | 1 | 50 | 760 |
| 121 | 1 | 50 | 761 |
| 122 | 1 | 51 | 762 |
| 123 | 2 | 60 | 763 |
| 124 | 2 | 62 | 764 |
| 125 | 2 | 63 | 765 |
+----------+-------------+-----------+---------+
+---------------+---------+----------+
| user_group_id | user_id | group_id |
+---------------+---------+----------+
| 1 | 1013410 | 122 |
| 2 | 1020345 | 122 |
| 3 | 1006021 | 125 |
| 4 | 1014809 | 124 |
| 5 | 3117387 | 123 |
| 6 | 3123441 | 125 |
| 7 | 1031113 | 123 |
| 8 | 1024314 | 122 |
| 9 | 1011757 | 120 |
| 10 | 1000795 | 122 |
| 11 | 1003932 | 123 |
| 12 | 3103704 | 120 |
| 13 | 1015192 | 123 |
| 14 | 1005211 | 125 |
| 15 | 1025876 | 120 |
| 16 | 1006252 | 121 |
| 17 | 1006657 | 125 |
| 18 | 1025972 | 125 |
| 19 | 1010517 | 121 |
| 20 | 1001044 | 120 |
| 21 | 1032961 | 123 |
| 22 | 1015185 | 125 |
| 23 | 1018973 | 120 |
| 24 | 1031082 | 120 |
| 25 | 1028828 | 124 |
| 26 | 1024051 | 122 |
| 27 | 1000927 | 120 |
| 28 | 1000608 | 121 |
| 29 | 1004419 | 124 |
| 30 | 1015102 | 124 |
+---------------+---------+----------+
比较EXPLAIN
对这两个查询的输出:
EXPLAIN
SELECT user_id
FROM `group`
LEFT JOIN user_group USING (group_id)
WHERE division_id = 2;
+----+-------------+------------+------------+------+---------------+-------------+---------+-------------------------------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+-------------+---------+-------------------------------+------+----------+-------------+
| 1 | SIMPLE | group | NULL | ref | division_id | division_id | 4 | const | 3 | 100.00 | Using index |
| 1 | SIMPLE | user_group | NULL | ref | group_id | group_id | 4 | group.group_id | 5 | 100.00 | NULL |
+----+-------------+------------+------------+------+---------------+-------------+---------+-------------------------------+------+----------+-------------+
EXPLAIN
SELECT user_id
FROM user_group
LEFT JOIN `group` USING (group_id)
WHERE division_id = 2;
+----+-------------+------------+------------+------+---------------------+-------------+---------+-------------------------------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------------+-------------+---------+-------------------------------+------+----------+--------------------------+
| 1 | SIMPLE | group | NULL | ref | PRIMARY,division_id | division_id | 4 | const | 3 | 100.00 | Using where; Using index |
| 1 | SIMPLE | user_group | NULL | ref | group_id | group_id | 4 | group.group_id | 5 | 100.00 | NULL |
+----+-------------+------------+------------+------+---------------------+-------------+---------+-------------------------------+------+----------+--------------------------+
我注意到联接的顺序给了我Using index
(对于第一个查询)或Using where; Using index
(对于第二个)。关于它们的其他所有内容几乎完全相同,并且根据我的数据,它们将始终生成相同的结果集。
问题:为什么第二个查询有额外的Using Where
条件,这是一个好处,有害还是没有后果?