1

I've got a problem with enum field in MySQL. When i use select with only one value SELECT * FROM test WHERE t IN ('new') everything is fine, but in case of several values SELECT * FROM test WHERE t IN ('new','deleted') MySQL doesn't use indexes at all, so perfomance of this queries is very low. Is it possible to do something with that ?

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

1 Answer 1

1

The fact MySQL hasn't used the index in the second case doesn't mean the performance will be poor (although it might be). The differentiation on the index isn't very great, so the query optimiser takes the view that rather than scan the index twice (once for each value) and then read the rows for each selected index, it is quicker just to scan the table and pick out the rows directly.

Indexes like this will typically need a lot of TLC (Tender Loving Care) if they are to be useful. Have a look here for more.

Sign up to request clarification or add additional context in comments.

2 Comments

What does TLC mean? All I can find is 'Transactions, Locking and Concurrency' but that doesn't seem to make sense in this context.
@MarkAmery, Tender Loving Care. The index has poor differentiation, so rebalancing will probably be necessary from time to time (the primary key would take care of itself).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.