gpt4 book ai didi

mysql - 为什么MySQL不使用索引?

转载 作者:行者123 更新时间:2023-11-29 06:48:44 24 4
gpt4 key购买 nike

我有一张 table :

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 - 为什么MySQL不使用索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16894712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com