即使查询包含索引提示,MySQL也不使用索引

时间:2015-12-26 06:03:17

标签: mysql indexing

  • MySQL 5.6

考虑下表:

create table mytbl (
    id long primary key,
    digest varchar(32)
);

digest列包含MD5摘要,我在digest上创建了索引:

create fulltext index digest on mytbl (digest);

我随后检查了一个查询:

explain select count(id) from mytbl where digest = 'abcde12345ffhhg';  -- a digest in the table

计划是(表格有1000行):

id | select_type | table | type | possible_keys | key | key_len | ref | rows | extra
1  | SIMPLE      | mytbl | ALL  | digest        | NULL| NULL    | NULL| 1000 | Using where

然后我调用了查询

explain select count(id) from mytbl use index (digest) where digest = 'abcde12345ffhhg';

但计划保持不变,即未使用索引digest

为什么MySQL会忽略digest上设置的索引?

修改

我认为我对[fulltext | spatial | unique]声明中的选项CREATE INDEX存在误解。我删除了fulltext index,然后创建了一个默认索引(create index digest ...)。因此,MySQL在查询计划中使用了digest索引。

1 个答案:

答案 0 :(得分:1)

此查询忽略全文索引,因为它没有解释全文搜索的执行计划。

要解释全文搜索并使用索引,请使用MATCH()...AGAINST()

EXPLAIN SELECT count(id) FROM mytbl WHERE MATCH(digest) AGAINST ('abcde12345ffhhg');