简单的mySQL查询不使用索引

时间:2011-07-07 15:29:31

标签: mysql database-design myisam

我对mySQL和简单索引的简单查询有一个非常奇怪的问题。我正在尝试使用此查询:

SELECT * FROM `counter_links` WHERE `link_id`=1544;

如您所见,还有一个名为link_id的索引:

mysql> show indexes from counter_links;
+---------------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+----------+---------------+
| Table         | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment  | Index_comment |
+---------------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+----------+---------------+
| counter_links |          1 | link_id     |            1 | link_id     | A         |        NULL |     NULL | NULL   |      | BTREE      | disabled |               |
+---------------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+----------+---------------+
1 row in set (0.12 sec)

但看看EXPLAIN返回了什么:

mysql> explain SELECT * FROM `counter_links` WHERE `link_id`=1544;
+----+-------------+---------------+------+---------------+------+---------+------+----------+-------------+
| id | select_type | table         | type | possible_keys | key  | key_len | ref  | rows     | Extra       |
+----+-------------+---------------+------+---------------+------+---------+------+----------+-------------+
|  1 | SIMPLE      | counter_links | ALL  | NULL          | NULL | NULL    | NULL | 22103687 | Using where |
+----+-------------+---------------+------+---------------+------+---------+------+----------+-------------+
1 row in set (0.03 sec)

为什么mySQL不在那里使用索引?我看到同样在较小版本的表上运行良好,但我无法识别出问题所在。你呢?

看起来更奇怪,因为较低的ID有时会与索引一起使用。

提前感谢您的所有意见!

干杯!

的Jakub

1 个答案:

答案 0 :(得分:3)

eindex的评论字段表示索引为disabled,不确定原因。

ALTER TABLE ... ENABLE KEYS可能有效