我有以下查询:
select min(a) from tbl where b > ?;
在我的 mysql 实例上使用 index(b, a)
(15M 行)大约需要 4 秒。有没有办法加快速度?
解释:
explain select min(parsed_id) from replays where game_date > '2016-10-01';
id: 1
select_type: SIMPLE
table: replays
partitions: NULL
type: range
possible_keys: replays_game_date_index,replays_game_date_parsed_id_index
key: replays_game_date_parsed_id_index
key_len: 6
ref: NULL
rows: 6854021
filtered: 100.00
Extra: Using where; Using index
索引声明:
create index replays_game_date_parsed_id_index on replays (game_date, parsed_id);
最佳答案
我认为 MySQL 使用的索引是正确的。查询应该是即时的,因为从索引中读取一次应该返回您想要的结果。我猜想对于这个查询,MySQL 的 SQL 优化器做得很差。
也许您可以重新措辞您的查询来欺骗 SQL 优化器使用不同的策略。也许你可以尝试:
select parsed_id
from replays
where game_date > '2016-10-01'
order by parsed_id
limit 1
关于mysql - 如何加快此 SQL 查询速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53874400/