我对 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 有时可以与索引配合使用。
感谢您提前提出的所有评论!
干杯!
雅库布
最佳答案
该索引的注释字段表示该索引已禁用
,但不确定原因。
ALTER TABLE ... ENABLE KEYS 可能有效
关于mysql - 简单的 mySQL 查询不使用索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6613098/