MYSQL关注者系统:索引关注者表

时间:2014-10-19 03:10:06

标签: php mysql indexing

我正在开发一款应用程序,其中包含以下内容,例如Twitter& Instagram的。我有关于索引关注表的问题。以我的两个MYSQL表为例。

users
- user_id
- username
...

followers 
- id
- user_id
- follower_id
...
index(user_id,follower_id)

我一直在阅读有关索引的内容:http://www.slideshare.net/billkarwin/how-to-design-indexes-really。 根据我的理解,索引按指定索引中最左边的列排序。那么在这种情况下,它的user_id和follower_id分散在整个表中?

*followers Table
id    user_id    follower_id
————————————————————--------
1        5          6
2        5          8
3        5          11
4        7          6

因此,如果我运行下面的简单查询,它应该是有效的,并允许我检索x用户关注的人:

SELECT * FROM followers WHERE user_id=5

然而,如果我需要让跟随某人的人,会发生什么?例如:

SELECT * FROM followers WHERE follower_id = 6

根据我的理解,follower_id只会在user_id列之后按顺序排序。因此,整个表格都会出现,必须检查整个表格吗?

处理这个问题的最佳方法是什么?我误解了什么?

1 个答案:

答案 0 :(得分:1)

请参阅here

从你的例子来看,这一点很突出:

  

MySQL使用索引进行这些操作:

     

快速查找与WHERE子句匹配的行。

我会使用正常索引类型索引user_idfollower_id

另外,请务必阅读多列索引,因为它可以为您的脚本提供方便。