首页 > 数据库 >mysql分页查询

mysql分页查询

时间:2023-06-16 17:34:25浏览次数:50  
标签:06 分页 16 33 47 mysql 查询 2023 34

创建测试表t1_part,id为自增主键,然后插入5900W条测试数据

CREATE TABLE t1_part (
id int NOT NULL AUTO_INCREMENT,
a varchar(30) DEFAULT NULL,
b varchar(30) DEFAULT NULL,
c varchar(30) DEFAULT NULL,
d timestamp NULL DEFAULT NULL,
PRIMARY KEY (id)
) 
 PARTITION BY RANGE (id)
(PARTITION p0 VALUES LESS THAN (10000001) ENGINE = InnoDB,
 PARTITION p1 VALUES LESS THAN (20000001) ENGINE = InnoDB,
 PARTITION p2 VALUES LESS THAN (30000001) ENGINE = InnoDB,
 PARTITION p3 VALUES LESS THAN (40000001) ENGINE = InnoDB,
 PARTITION p4 VALUES LESS THAN (50000001) ENGINE = InnoDB,
 PARTITION p5 VALUES LESS THAN (60000001) ENGINE = InnoDB,
 PARTITION p6 VALUES LESS THAN (70000001) ENGINE = InnoDB);

 

使用limit随机分页查看10条数据,可以看到随着分页深度增加需要的时间也增加

mysql> select a,b,c,d from t1_part limit 0,10;
+--------+-------------------------+------------------------+---------------------+
| a      | b                       | c                      | d                   |
+--------+-------------------------+------------------------+---------------------+
| name1  | bcdefghijklmnopqrstuvwx | cdefghijklmnopqrstuvwx | 2023-06-16 16:34:40 |
| name2  | bcdefghijklm            | fghijklm               | 2023-06-16 16:34:40 |
| name3  | bcdefghijklmno          | efghijklmno            | 2023-06-16 16:34:40 |
| name4  | bcdefghijklmnopqrstuv   | fghijklmnopqrstuv      | 2023-06-16 16:34:40 |
| name5  | bcdefghijklmnopqr       | jklmnopqr              | 2023-06-16 16:34:40 |
| name6  | bcdefghijklmnopqr       | fghijklmnopqr          | 2023-06-16 16:34:40 |
| name7  | bcdefghijklmnopqrst     | mnopqrst               | 2023-06-16 16:34:40 |
| name8  | bcdefghijklmnop         | defghijklmnop          | 2023-06-16 16:34:40 |
| name9  | bcdefghijklmnopqrs      | defghijklmnopqrs       | 2023-06-16 16:34:40 |
| name10 | bcdefghijklmnopqrstuvwx | lmnopqrstuvwx          | 2023-06-16 16:34:40 |
+--------+-------------------------+------------------------+---------------------+
10 rows in set (0.00 sec)

mysql> select a,b,c,d from t1_part limit 10000,10;
+-----------+-------------------------+---------------------+---------------------+
| a         | b                       | c                   | d                   |
+-----------+-------------------------+---------------------+---------------------+
| name10001 | bcdefghijklmnopqrstu    | lmnopqrstu          | 2023-06-16 16:34:40 |
| name10002 | bcdefghijklmnopqrstuvwx | fghijklmnopqrstuvwx | 2023-06-16 16:34:40 |
| name10003 | bcdefghijklmnopqrstuvwx | jklmnopqrstuvwx     | 2023-06-16 16:34:40 |
| name10004 | bcdefghijklmnopq        | jklmnopq            | 2023-06-16 16:34:40 |
| name10005 | bcdefghijklmno          | lmno                | 2023-06-16 16:34:40 |
| name10006 | bcdefghijklmnopq        | fghijklmnopq        | 2023-06-16 16:34:40 |
| name10007 | bcdefghijklmn           | lmn                 | 2023-06-16 16:34:40 |
| name10008 | bcdefghijklmnop         | ijklmnop            | 2023-06-16 16:34:40 |
| name10009 | bcdefghijklm            | ijklm               | 2023-06-16 16:34:40 |
| name10010 | bcdefghijklmnopqr       | hijklmnopqr         | 2023-06-16 16:34:40 |
+-----------+-------------------------+---------------------+---------------------+
10 rows in set (0.00 sec)

mysql> select a,b,c,d from t1_part limit 1000000,10;
+-------------+--------------------------+-------------------------+---------------------+
| a           | b                        | c                       | d                   |
+-------------+--------------------------+-------------------------+---------------------+
| name1000001 | bcdefghijklmnopqrstuv    | defghijklmnopqrstuv     | 2023-06-16 16:34:54 |
| name1000002 | bcdefghijklmnopqrstuvw   | ghijklmnopqrstuvw       | 2023-06-16 16:34:54 |
| name1000003 | bcdefghijklmnopqrstuvwxy | cdefghijklmnopqrstuvwxy | 2023-06-16 16:34:54 |
| name1000004 | bcdefghijklmnopq         | jklmnopq                | 2023-06-16 16:34:54 |
| name1000005 | bcdefghijklmnopqr        | hijklmnopqr             | 2023-06-16 16:34:54 |
| name1000006 | bcdefghijklmnopqrs       | hijklmnopqrs            | 2023-06-16 16:34:54 |
| name1000007 | bcdefghijklmno           | defghijklmno            | 2023-06-16 16:34:54 |
| name1000008 | bcdefghijklmnopqrs       | cdefghijklmnopqrs       | 2023-06-16 16:34:54 |
| name1000009 | bcdefghijklmnop          | efghijklmnop            | 2023-06-16 16:34:54 |
| name1000010 | bcdefghijklmnopqr        | cdefghijklmnopqr        | 2023-06-16 16:34:54 |
+-------------+--------------------------+-------------------------+---------------------+
10 rows in set (0.55 sec)

mysql> select a,b,c,d from t1_part limit 10000000,10;
+--------------+-----------------------+---------------------+---------------------+
| a            | b                     | c                   | d                   |
+--------------+-----------------------+---------------------+---------------------+
| name10000001 | bcdefghijklmnopq      | ghijklmnopq         | 2023-06-16 16:37:14 |
| name10000002 | bcdefghijklmnopqr     | fghijklmnopqr       | 2023-06-16 16:37:14 |
| name10000003 | bcdefghijklmnopq      | mnopq               | 2023-06-16 16:37:14 |
| name10000004 | bcdefghijklmn         | cdefghijklmn        | 2023-06-16 16:37:14 |
| name10000005 | bcdefghijklmnopqr     | hijklmnopqr         | 2023-06-16 16:37:14 |
| name10000006 | bcdefghijklmnopqrstuv | lmnopqrstuv         | 2023-06-16 16:37:14 |
| name10000007 | bcdefghijklmn         | jklmn               | 2023-06-16 16:37:14 |
| name10000008 | bcdefghijklmnopqrs    | ijklmnopqrs         | 2023-06-16 16:37:14 |
| name10000009 | bcdefghijklmnopqrstu  | fghijklmnopqrstu    | 2023-06-16 16:37:14 |
| name10000010 | bcdefghijklmnopqrstu  | cdefghijklmnopqrstu | 2023-06-16 16:37:14 |
+--------------+-----------------------+---------------------+---------------------+
10 rows in set (6.59 sec)
mysql> select a,b,c,d from t1_part limit 50000000,10;
+--------------+--------------------------+--------------------+---------------------+
| a            | b                        | c                  | d                   |
+--------------+--------------------------+--------------------+---------------------+
| name50000001 | bcdefghijklmnopqrstu     | defghijklmnopqrstu | 2023-06-16 16:47:33 |
| name50000002 | bcdefghijklmnopqrstu     | ghijklmnopqrstu    | 2023-06-16 16:47:33 |
| name50000003 | bcdefghijklmn            | ghijklmn           | 2023-06-16 16:47:33 |
| name50000004 | bcdefghijklmnopqrstuv    | mnopqrstuv         | 2023-06-16 16:47:33 |
| name50000005 | bcdefghijklmno           | ghijklmno          | 2023-06-16 16:47:33 |
| name50000006 | bcdefghijklmnopqrstu     | defghijklmnopqrstu | 2023-06-16 16:47:33 |
| name50000007 | bcdefghijklmnopqrs       | cdefghijklmnopqrs  | 2023-06-16 16:47:33 |
| name50000008 | bcdefghijklm             | ghijklm            | 2023-06-16 16:47:33 |
| name50000009 | bcdefghijklmnopqrstuvwxy | mnopqrstuvwxy      | 2023-06-16 16:47:33 |
| name50000010 | bcdefghijklmnopq         | ghijklmnopq        | 2023-06-16 16:47:33 |
+--------------+--------------------------+--------------------+---------------------+
10 rows in set (34.04 sec)

查看执行计划,走的是全表扫描,扫描了58816813行

mysql> explain select a,b,c,d from t1_part limit 50000000,10;
+----+-------------+---------+-------------------------------+------+---------------+------+---------+------+----------+----------+-------+
| id | select_type | table   | partitions                    | type | possible_keys | key  | key_len | ref  | rows     | filtered | Extra |
+----+-------------+---------+-------------------------------+------+---------------+------+---------+------+----------+----------+-------+
|  1 | SIMPLE      | t1_part | p0,p1,p2,p3,p4,p5,p6,p7,p8,p9 | ALL  | NULL          | NULL | NULL    | NULL | 58816813 |   100.00 | NULL  |
+----+-------------+---------+-------------------------------+------+---------------+------+---------+------+----------+----------+-------+

 

通过主键id + limit查看

mysql> select a,b,c,d from t1_part where id > 50000000 limit 10;
+--------------+--------------------------+--------------------+---------------------+
| a            | b                        | c                  | d                   |
+--------------+--------------------------+--------------------+---------------------+
| name50000001 | bcdefghijklmnopqrstu     | defghijklmnopqrstu | 2023-06-16 16:47:33 |
| name50000002 | bcdefghijklmnopqrstu     | ghijklmnopqrstu    | 2023-06-16 16:47:33 |
| name50000003 | bcdefghijklmn            | ghijklmn           | 2023-06-16 16:47:33 |
| name50000004 | bcdefghijklmnopqrstuv    | mnopqrstuv         | 2023-06-16 16:47:33 |
| name50000005 | bcdefghijklmno           | ghijklmno          | 2023-06-16 16:47:33 |
| name50000006 | bcdefghijklmnopqrstu     | defghijklmnopqrstu | 2023-06-16 16:47:33 |
| name50000007 | bcdefghijklmnopqrs       | cdefghijklmnopqrs  | 2023-06-16 16:47:33 |
| name50000008 | bcdefghijklm             | ghijklm            | 2023-06-16 16:47:33 |
| name50000009 | bcdefghijklmnopqrstuvwxy | mnopqrstuvwxy      | 2023-06-16 16:47:33 |
| name50000010 | bcdefghijklmnopq         | ghijklmnopq        | 2023-06-16 16:47:33 |
+--------------+--------------------------+--------------------+---------------------+
10 rows in set (0.01 sec)

查看执行计划 ,走的主键索引,扫描了4875761行

mysql> explain select a,b,c,d from t1_part where id > 50000000 limit 10;
+----+-------------+---------+----------------+-------+---------------+---------+---------+------+---------+----------+-------------+
| id | select_type | table   | partitions     | type  | possible_keys | key     | key_len | ref  | rows    | filtered | Extra       |
+----+-------------+---------+----------------+-------+---------------+---------+---------+------+---------+----------+-------------+
|  1 | SIMPLE      | t1_part | p5,p6,p7,p8,p9 | range | PRIMARY       | PRIMARY | 4       | NULL | 4875761 |   100.00 | Using where |
+----+-------------+---------+----------------+-------+---------------+---------+---------+------+---------+----------+-------------+

 

使用between .. and ..查看

mysql> select a,b,c,d from t1_part where id between 50000001 and 50000010;
+--------------+--------------------------+--------------------+---------------------+
| a            | b                        | c                  | d                   |
+--------------+--------------------------+--------------------+---------------------+
| name50000001 | bcdefghijklmnopqrstu     | defghijklmnopqrstu | 2023-06-16 16:47:33 |
| name50000002 | bcdefghijklmnopqrstu     | ghijklmnopqrstu    | 2023-06-16 16:47:33 |
| name50000003 | bcdefghijklmn            | ghijklmn           | 2023-06-16 16:47:33 |
| name50000004 | bcdefghijklmnopqrstuv    | mnopqrstuv         | 2023-06-16 16:47:33 |
| name50000005 | bcdefghijklmno           | ghijklmno          | 2023-06-16 16:47:33 |
| name50000006 | bcdefghijklmnopqrstu     | defghijklmnopqrstu | 2023-06-16 16:47:33 |
| name50000007 | bcdefghijklmnopqrs       | cdefghijklmnopqrs  | 2023-06-16 16:47:33 |
| name50000008 | bcdefghijklm             | ghijklm            | 2023-06-16 16:47:33 |
| name50000009 | bcdefghijklmnopqrstuvwxy | mnopqrstuvwxy      | 2023-06-16 16:47:33 |
| name50000010 | bcdefghijklmnopq         | ghijklmnopq        | 2023-06-16 16:47:33 |
+--------------+--------------------------+--------------------+---------------------+
10 rows in set (0.00 sec)

查看执行计划,走的主键索引,扫描了10行

mysql> explain select a,b,c,d from t1_part where id between 50000001 and 50000010;
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table   | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | t1_part | p4,p5      | range | PRIMARY       | PRIMARY | 4       | NULL |   10 |   100.00 | Using where |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-------------+

 

使用between and + limit查看

mysql> select a,b,c,d from t1_part where id between 50000001 and 50010000 limit 10;
+--------------+--------------------------+--------------------+---------------------+
| a            | b                        | c                  | d                   |
+--------------+--------------------------+--------------------+---------------------+
| name50000001 | bcdefghijklmnopqrstu     | defghijklmnopqrstu | 2023-06-16 16:47:33 |
| name50000002 | bcdefghijklmnopqrstu     | ghijklmnopqrstu    | 2023-06-16 16:47:33 |
| name50000003 | bcdefghijklmn            | ghijklmn           | 2023-06-16 16:47:33 |
| name50000004 | bcdefghijklmnopqrstuv    | mnopqrstuv         | 2023-06-16 16:47:33 |
| name50000005 | bcdefghijklmno           | ghijklmno          | 2023-06-16 16:47:33 |
| name50000006 | bcdefghijklmnopqrstu     | defghijklmnopqrstu | 2023-06-16 16:47:33 |
| name50000007 | bcdefghijklmnopqrs       | cdefghijklmnopqrs  | 2023-06-16 16:47:33 |
| name50000008 | bcdefghijklm             | ghijklm            | 2023-06-16 16:47:33 |
| name50000009 | bcdefghijklmnopqrstuvwxy | mnopqrstuvwxy      | 2023-06-16 16:47:33 |
| name50000010 | bcdefghijklmnopq         | ghijklmnopq        | 2023-06-16 16:47:33 |
+--------------+--------------------------+--------------------+---------------------+
10 rows in set (0.00 sec)

查看执行计划,扫描了19711行

mysql> explain select a,b,c,d from t1_part where id between 50000001 and 50010000 limit 10;
+----+-------------+---------+------------+-------+---------------+---------+---------+------+-------+----------+-------------+
| id | select_type | table   | partitions | type  | possible_keys | key     | key_len | ref  | rows  | filtered | Extra       |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+-------+----------+-------------+
|  1 | SIMPLE      | t1_part | p4,p5      | range | PRIMARY       | PRIMARY | 4       | NULL | 19711 |   100.00 | Using where |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+-------+----------+-------------+

 

总结:

  通过有序的“主键”或“唯一键”索引为分页查看效率更高,通过扫描行数来看使用between ..and进行分页查询,执行时间和扫描成本更少。

标签:06,分页,16,33,47,mysql,查询,2023,34
From: https://www.cnblogs.com/haha029/p/17485593.html

相关文章

  • 基于MySQL 8.0从库磁盘满报13121错误的处理
    背景:基于GTID的从库服务器磁盘使用率100%,扩容磁盘后报错,开启复制,过一会就报错13121错误一.报错现象: Last_SQL_Errno:13121Last_SQL_Error:Relaylogreadfailure:Couldnotparserelaylogevententry.Thepossiblereasonsare:themaster'sbinarylogiscorrupted......
  • 过滤实现条件查询记录:
    publicList<TbRemouldAirVO>airQueryByMap(List<TbRemouldAirVO>airVO,Map<String,String>map){//1.改造时间if(!StringUtils.isEmpty(map.get("remouldTimeAir"))){airVO=airVO.stream().filter(x->Objects.equals......
  • MySQL 拓展
    更新自增idaltertableTestCaseAUTO_INCREMENT=495;查看sql表进程//命令行SHOWFULLPROCESSLIST;杀死sql进程//命令行killprocessmysql获取自增值SELECTauto_incrementFROMinformation_schema.tablesWHEREtable_name='表名'mysql自增id重置--1、......
  • MySQL报错1406_MySQL #1406 Data too long错误
    造成这个错误有两种可能1)仔细检查sql语句是不是插入值颠倒了位置2)java端与mysql表编码不一致:1.修改Server端编码。修改character_set_xxx2.在JDBC-URL连接后面加上&useUnicode=true&characterEncoding=UTF83.修改my.ini文件去掉sql-mode="STRICT_TRANS_TABLES,NO_A......
  • MySQL分库分表
    垂直切分将不同业务模块所使用的表切分到不同的数据库(主机)之上,这样的切能够称之为数据的垂直(纵向)切分在架构设计中,各个功能模块相互之间的交互点越统一越少,系统的耦合度就越低,系统各个模块的维护性以及扩展性也就越好,实现数据的垂直切分也就越简单垂直切分的长处◆数据库的拆......
  • 怎样利用SEO查询提升网站建设?
    作为一名网站管理员或SEO从业者,你肯定知道每个网站的排名对于流量和曝光率有着重要的影响。那么如何利用SEO查询,提升网站排名呢?今天,我将和大家分享一些我自己的经验和技巧。1.了解网站关键词在进行SEO查询之前,我们需要先了解我们网站的关键词。关键词是指用户在搜索引擎中输入的......
  • 二进制文件安装mysql5.7
    yum安装mysql5.7虽然方便,但是安装的路径和配置难以定制化,这边使用二进制安装,即使用官方提供的编译软件包来安装,不需要额外准备依赖的环境软件,安装也会变的更加轻量,且可以定制化。一、自定制规范Mysql本身没有明确的安装规范,但是日常工作中为了便于管理和定位问题,我们需要对Mysql......
  • mysql 启动提示:错误2系统找不到指定文件;
    mysql启动后系统提示错误:mysql启动提示:错误2系统找不到指定文件;问题解决过程:注册表位置: \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\mysql图中所示的位置与拷贝到服务器的mysql部署位置不一致,修改后,系统启动正常。  修改后,mysql正常启动。 ......
  • MySQL索引优化与查询优化
    一:优化方式及数据准备1:优化方式在日常开发中都有哪些维度可以进行数据库调优?如下:①:索引失效、没有充分利用到索引--需要合理建立索引和利用索引②:关联查询太多JOIN(设计缺陷或不得已的需求) --需要对SQL优化③:服务器......
  • solr 模拟数据库like查询(不使用分词)
    IK分词个别拆分的不够完美,另外个别业务逻辑是需要替代数据库的like查询。所以本篇文章是介绍如何在solr中使用类似数据库的like查询。本片文章是介绍如何在solr中使用类似数据库的like操作。首先我们抛弃text_ik。IK分词,因为使用的是like操作,所以这块不能在使用分词了。我们需......