当前位置: 首页 > news >正文

优化带排序的分页查询

优化带排序的分页查询
浅分页:
select user_no,user_name,socre from student order by score desc limit 5,20
深分页:
select user_no,user_name,socre from student order by score desc limit 80000,20
因为偏移量深分页更大,所以深分页执行时间更长

优化1:alter table student add index_socre(score);
浅分页走了索引,深分页没有走索引:
select user_no,user_name,socre from student force index(index_socre) order by score desc limit 5,20
走强制索引的话执行时间,比不走还慢。
因为sql除了查询score之外还查询了user_no,user_name,需要回表查询
回表需要时间,排序也需要时间,mysql 帮我们做了优化,两者取最优。

优化2:添加联合索引,就不用回表了  alter table student add index_socre_no_name(user_no,user_name,score);
深分页走了联合索引,extra:using index 走了覆盖索引
缺点:增加了查询字段就不行了,索引就失效了

优化3:删除联合索引,只留下 score的索引
Select user_no,user_name,socre from student a join (select id from student order by socre desc limit 80000,20
) b on a.id = b.id
缺点:子查询的id 集合比较多的话(这里是20个),不建议这样使用

优化4:只留下 score的索引,分数一样的情况下,id递增


Select user_no,user_name,socre from student where id<上一次最大的 and score < 上一次最大的 order by score desc limit 80000,20 

索引结构:

 

http://www.lryc.cn/news/90506.html

相关文章:

  • chatgpt赋能python:Python如何删除空白
  • [论文阅读] Explicit Visual Prompting for Low-Level Structure Segmentations
  • swagger在spring项目中的使用
  • 操作系统第五章——输入输出管理(中)
  • 【网络】socket套接字基础知识
  • Go语言介绍以及Go语言环境安装
  • FPGA纯verilog实现CameraLink视频接收和发送,附带工程源码和技术支持
  • k8s中的service、api-server、kube-proxy有什么区别
  • 记录::opencv编译,cmake编译vs动态库
  • 网易SmartAuto,中文编程就是爽
  • 适配器模式那么强大,该怎么使用呢?
  • [极客大挑战 2019]PHP1
  • PID 学习
  • 002. java.lang.NumberFormatException: Infinite or NaN,怎么破?
  • Vue常用的修饰符
  • freertos笔记-任务切换
  • 企业电子招投标采购系统源码之登录页面-java spring cloud
  • 接口测试|Fiddler弱网测试
  • Linux-0.11 文件系统super.c详解
  • 什么是ChatGPT、历史发展及应用领域
  • Spring的创建与使用
  • 抖音Flutter插件的使用
  • Debezium报错处理系列之六十八:No resolvable bootstrap urls given in bootstrap.servers
  • Python二级编程:分词去重
  • Android Wifi开发——Wifi锁(十九)
  • Nginx的优化与防盗链
  • STP协议
  • 方法——检查参数的有效性
  • 七、Docker仓库之nexus搭建(四)
  • MySQL 锁机制