MySQL: 'EXPLAIN' 对类似查询显示不同的解释

标签 mysql sql key explain

我在数据库中有一个“posts”表,它在 user_id 上有非唯一索引(键:MUL)。

mysql> show columns from posts;
+---------+--------------+------+-----+-------------------+----------------+
| Field   | Type         | Null | Key | Default           | Extra          |
+---------+--------------+------+-----+-------------------+----------------+
| id      | int(11)      | NO   | PRI | NULL              | auto_increment |
| user_id | int(11)      | YES  | MUL | NULL              |                |
| post    | varchar(140) | NO   |     | NULL              |                |
+---------+--------------+------+-----+-------------------+----------------+

对于此表,explain 给出预期的解释,其中类型为“REF

mysql> explain select * from posts where posts.user_id=1;
+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key     | key_len | ref   | rows | Extra       |
+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
|  1 | SIMPLE      | posts | ref  | user_id       | user_id | 5       | const |   74 | Using where |
+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+

我有第二个表“followers”,其中“user_id”和“follower”是非唯一索引的一部分

mysql> show columns from followers;
+---------------+-----------+------+-----+---------------------+----------------+
| Field         | Type      | Null | Key | Default             | Extra          |
+---------------+-----------+------+-----+---------------------+----------------+
| id            | int(11)   | NO   | PRI | NULL                | auto_increment |
| user_id       | int(11)   | YES  | MUL | NULL                |                |
| follower      | int(11)   | YES  | MUL | NULL                |                |
+---------------+-----------+------+-----+---------------------+----------------+

但是在这个表中,类型是'ALL'。我希望它是 'REF' 与上表中的 'user_id' 类似,这个 'user_id' 也有非唯一索引。对此有什么解释吗?

mysql> explain select * from followers where followers.user_id=1;
+----+-------------+-----------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table     | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-----------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | followers | ALL  | user_id       | NULL | NULL    | NULL |    6 | Using where |
+----+-------------+-----------+------+---------------+------+---------+------+------+-------------+

最佳答案

我会将其作为答案发布,因为我很确定情况就是如此。

我认为你会有所不同,因为在 followers 表中你有一个来自 user_idfollower 字段的复合键,而不仅仅是一个键在 user_id 上。

因此,索引将用于在 WHERE 子句中同时使用 user_idfollower 的查询。

user_id 字段上添加一个单独的索引,您将得到相同的解释。

关于MySQL: 'EXPLAIN' 对类似查询显示不同的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11882976/

相关文章:

mysql在表中选择唯一用户并根据列对其进行多次计数

php - Codeigniter 中缺少参数让我发疯

mysql - WordPress 查询时间慢

mysql - Mysqli排名语法

azure - ConnectionStrings.Config 引用环境变量或 key 保管库条目?

sql - 查找每个帖子的用户总数 SQL QUERY

python - SQLAlchemy:在查询对象上调用 list() 时会发生什么?

sql - 列在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中

mysql - 外键等于 Null

python - 从 Python 字典中的 key 中删除元素