当前位置:网站首页>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
边栏推荐
- 常见JVM面试题及答案整理
- (Crypto essential dry goods) Detailed analysis of the current NFT trading markets
- tf.keras.utils.get_file()
- 梳理一下自己常用的快捷键
- gin框架学习-Casbin进阶之策略管理API使用方法
- Three-party login using wallet Metamask based on web3.0
- NFT:数字所有权的核心
- 第7章 网络层第2次练习题答案(第三版)
- Digital twins will be an important way to enter the "metaverse"
- [Elastic-Job source code analysis] - job listener
猜你喜欢

win11中利用IIS10搭建asp网站

leetcode-每日一题1252. 奇数值单元格的数目(模拟优化)

Why is the redis single-threaded also so fast?

npm WARN config global `--global`, `--local` are deprecated. Use `--location解决方案

详解扫雷游戏(C语言)

10 【组件编码流程 组件自定义事件 全局事件总线】

leetcode-829. 连续整数求和(数论)

Yuan prospect and four track of the universe

带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?

12 【nextTick 过渡与动画】
随机推荐
Swordsman Offer Special Assault Edition ---- Day 6
C language tutorial (3) - if and loop
Oracle数据库中的“limit”查询
What is GameFi?
Fragmented NFT (Fractional NFT)
gin框架学习-JWT认证
闭包(三)----执行环境
什么是EVM兼容链?
数字孪生将成为进入“元宇宙”一项重要的途径
Linux修改MySQL数据库密码
feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
Detailed explanation of pointers in C language
在kali上搭建vulhub漏洞靶场
leetcode-每日一题剑指 Offer II 041. 滑动窗口的平均值(队列模拟)
【Elastic-Job】分布式调度任务概览篇
Redis:简单实用
05 【绑定样式 条件渲染 列表渲染】
MySQL-如何分库分表?一看就懂
Sword Point Offer Special Assault Edition ---- Day 1
【uiautomation】微信好友列表获取(存储到txt中)