在MySQL JOIN中使用ON或USING或WHERE是否更具性能?

时间:2018-07-02 13:55:08

标签: mysql join

假设我有一个foobar表,其中两个id作为主键。

以下查询:

SELECT * 
FROM foo 
JOIN bar
WHERE foo.id = bar.id
AND foo.id = :id

与以下查询一样具有表现力:

SELECT * 
FROM foo 
JOIN bar ON foo.id = bar.id
AND foo.id = :id

或者:

SELECT * 
FROM foo 
JOIN bar USING (id)
AND foo.id = :id

EXPLAINS告诉我所有这些查询都是同义词,但是我想知道使用多个索引时是否存在边缘情况?我没有找到关于该主题的任何明确文档。

附件:EXPLAINS个结果。

mysql> EXPLAIN
    ->     SELECT *
    ->     FROM foo
    ->     JOIN bar
    ->     WHERE foo.id = bar.id
    -> ;
+----+-------------+-------+------------+--------+---------------+---------+---------+-------------+------+----------+-------------+
| id | select_type | table | partitions | type   | possible_keys | key     | key_len | ref         | rows | filtered | Extra       |
+----+-------------+-------+------------+--------+---------------+---------+---------+-------------+------+----------+-------------+
|  1 | SIMPLE      | foo   | NULL       | index  | PRIMARY       | PRIMARY | 4       | NULL        |  100 |   100.00 | Using index |
|  1 | SIMPLE      | bar   | NULL       | eq_ref | PRIMARY       | PRIMARY | 4       | test.foo.id |    1 |   100.00 | Using index |
+----+-------------+-------+------------+--------+---------------+---------+---------+-------------+------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)

mysql> EXPLAIN
    ->     SELECT *
    ->     FROM foo
    ->     JOIN bar USING (id)
    -> ;
+----+-------------+-------+------------+--------+---------------+---------+---------+-------------+------+----------+-------------+
| id | select_type | table | partitions | type   | possible_keys | key     | key_len | ref         | rows | filtered | Extra       |
+----+-------------+-------+------------+--------+---------------+---------+---------+-------------+------+----------+-------------+
|  1 | SIMPLE      | foo   | NULL       | index  | PRIMARY       | PRIMARY | 4       | NULL        |  100 |   100.00 | Using index |
|  1 | SIMPLE      | bar   | NULL       | eq_ref | PRIMARY       | PRIMARY | 4       | test.foo.id |    1 |   100.00 | Using index |
+----+-------------+-------+------------+--------+---------------+---------+---------+-------------+------+----------+-------------+
2 rows in set, 1 warning (0.01 sec)

mysql> EXPLAIN
    ->     SELECT *
    ->     FROM foo
    ->     JOIN bar ON foo.id = bar.id
    -> ;
+----+-------------+-------+------------+--------+---------------+---------+---------+-------------+------+----------+-------------+
| id | select_type | table | partitions | type   | possible_keys | key     | key_len | ref         | rows | filtered | Extra       |
+----+-------------+-------+------------+--------+---------------+---------+---------+-------------+------+----------+-------------+
|  1 | SIMPLE      | foo   | NULL       | index  | PRIMARY       | PRIMARY | 4       | NULL        |  100 |   100.00 | Using index |
|  1 | SIMPLE      | bar   | NULL       | eq_ref | PRIMARY       | PRIMARY | 4       | test.foo.id |    1 |   100.00 | Using index |
+----+-------------+-------+------------+--------+---------------+---------+---------+-------------+------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)

1 个答案:

答案 0 :(得分:0)

1)对于子句USING,您必须使用两个表中都存在且名称相同的字段。
但是在ON子句中,您可以输入以下内容:

FROM foo JOIN bar ON foo.id = bar.foo_id

2)如果您需要使用特定的索引,请根据您的索引指定字段(适用于usingon)。

3)如果您使用mysql-您可以使用或USING两者ON都可以正常工作(没有人拒绝任何此条款)。