当前位置:网站首页>5 methods of MySQL paging query
5 methods of MySQL paging query
2022-07-31 05:52:00 【m0_67391401】
Method 1:
select * from table order by id limit m, n;
It is very simple. The meaning of this statement is to query m+n records, remove the first m records, and return the last n records.There is no doubt that the query can achieve paging, but the larger m, the lower the query performance, because MySQL needs to scan all m+n records.
Method 2:
select * from table where id > #max_id# order by id limit n;
This query will also return the last n records, but it does not need to scan the first m records as in method 1, but must get the maximum id (or minimum id) of the previous query (previous page) in each query,is the more common way.
Of course, the problem with this query is that we may not be able to get this id. For example, if we are currently on page 3 and need to query the data on page 5, it will not work.
Method 3:
In order to avoid cross-page query that cannot be achieved by way 2, it is necessary to combine way 1.
For performance needs, m should be as small as possible.For example, currently on page 3, you need to query page 5, each page has 10 pieces of data, and the current maximum id of page 3 is #max_id#, then:
select * from table where id > #max_id# order by id limit 10, 10;
This method partially solves the problem of method 2, but if it is currently on page 2 and needs to check page 1000, the performance is still poor.
Method 4:
select * from table as a inner join (select id from table order by id limit m, n) as b on a.id = b.id order by a.id;
This query is the same as mode 1, the value of m may be very large, but because the internal subquery only scans the id field instead of the whole table, the performance is stronger than that of mode 1, and it can solve the problem of cross-page query.
Method 5:
select * from table where id > (select id from table order by id limit m, 1) limit n;
This query also scans the field id through a subquery, and the effect is the same as method 4.However, the performance of method 5 is slightly better than that of method 4, because it does not require table association, but a simple comparison, which is a recommended usage without knowing the maximum id of the previous page.
Let me introduce myself first. The editor graduated from Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Ali in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- Redis first meeting
- On the side of Ali, tell me what are the application scenarios of message middleware you know?
- 字符串的扩展
- Linux中mysql密码修改方法(亲测可用)
- MySQL-如何分库分表?一看就懂
- Three-party login using wallet Metamask based on web3.0
- (Crypto essential dry goods) Detailed analysis of the current NFT trading markets
- Volatility取证工具使用日记
- 实验7 UDP与TCP对比
- 年终总结——岁月静好~
猜你喜欢
随机推荐
C language tutorial (3) - if and loop
阿里一面,说说你知道消息中间件的应用场景有哪些?
实验8 DNS解析
leetcode-每日一题745. 前缀和后缀搜索(哈希和字典树)
[Elastic-Job source code analysis] - job listener
tf.keras.utils.pad_sequences()
leetcode-438. 找到字符串中所有字母异位词(滑动窗口)
wpf wrapPanel居中并从左到右排列
数据库上机实验3 连接查询和分组查询
03 【数据代理 事件处理】
gin框架学习-JWT认证
【Elastic-Job源码分析】——作业监听器
gin框架学习-GORM框架进阶之CRUD接口(数据库增删改查操作)
Access数据库的查询
Sword Point Offer Special Assault Edition ---- Day 1
wpf ScrowViewer水平滚动
Memcached :安装
Swordsman Offer Special Assault Edition ---- Day 6
Xiaobai learns reptiles - introduction to reptiles
继承、Super,重写、抽象类、抽象方法 1(第七天)









