I ve got this table:
CREATE TABLE IF NOT EXISTS `test1_nopart` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`idAccount` int(10) unsigned NOT NULL,
`data` mediumint(8) unsigned NOT NULL,
`date` date NOT NULL,
PRIMARY KEY (`id`),
KEY `date` (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I fill this table with 10 000 000 rows. The repartition by date is homogenous
EXPLAIN SELECT * FROM `test1_nopart` WHERE date = "2014-03-04"
Here is the result
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE test1_nopart ALL NULL NULL NULL NULL 7875981 Using where
=> without the explain 3.6sec for a result of 3000 rows (approx)
As you can see the index is not used and it is not part of the possible_keys column!
the same request with a covering index way
EXPLAIN SELECT date FROM `test1_nopart` WHERE date = "2014-03-04"
the result:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE test1_nopart index NULL date 3 NULL 7875981 Using where; Using index
=> without the explain 2.8sec for a result of 3000 rows (approx)
Why MySQL doesn't use properly this index(DATE) ???
Info: - VM Server (our dev environment, I don't know what is the hardware composition) - MySQL 5.5.8
SHOW INDEX FROM test1_nopart
result:
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
test1_nopart 0 PRIMARY 1 id A 7875981 NULL NULL BTREE
test1_nopart 1 date 1 date A 6077 NULL NULL BTREE
- For date 2014-03-04 => 3134 rows
- The total (rollup) => 7 875 488
- There is 2556 differents 'date' values in the table