我有一個表:爲什麼MySQL不使用索引?
CREATE TABLE `ais`.`last_location` (
`timestamp` timestamp NOT NULL default '0000-00-00 00:00:00',
`message_type` tinyint(4) NOT NULL default '0',
`repeat_indicator` tinyint(4) NOT NULL default '0',
`mmsi` int(11) NOT NULL default '0',
`navigation_status` tinyint(4) NOT NULL default '0',
`rot` tinyint(4) NOT NULL default '0',
`sog` smallint(6) NOT NULL default '0',
`position_accuracy` tinyint(4) NOT NULL default '0',
`longitude` int(11) NOT NULL default '0',
`latitude` int(11) NOT NULL default '0',
`cog` smallint(6) NOT NULL default '0',
`hdg` smallint(6) NOT NULL default '0',
`time_stamp` tinyint(4) NOT NULL default '0',
`maneuver_indicator` tinyint(4) NOT NULL default '0',
`spare` tinyint(4) NOT NULL default '0',
`raim_flag` tinyint(4) NOT NULL default '0',
`sotdma_sync_state` tinyint(4) NOT NULL default '0',
`sotdma_slot_timeout` tinyint(4) NOT NULL default '0',
`sotdma_slot_offset` smallint(6) NOT NULL default '0',
PRIMARY KEY USING BTREE (`mmsi`),
KEY `Index_2` (`timestamp`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
我要查詢解釋:
EXPLAIN SELECT *
FROM last_location
WHERE `timestamp` BETWEEN '2013-01-01 12:00:00' AND '2013-06-03 11:30:00'
1, 'SIMPLE', 'last_location', 'range', 'Index_2', 'Index_2', '4', '', 83, 'Using where'
EXPLAIN SELECT *
FROM last_location
WHERE `timestamp` BETWEEN '2013-01-01 12:00:00' AND '2013-06-03 11:40:00'
1, 'SIMPLE', 'last_location', 'ALL', 'Index_2', '', '', '', 478, 'Using where'
誰能告訴我搜索到11:30和11:40之間的區別?當使用11:30時,一切都看起來沒問題,我想?但是在使用11:40時它不再使用索引了?
推測mysql根據它在表上的統計數據計算出它在這種情況下不值得使用索引。執行時間有重大差異嗎? – nos
[MySql中的BETWEEN操作索引]的可能的重複(http://stackoverflow.com/questions/2175163/index-for-between-operation-in-mysql) – Vyktor
表中有多少行? – ankurtr