数据库
首页 > 数据库> > MySQL orderby查询效率/索引影响

MySQL orderby查询效率/索引影响

作者:互联网

MySQL Order by 查询效率

遇到问题
 使用 order by 之后查询效率特别慢
sql语句如下:
 -- 注:索引: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_typetabletypepossible_keyskeykey_lenrefrowExtra
SIMPLEwx_userindexctPRIMARY4null273Using 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;   
Explain:
select_typetabletypepossible_keyskeykey_lenrefrowExtra
SIMPLEwx_userrangectct5null208754Using where; Using index

explain 参考文档

https://cloud.tencent.com/developer/article/1176479?from=article.detail.1093229

标签:orderby,00,create,MySQL,索引,time,where,id,select
来源: https://blog.csdn.net/weixin_44088850/article/details/122751364