我正在开发一款包含关注者的应用程序,就像 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 列之后按顺序排序。因此,它发生在整个表中,并且必须检查整个表?
处理这个问题的最佳方法是什么?我误解了什么?
最佳答案
参见here .
从您的示例中,这一点很突出:
MySQL uses indexes for these operations:
To find the rows matching a WHERE clause quickly.
我会使用普通索引类型对 user_id
和 follower_id
进行索引。
此外,请务必阅读多列索引,因为它可以为您的脚本派上用场。
关于php - MYSQL关注者系统: Indexing the Followers Table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26446908/