CREATE TABLE `optimizer_test` (
`no` int(11) NOT NULL AUTO_INCREMENT,
`datetime` varchar(30) CHARACTER SET latin1 DEFAULT NULL,
PRIMARY KEY (`no`),
KEY `DATETIME` (`datetime`)
) ENGINE=InnoDB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8;
mysql> explain select no,datetime FROM optimizer_test WHERE datetime between '2014-09-24 00:00:00' and '2014-10-01 23:59:59' ORDER BY datetime DESC;
+----+-------------+----------------+-------+---------------+----------+---------+------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+-------+---------------+----------+---------+------+------+--------------------------+
| 1 | SIMPLE | optimizer_test | index | DATETIME | DATETIME | 33 | NULL | 1 | Using where; Using index |
+----+-------------+----------------+-------+---------------+----------+---------+------+------+--------------------------+
mysql> ALTER TABLE optimizer_test ADD INDEX datetimedesc(DATETIME(23) DESC);
mysql> explain select no,datetime FROM optimizer_test WHERE datetime between '2014-09-24 00:00:00' and '2014-10-01 23:59:59' ORDER BY datetime DESC;
+----+-------------+----------------+-------+-----------------------+----------+---------+------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+-------+-----------------------+----------+---------+------+------+--------------------------+
| 1 | SIMPLE | optimizer_test | index | DATETIME,datetimedesc | DATETIME | 33 | NULL | 1 | Using where; Using index |
+----+-------------+----------------+-------+-----------------------+----------+---------+------+------+--------------------------+