gpt4 book ai didi

MySQL 不使用索引进行 BETWEEN 查询。我究竟做错了什么?

转载 作者:行者123 更新时间:2023-11-29 04:50:08 25 4
gpt4 key购买 nike

我尝试了此查询的不同变体,但我就是无法让它使用时间戳索引。我做错了什么?

任何帮助将不胜感激:)


MariaDB [alienvault]> explain extended SELECT * FROM alienvault_siem.acid_event WHERE (timestamp BETWEEN '2012-10-09 11:20:17' AND '2012-10-10 03:20:17');
+------+-------------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+------+-------------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| 1 | SIMPLE | acid_event | ALL | timestamp | NULL | NULL | NULL | 4481579 | 50.00 | Using where |
+------+-------------+------------+------+---------------+------+---------+------+---------+----------+-------------+
1 row in set, 1 warning (0.01 sec)

表结构:

CREATE TABLE IF NOT EXISTS `acid_event` (
(...)
`timestamp` datetime NOT NULL,
(...)
PRIMARY KEY (`id`),
(...)
KEY `timestamp` (`timestamp`),
(...)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

软件:Percona 服务器

软件版本:5.5.27-28.1 - Percona Server (GPL),28.1 版

最佳答案

可能由以下原因引起: - 索引的选择性不够,mysql 认为全扫描会更快 - mysql 有错误的索引统计信息

您可以检查“SHOW INDEX FROM alienvault_siem.acid_event;”的输出您还可以尝试强制使用索引(但如果全扫描速度更快,则可能会很糟糕):

SELECT * FROM alienvault_siem.acid_event FORCE INDEX (`timestamp`) WHERE (timestamp BETWEEN '2012-10-09 11:20:17' AND '2012-10-10 03:20:17');

关于MySQL 不使用索引进行 BETWEEN 查询。我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12811822/

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