MySQL分页Limit的优化过程实战
作者:旧梦发癫 时间:2024-01-25 12:16:58
前言
在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,这个时候怎么办呢?不用担心,mysql已经为我们提供了这样一个功能。
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1): 为了与 PostgreSQL 兼容,MySQL 也支持句法: LIMIT # OFFSET #。
所以通常在查询数据的时候,我们都会用到limit分页,因为这样避免了全表查询,会提高查询效率。但是在一个表的数据量多了之后,分页查询会明细的变慢,下面来一起看看详细的介绍吧
MySQL分页Limit优化
创建测试表card 2000万数据
mysql> select count(*) from card;
+----------+
| count(*) |
+----------+
| 20000000 |
+----------+
1 row in set (0.00 sec)
-首先测试前1000行查询速度
mysql> select * from card limit 1000,10;
+---------+--------------------------------------+
| card_id | card_number |
+---------+--------------------------------------+
| 1001 | 13fc90a6-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1002 | 13fc923e-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1003 | 13fc93d5-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1004 | 13fc956a-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1005 | 13fc9702-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1006 | 13fc9899-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1007 | 13fc9a31-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1008 | 13fc9bc6-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1009 | 13fc9d5e-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1010 | 13fc9ef5-2e3b-11e8-ae62-9c5c8e6e37cf |
+---------+--------------------------------------+
10 rows in set (0.00 sec)
-测试100万之后的查询
mysql> select * from card limit 1000000,10;
+---------+--------------------------------------+
| card_id | card_number |
+---------+--------------------------------------+
| 1000001 | 2d87021a-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000002 | 2d8703ac-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000003 | 2d87053b-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000004 | 2d8706cd-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000005 | 2d87085f-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000006 | 2d8709f1-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000007 | 2d870b83-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000008 | 2d870d18-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000009 | 2d870eaa-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000010 | 2d871039-2e3b-11e8-ae62-9c5c8e6e37cf |
+---------+--------------------------------------+
10 rows in set (0.18 sec)
-测试1000万之后的查询
mysql> select * from card limit 10000000,10;
+----------+--------------------------------------+
| card_id | card_number |
+----------+--------------------------------------+
| 10000001 | b11ad76c-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000002 | b11aefd5-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000003 | b11af868-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000004 | b11b0031-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000005 | b11b07ad-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000006 | b11b0f0f-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000007 | b11b1669-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000008 | b11b1db2-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000009 | b11b24fa-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000010 | b11b2c37-2e49-11e8-ae62-9c5c8e6e37cf |
+----------+--------------------------------------+
10 rows in set (1.29 sec)
可以看到越到后面查询效率会越低。因为在查询100万之后的数据的时候,mysql会首先查询100万零10条数据,然后截取后面的十条数据。这些就造成的性能的降低。
那么怎么去避免这个扫描100万条数据呢。我们可以明确的知道,100万之后的主键是大于100万的。所以我们可以将sql改写,让其用到索引,降低扫描的行数
mysql> select * from card where card_id>=1000000 limit 10;
+---------+--------------------------------------+
| card_id | card_number |
+---------+--------------------------------------+
| 1000000 | 2d870088-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000001 | 2d87021a-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000002 | 2d8703ac-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000003 | 2d87053b-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000004 | 2d8706cd-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000005 | 2d87085f-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000006 | 2d8709f1-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000007 | 2d870b83-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000008 | 2d870d18-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000009 | 2d870eaa-2e3b-11e8-ae62-9c5c8e6e37cf |
+---------+--------------------------------------+
10 rows in set (0.00 sec)
这样就可以很大的提高查询效率
来源:https://segmentfault.com/a/1190000016251817
标签:mysql,limit,分页
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
教你如何使用firebug调试功能了解javascript闭包和this
2024-04-22 13:09:34
![](https://img.aspxhome.com/file/2023/9/135739_0s.jpg)
详解MySql存储过程参数的入门使用
2024-01-25 23:24:06
![](https://img.aspxhome.com/file/2023/8/95738_0s.png)
JavaScript实现大文件上传的示例代码
2024-05-28 15:40:23
提升MySQL查询效率及查询速度优化的四个方法详析
2024-01-14 21:05:11
![](https://img.aspxhome.com/file/2023/1/118471_0s.jpg)
Web 2.0 框架发布
2008-03-25 09:40:00
![](https://img.aspxhome.com/file/UploadPic/20083/25/200832595359766s.jpg)
golang1.16新特性速览(推荐)
2023-07-06 10:34:47
手把手教你用python抢票回家过年(代码简单)
2023-07-13 22:46:02
![](https://img.aspxhome.com/file/2023/0/118100_0s.jpg)
python PyTorch预训练示例
2022-07-06 18:24:02
SQL 字母数字混合型字段 按里面的数字排序
2010-04-23 18:18:00
![](https://img.aspxhome.com/file/UploadPic/20104/23/1-47s.jpg)
Python requests模块安装及使用教程图解
2022-08-10 15:29:45
![](https://img.aspxhome.com/file/2023/2/124962_0s.png)
在python中利用最小二乘拟合二次抛物线函数的方法
2021-05-03 01:18:20
![](https://img.aspxhome.com/file/2023/8/95268_0s.jpg)
python基于scrapy爬取京东笔记本电脑数据并进行简单处理和分析
2023-08-05 03:18:06
![](https://img.aspxhome.com/file/2023/2/72252_0s.png)
python基础教程项目三之万能的XML
2022-09-22 05:33:25
ORACLE 报警日志如何查看?第1/2页
2009-07-02 12:06:00
Python开根号的几种方式详解
2021-07-10 20:37:34
![](https://img.aspxhome.com/file/2023/5/75935_0s.png)
Mysql如何查看是否使用到索引
2024-01-25 09:58:06
![](https://img.aspxhome.com/file/2023/2/119712_0s.png)
如何实现一个简易版的vuex持久化工具
2024-04-30 10:34:29
PHP global全局变量经典应用与注意事项分析【附$GLOBALS用法对比】 <font color=red>原创</font>
2023-11-16 07:37:55
英文版面设计的8个禁忌
2009-10-14 20:42:00
![](https://img.aspxhome.com/file/UploadPic/200910/14/01-84s.jpg)
Python中的 any() 函数和 all() 函数
2023-05-05 08:38:50