当前位置:网站首页>MySQL limit使用及超大分页问题解决
MySQL limit使用及超大分页问题解决
2022-07-27 21:56:00 【律二萌萌哒】
limit语法
limit语法支持两个参数,offset和limit,前者表示偏移量,后者表示取前limit条数据.
例如:
## 返回符合条件的前10条语句
select * from user limit 10
## 返回符合条件的第11-20条数据
select * from user limit 10,20
从上面也可以看出来,limit n 等价于limit 0,n.
性能分析
实际使用中我们会发现,在分页的后面一些页,加载会变慢,也就是说:
select * from user limit 1000000,10
语句执行较慢.那么我们首先来测试一下.
首先是在offset较小的情况下拿100条数据.(数据总量为200左右).然后逐渐增大offset.
select * from user limit 0,100 ---------耗时0.03s
select * from user limit 10000,100 ---------耗时0.05s
select * from user limit 100000,100 ---------耗时0.13s
select * from user limit 500000,100 ---------耗时0.23s
select * from user limit 1000000,100 ---------耗时0.50s
select * from user limit 1800000,100 ---------耗时0.98s
可以看到随着offset的增大,性能越来越差.
这是为什么呢?因为limit 10000,10的语法实际上是mysql查找到前10010条数据,之后丢弃前面的10000行,这个步骤其实是浪费掉的.
优化
1. 用id优化
先找到上次分页的最大ID,然后利用id上的索引来查询,类似于
select * from user where id>1000000 limit 100.
这样的效率非常快,因为主键上是有索引的,但是这样有个缺点,就是ID必须是连续的,并且查询不能有where语句,
因为where语句会造成过滤数据.
2. 用覆盖索引优化
mysql的查询完全命中索引的时候,称为覆盖索引,是非常快的,因为查询只需要在索引上进行查找,之后可以直接返回,而不用再回数据表拿数据.因此我们可以先查出索引的ID,然后根据Id拿数据.
select * from (select id from job limit 1000000,100) a left join job b on a.id = b.id;
耗时0.2秒.
总结
用mysql做大量数据的分页确实是有难度,但是也有一些方法可以进行优化,需要结合业务场景多进行测试.
当用户翻到10000页的时候,不如我们直接返回空好了,这么无聊的吗…
边栏推荐
- A few lines of code can easily realize the real-time reasoning of paddleocr. Come and get!
- Esp8266----- mqtt cloud device on cloud
- Shell(3)
- 数据中台的那些“经验与陷阱”
- Read one line of string input each time (to be supplemented)
- Microsoft Amazon layoffs, the economic crisis is getting closer...
- Cache and MMU management
- 永州出入境检验实验室建设那些事
- Remote monitoring of pump station
- 基本初等函数
猜你喜欢

英特尔发布开源AI参考套件

Harmonyos 3 pure mode can limit the access to personal data of risk applications detected in Huawei's application market
![[21 day learning challenge] classmate K invites you to participate in the in-depth learning seminar](/img/88/b8d5e2a8609fbef57a1291b7c4225e.png)
[21 day learning challenge] classmate K invites you to participate in the in-depth learning seminar

强强协同,共拓发展!英特尔与太一物联举办 AI 计算盒聚合服务研讨会

元宇宙的应用场景展示

JS ATM output

MFC prompts that this application has requested the runtime to terminate it in an unused way editbox box has been deleted and is still in use

Openvino integrates tensorflow to accelerate reasoning

Strong collaboration and common development! Intel and Taiyi IOT held a seminar on AI computing box aggregation services

图片提取文字很神奇?试试三步实现OCR!
随机推荐
细数国产接口协作平台的六把武器!
Impulse attends the 2022 Forum on safe circulation of data elements Online - a special session in the field of government affairs, and helps the construction and innovative development of big data for
ADB path cannot contain 2 spaces remote could n't create file: is a directory
JVM memory model
Precautions for site selection of Yongzhou clean animal laboratory
Esp8266----- mqtt cloud device on cloud
服务器开放敏感端口
The second uncle cured my spiritual internal friction and made me angry out of station B
「图神经网络:基础、前沿与应用」最新IJCAI2022教程
Window function over
Camera and lidar calibration: gazebo simulation livox_ camera_ lidar_ Calibration ---- external parameter calibration calculation and result verification
相应通道无电压但ADC的值却在大幅变化且不等于0的可能原因
Intel AI practice day issue 56 | explore new trends in industry development
图片提取文字很神奇?试试三步实现OCR!
MATLAB | 那些你不得不知道的MATLAB小技巧(三)
BuildForge 资料
Strong collaboration and common development! Intel and Taiyi IOT held a seminar on AI computing box aggregation services
这种动态规划你见过吗——状态机动态规划之股票问题(中)
窗口函数over
Leetcode 452. minimum number of arrows to burst balloons (medium)