db42
db42

Reputation: 4544

MySQL: 'EXPLAIN' showing different explanations for similar queries

I have a table 'posts' in database which has non-unique index on user_id (Key: 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              |                |
+---------+--------------+------+-----+-------------------+----------------+

For this table, explain gives expected explanation where type is '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 |
+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+

I have a second table 'followers' where 'user_id' and 'follower' are part of non-unique index

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                |                |
+---------------+-----------+------+-----+---------------------+----------------+

But in this table, type is 'ALL'. I expected it to be 'REF' as similar to 'user_id' in previous table, this 'user_id' also has non-unique index. Is there any explanation for this?

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 |
+----+-------------+-----------+------+---------------+------+---------+------+------+-------------+

Upvotes: 1

Views: 134

Answers (1)

favoretti
favoretti

Reputation: 30167

I'll post it as an answer, cause I'm pretty sure this is the case.

I think you get differences because in followers table you have a composite key from both user_id and follower fields, rather than just a key on user_id.

Therefore index will be used for queries that use both user_id AND follower in WHERE clause.

Add a separate index on user_id field and you will get the same explanation.

Upvotes: 2

Related Questions