mysql的索引使用问题
表的结构是这样的
CREATE TABLE `abc` (
`log_date` date NOT NULL ,
`pId` int(11) NOT NULL,
`sid` int(11) unsigned NOT NULL
KEY `sid` (`sid`,`log_date`,`pId`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
explain select * from abc where
sid=123 and
log_date=date_sub(NOW(),INTERVAL 3
MONTH);
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE noticed after reading const tables |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
不知道为何会出现这种情况,没使上索引,若改成
explain select * from abc where
sid=123 and
log_date<date_sub(NOW(),INTERVAL 3
MONTH);
就可以了
+----+-------------+----------------+-------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+-------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | challengeAward | range | sid | sid | 7 | NULL | 1 | Using where |
+----+-------------+----------------+-------+---------------+------+---------+------+------+-------------+