当前位置:网站首页>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
边栏推荐
猜你喜欢

Kubernetes certificate validity period modification

leetcode-每日一题735. 行星碰撞(栈模拟)

碎片化NFT(Fractional NFT)

12 【nextTick 过渡与动画】
uni-app进阶之认证【day12】

leetcode-每日一题558. 四叉树交集(分治递归)

feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r

数字取证autopsy工具用法

MySql创建数据表

03 【数据代理 事件处理】
随机推荐
Access数据库的查询
gin框架学习-Casbin入门指南(ACL、RBAC、域内RBAC模型)
Digital twins will be an important way to enter the "metaverse"
uni-app进阶之认证【day12】
数据库上机实验5 数据库安全性
MySQL压缩包方式安装,傻瓜式教学
npm WARN config global `--global`, `--local` are deprecated. Use `--location解决方案
数据库上机实验1 数据库定义语言
11 【定位】
C语言文件读、写、定位函数
How to distinguish big and small endian in C language
踏上编程之路,你必须要干的几件事
Year-end summary - the years are quiet~
Swordsman Offer Special Assault Edition ---- Day 6
mysql password modification method in Linux (pro-test available)
First acquaintance with Flask
gin框架学习-GORM框架进阶之CRUD接口(数据库增删改查操作)
什么是EVM兼容链?
If the account number or password is entered incorrectly for many times, the account will be banned.
uni-app进阶之自定义【day13】