-- 注:索引:create_time :Key、 id: Primary Key -- wx_user 6000万条数据
select id from wx_user where create_time>='2022-01-29 00:00:00' and create_time<='2022-01-30 00:00:00' order by id limit 1;
-- 执行耗时60s+
Explain:
select_type
table
type
possible_keys
key
key_len
ref
row
Extra
SIMPLE
wx_user
index
ct
PRIMARY
4
null
273
Using where
猜测
order by id 导致where条件的 create_time索引没有使用而使用了主键扫描
解决方案 修改 order by 使用where 条件
select id from wx_user where create_time>='2022-01-29 00:00:00' and create_time<='2022-01-30 00:00:00' order by create_time limit 1;