考虑一个具有以下模式的MySql表
+-------------------+------------+------+-----+-
| Field | Type | Null | Key |
+-------------------+------------+------+-----+-
| id | int(11) | NO | PRI |
| user_id | int(11) | YES | MUL |
| following_user_id | int(11) | NO | MUL |
+-------------------+------------+------+-----+-现在我需要像这样的查询
select * from table where user_id = <x> and following_user_id = <y>;而且还
select * from table where following_user_id = <x> and user_id = <y>;因此,我考虑在这两列上创建复合索引,如下所示:
index(user_id, following_user_id)和
index(following_user_id, user_id)1)索引是根据需要创建的,但是当记录很多(~数百万)时,它们是否可以工作?
2)索引是否会在正确的时间使用正确的索引来加快查询速度?
PS:我不需要sort/range selection查询,只需要直接匹配查询。有没有更好的索引方案来满足这个要求?
发布于 2013-02-22 02:59:28
从编译器的角度来看,您的查询是相同的。这两个索引都会起作用。对于限定索引查询,where语句中的子句顺序无关紧要。
但是,如果您有不平等或只有一个子句,那么索引中的排序就会有所不同。
因此,索引index(user_id, following_user_id)对于以下情况很有用:
它不会用于:
https://stackoverflow.com/questions/15010170
复制相似问题