【问题标题】:MYSQL Followers System: Indexing the Followers TableMYSQL 追随者系统:索引追随者表
【发布时间】:2014-10-19 03:10:06
【问题描述】:

我正在开发一款包含关注的应用,就像 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 列之后按顺序排序。因此,它发生在整个表中并且必须检查整个表?

处理此问题的最佳方法是什么,我有什么误解?

【问题讨论】:

    标签: php mysql indexing


    【解决方案1】:

    here

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

    MySQL 为这些操作使用索引:

    快速找到匹配 WHERE 子句的行。

    我将使用普通索引类型同时索引user_idfollower_id

    另外,请务必阅读多列索引,因为它可以为您的脚本派上用场。

    【讨论】:

      猜你喜欢
      • 2021-06-13
      • 2019-07-03
      • 2017-12-08
      • 1970-01-01
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多