在MySQL中使用内连接表上的索引

时间:2013-06-13 09:18:43

标签: mysql indexing inner-join large-data

我的表Foo有2亿条记录,表格条有1000条记录,它们是多对一连接的。列Foo.someTime和Bar.someField有索引。同样在Bar 900中,记录的某些字段为1,100,其中某些字段为2。

(1)此查询立即执行:

mysql> select * from Foo f inner join Bar b on f.table_id = b.table_id where f.someTime     between '2008-08-14' and '2018-08-14' and b.someField = 1 limit 20;
...
20 rows in set (0.00 sec)

(2)这个只需要永远(唯一的变化是b.someField = 2):

mysql> select * from Foo f inner join Bar b on f.table_id = b.table_id where f.someTime     between '2008-08-14' and '2018-08-14' and b.someField = 2 limit 20;

(3)但是如果我在某个时间删除where子句而不是立即执行:

mysql> select * from Foo f inner join Bar b on f.table_id = b.table_id where b.someField = 2 limit 20;
...
20 rows in set (0.00 sec)

(4)我也可以通过强制索引使用来加快速度:

mysql> select * from Foo f inner join Bar b force index(someField) on f.table_id = b.table_id where f.someTime     between '2008-08-14' and '2018-08-14' and b.someField = 2 limit 20;
...
20 rows in set (0.00 sec)

这是关于查询(2)(永远需要)的解释

+----+-------------+-------+--------+-------------------------------+-----------+---------+--------------------------+----------+-------------+
| id | select_type | table | type   | possible_keys                 | key       | key_len | ref                      | rows     | Extra       |
+----+-------------+-------+--------+-------------------------------+-----------+---------+--------------------------+----------+-------------+
|  1 | SIMPLE      | g     | range  | bar_id,bar_id_2,someTime      | someTime  | 4       | NULL                     | 95022220 | Using where |
|  1 | SIMPLE      | t     | eq_ref | PRIMARY,someField,bar_id      | PRIMARY   | 4       | db.f.bar_id              |        1 | Using where |
+----+-------------+-------+--------+-------------------------------+-----------+---------+--------------------------+----------+-------------+

这是(4)(有力量指数)的解释

+----+-------------+-------+------+-------------------------------+-----------+---------+--------------------------+----------+-------------+
| id | select_type | table | type | possible_keys                 | key       | key_len | ref                      | rows     | Extra       |
+----+-------------+-------+------+-------------------------------+-----------+---------+--------------------------+----------+-------------+
|  1 | SIMPLE      | t     | ref  | someField                     | someField | 1       |   const                  |       92 |             |
|  1 | SIMPLE      | g     | ref  | bar_id,bar_id_2,someTime      | bar_id    | 4       | db.f.foo_id              | 10558024 | Using where |
+----+-------------+-------+------+-------------------------------+-----------+---------+--------------------------+----------+-------------+

所以问题是如何教MySQL使用正确的索引?查询由ORM生成,并不仅限于这两个字段。而且避免更改查询会很好(尽管我不确定内连接是否适合这里)。

更新

mysql> create index index_name on Foo (bar_id, someTime);

之后,查询(2)以0.00秒执行。

1 个答案:

答案 0 :(得分:5)

如果为foo(table_id, sometime)创建复合索引,它应该会有很大帮助。这是因为服务器可以先将结果集缩小table_id,然后再缩小sometime

请注意,使用LIMIT时,如果许多行符合您的WHERE约束,则服务器不保证将获取哪些行。从技术上讲,每次执行都会给你带来稍微不同的结果。如果您想避免含糊不清,则在使用ORDER BY时应始终使用LIMIT。但是,这也意味着您应该更加谨慎地创建适当的索引。