我在MySQL中遇到了enum字段的问题。当我使用select只有一个值SELECT * FROM test WHERE t IN('new')一切都很好,但是在多个值的情况下SELECT * FROM test WHERE t IN('new','deleted')MySQL没有完全使用索引,因此这些查询的性能非常低。是否有可能做到这一点?
CREATE TABLE `test` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`t` ENUM('new','active','deleted') NOT NULL,
PRIMARY KEY (`id`),
INDEX `t` (`t`)
)
ENGINE=MyISAM;
INSERT INTO test SET t = 'new';
INSERT INTO test SET t = 'new';
INSERT INTO test SET t = 'active';
INSERT INTO test SET t = 'active';
INSERT INTO test SET t = 'deleted';
INSERT INTO test SET t = 'deleted';
EXPLAIN SELECT * FROM test WHERE t IN ('new');
id|select_type|table|type|possible_keys|key|key_len|ref |rows|Extra
1 |SIMPLE |test |ref |t |t |1 |const|2 |Using index condition
EXPLAIN SELECT * FROM test WHERE t IN ('new','deleted');
id|select_type|table|type|possible_keys|key |key_len|ref |rows|Extra
1 |SIMPLE |test |ALL |t |NULL|NULL |NULL|6 |Using where
答案 0 :(得分:1)
MySQL在第二种情况下没有使用索引的事实并不意味着性能会很差(尽管可能是这样)。索引的区别不是很大,所以查询优化器认为不是扫描索引两次(每个值一次),然后读取每个选定索引的行,只需扫描表格就可以了。直接挑出行。
这样的索引通常需要很多TLC (Tender Loving Care)才能有用。看看here了解更多信息。