当前位置:网站首页>mysql分页查询实例_mysql分页查询实例讲解「建议收藏」
mysql分页查询实例_mysql分页查询实例讲解「建议收藏」
2022-07-27 11:35:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
LIMIT子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1)。下面,我们针对特例对mysql分页查询进行总结。
mysql提供分页的功能:SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
最简单的用法就是:select * from table limit ?,?
这种是最简单的limit分页查询。配合where条件使用:select * from table where column > ? order by id limit ?,?
上边这种情况,建议在column和id建立复合索引比较好。
以上两种情况,对于小数据量分页查询时,这样的sql就足够用了。但是对于百万级以上的数据表,如果使用上边的sql 的话,越往后limit语句的偏移量越来越大,查询就会变得越来越慢。类似于:select * from `user` where `cate`=’陕西’ order by id limit 100000,10
为了避免这种查询,我们可以通过子查询的方式来提高查询效率。select * from `user` where id >=(select * from `user` where `cate`=’陕西’ order by id limit 100000,1) and `cate`=’陕西’ limit 10
通过explain我们可以看出直接limit查询和通过子查询的差距:直接limit查询:typepossible_keyskeykey_lenrefrowsExtraALL(NULL)(NULL)(NULL)(NULL)4076607子查询分页查询:typepossible_keyskeykey_lenrefrowsExtraPRIMARYrangePRIMARYPRIMARY42038331Using where
SUBQUERYindex(NULL)PRIMARY44076663Using index
可以看出,通过子查询的方式,子查询是在索引上进行的,而普通的查询是在数据文件上进行的。 通常来说,索引文件要比数据文件小的多,所以操作索引文件更直接高效。
此外,还可以通过join分页方式SELECT * FROM `user` AS t1
JOIN (SELECT id FROM `user` ORDER BY id LIMIT 100000, 1) AS t2
WHERE t1.id <= t2.id ORDER BY t1.id LIMIT 10;
join分页和子查询分页的效率基本在一个等级上。(但是,子查询需要在内存中建立临时表,查询完毕后,MySQL需要撤销这些临时表。通过join可以避免这种情况)在分页查询前,可以进行判断,如果是在限定页数内,就使用基本分页查询,大于则使用子查询分页处理。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/128200.html原文链接:https://javaforall.cn
边栏推荐
- zabbix自定义监控项
- PWM的原理和PWM波的产生
- go语言之sync.Map
- Shell script text three swordsman awk
- Pytorch shows the summary like tensorflow
- Shell脚本文本三剑客之awk
- Principle of control system based on feedback rate
- Difference between verification and calibration
- Analysis of the use of JUC framework from runnable to callable to futuretask
- A possibility that ch340 module cannot be recognized / burned
猜你喜欢

Vscode removes style / syntax highlighting / code highlighting / black background when copying code

LNMP架构搭建(部署Discuz论坛)

美现首例孕妇猴痘病例:新生儿被注射免疫球蛋白,已安全出生

TapNet: Multivariate Time Series Classification with Attentional Prototypical Network

Shell script text three swordsmen sed

解决方案:idea project没有显示树状图

Finding the finite zero point of transfer function under different sampling periods
![[untitled] multimodal model clip](/img/f0/8ae72ae0845372b6fe2866fae83f52.png)
[untitled] multimodal model clip

Newton-Raphson迭代法

Matlab draws Bode diagram with time delay system
随机推荐
Difference quotient approximation of wechat quotient
暂用 Solo,博客选择困难
微商的差商近似
N ¨UWA: Visual Synthesis Pre-training for Neural visUal World creAtionChenfei
ZABBIX custom monitoring items
[machine learning whiteboard derivation series] learning notes - support vector machine and principal component analysis
LAN SDN technology hard core insider 13 from LAN to Internet
Arduino常见供电问题与解决
STM32编译出现error: L6235E: More than one section matches selector - cannot all be FIRST/L
Sword finger offer note: T39. Numbers that appear more than half of the time in the array
JS-寄生组合式继承
LeetCode 01: T1. 两数之和 ; T1108. IP 地址无效化 ; T344. 反转字符串
MySQL数据库主从复制集群原理概念以及搭建流程
A possibility that ch340 module cannot be recognized / burned
【机器学习-白板推导系列】学习笔记---条件随机场
Beyond Compare 3 下一个差异段/向下搜索箭头 找不到了
JUC框架 从Runnable到Callable到FutureTask 使用浅析
Sword finger offer note: t45. arrange the array into the smallest number
Proteus8专业版破解后用数码管闪退的解决
LeetCode 03: T58. 最后一个单词的长度(简单); 剑指 Offer 05. 替换空格(简单); 剑指 Offer 58 - II. 左旋转字符串(简单)