I have a table with several million records in MySQL in the MyISAM table. Very simplified like this:
CREATE TABLE `test` ( `text` varchar(5) DEFAULT NULL, `number` int(5) DEFAULT NULL, KEY `number` (`number`) USING BTREE, KEY `text_number` (`text`,`number`) USING BTREE ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
It is populated with this data:
INSERT INTO `test` VALUES ('abcd', '1'); INSERT INTO `test` VALUES ('abcd', '2'); INSERT INTO `test` VALUES ('abcd', '3'); INSERT INTO `test` VALUES ('abcd', '4'); INSERT INTO `test` VALUES ('bbbb', '1'); INSERT INTO `test` VALUES ('bbbb', '2'); INSERT INTO `test` VALUES ('bbbb', '3');
When I run the following query:
EXPLAIN SELECT * FROM `test` WHERE (`text` = 'bbbb' AND `number` = 2)
It returns a "number" as a key to use. But the following request:
EXPLAIN SELECT * FROM `test` WHERE (`text` = 'bbbb' AND `number` = 1)
It returns "text_number" as the key to use, which will make more sense to me, since this combined key exactly matches the two columns in WHERE. On this number of records, performance is not a problem, but on several million records, a query that uses the text index takes 4 seconds, and one that uses the text_number index is completed in a few milliseconds.
Is there a logical explanation for this? How can I change the index that MySQL uses for the index? I know I can use USE INDEX, but I want MySQL to be able to find a better plan to execute the query. This is on MySQL 5.1 and 5.5, the same results.