I can't figure out why an datetime index is ignored by mysql if the greater than operator is used.
Here's the table:
CREATE TABLE IF NOT EXISTS `maverick_player_stints` ( `unique_id` int(10) unsigned NOT NULL default '0', `player_id` mediumint(9) NOT NULL default '0', `start_stint` datetime NOT NULL default '0000-00-00 00:00:00', `end_stint` datetime NOT NULL default '0000-00-00 00:00:00', `at_position` tinyint(3) unsigned NOT NULL default '0', `on_team` tinyint(3) unsigned NOT NULL default '0', PRIMARY KEY (`unique_id`), KEY `player_id~stint` (`player_id`,`start_stint`), KEY `stint_starts` (`start_stint`)) ENGINE=InnoDB DEFAULT CHARSET=latin1
Here's the first query explained:
explain select * from maverick_player_stints where start_stint = '2007-01-22 09:00:00';
+----+-------------+------------------------+------+---------------+--------------+---------+-------+------+-------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+------------------------+------+---------------+--------------+---------+-------+------+-------+| 1 | SIMPLE | maverick_player_stints | ref | stint_starts | stint_starts | 8 | const | 2 | | +----+-------------+------------------------+------+---------------+--------------+---------+-------+------+-------+
Here's the same query with the greater than operator:
explain select * from maverick_player_stints where start_stint > '2007-01-22 09:00:00';
+----+-------------+------------------------+------+---------------+------+---------+------+-------+-------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+------------------------+------+---------------+------+---------+------+-------+-------------+| 1 | SIMPLE | maverick_player_stints | ALL | stint_starts | NULL | NULL | NULL | 15401 | Using where | +----+-------------+------------------------+------+---------------+------+---------+------+-------+-------------+
Why is the possible key not used in the second case? It's scanning the entire table. Is this something particular to the datetime data type?
I could use some help on this one, thanks.
Here's the table:
CREATE TABLE IF NOT EXISTS `maverick_player_stints` ( `unique_id` int(10) unsigned NOT NULL default '0', `player_id` mediumint(9) NOT NULL default '0', `start_stint` datetime NOT NULL default '0000-00-00 00:00:00', `end_stint` datetime NOT NULL default '0000-00-00 00:00:00', `at_position` tinyint(3) unsigned NOT NULL default '0', `on_team` tinyint(3) unsigned NOT NULL default '0', PRIMARY KEY (`unique_id`), KEY `player_id~stint` (`player_id`,`start_stint`), KEY `stint_starts` (`start_stint`)) ENGINE=InnoDB DEFAULT CHARSET=latin1
Here's the first query explained:
explain select * from maverick_player_stints where start_stint = '2007-01-22 09:00:00';
+----+-------------+------------------------+------+---------------+--------------+---------+-------+------+-------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+------------------------+------+---------------+--------------+---------+-------+------+-------+| 1 | SIMPLE | maverick_player_stints | ref | stint_starts | stint_starts | 8 | const | 2 | | +----+-------------+------------------------+------+---------------+--------------+---------+-------+------+-------+
Here's the same query with the greater than operator:
explain select * from maverick_player_stints where start_stint > '2007-01-22 09:00:00';
+----+-------------+------------------------+------+---------------+------+---------+------+-------+-------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+------------------------+------+---------------+------+---------+------+-------+-------------+| 1 | SIMPLE | maverick_player_stints | ALL | stint_starts | NULL | NULL | NULL | 15401 | Using where | +----+-------------+------------------------+------+---------------+------+---------+------+-------+-------------+
Why is the possible key not used in the second case? It's scanning the entire table. Is this something particular to the datetime data type?
I could use some help on this one, thanks.
Comment