mysql分页是物理分页_学习MySQL:什么是分页

2023-05-16

mysql分页是物理分页

In this article, I am going to explain that in MySQL, what is pagination and how we can implement it. When we are populating a large dataset from the MySQL database, it is not easy to read all the records on the same page. Using pagination, we can divide the result-set into multiple pages, and that increases the readability of the result set. In this article, I am going to explain:

在本文中,我将解释在MySQL中什么是分页以及如何实现它。 当我们从MySQL数据库填充大型数据集时,要读取同一页面上的所有记录并不容易。 使用分页,我们可以将结果集分为多个页面,这可以提高结果集的可读性。 在本文中,我将解释:

  1. How to implement pagination with example

    如何通过示例实现分页
  2. OFFSET using Page Number 偏移量
  3. The performance overheads and how to deal with it

    性能开销以及如何处理

For the demonstration, I have installed MySQL on my workstation. You can walk through one of my previous articles, How to install MySQL database server 8.0.19 on Windows 10, that explains how we can install MySQL on window 10. I have imported a sample database named sakila in the MySQL database server. You can add the sample database while installing the MySQL database server.

为了演示,我在工作站上安装了MySQL。 您可以浏览我以前的文章之一, 如何在Windows 10上安装MySQL数据库服务器8.0.19 ,它说明了如何在窗口10上安装MySQL。我已经在MySQL数据库服务器中导入了一个名为sakila的示例数据库。 您可以在安装MySQL数据库服务器时添加示例数据库。

如何实现分页 (How to implement pagination)

We can implement pagination by using the LIMIT clause in the SELECT query. Following is the syntax:

我们可以通过在SELECT查询中使用LIMIT子句来实现分页。 以下是语法:

Select * from <table_name> LIMIT value_1, OFFSET value_2

LIMIT条款 (LIMIT Clause)

In syntax, the first argument is the LIMIT clause. This LIMIT clause is used to restrict the number of rows to be returned by the SQL Query. It is like the TOP clause in SQL Server.

在语法上,第一个参数是LIMIT子句。 此LIMIT子句用于限制SQL查询返回的行数。 就像SQL Server中的TOP子句。

For example, I want to return the top 20 actors. The output must be sorted based on the actor_id column, and the order must be ascending. Following is the query:

例如,我想返回前20名演员。 必须根据actor_id列对输出进行排序,并且顺序必须升序。 以下是查询:

select * from actor order by actor_id asc LIMIT 20

Below is the output of the query:

以下是查询的输出:

抵消条款 (OFFSET Clause)

If you notice the syntax again, the second argument is OFFSET. It is used to view a specific number of rows; for example, in a query output, you want to see the records between 10 and 20, then you can use OFFSET. It populates all the records of the table, and it discards the previous records that are defined in the OFFSET clause.

如果再次注意到语法,则第二个参数为OFFSET 。 它用于查看特定数量的行; 例如,在查询输出中,您想要查看10到20之间的记录,则可以使用OFFSET 。 它填充表的所有记录,并丢弃在OFFSET子句中定义的先前记录。

For example, we want to display the top 20 names of actor and the value of the actor_id column must start from 11 then you can write the query as below:

例如,我们要显示actor的前20名,并且actor_id列的值必须从11开始,然后可以编写如下查询:

select * from actor LIMIT 20 OFFSET 10;

Following is the output:

以下是输出:

As you can see in the above image, the value of the actor_id column starts from 10, and it has returned a total of 20 records.

如上图所示, actor_id列的值从10开始它总共返回了20条记录。

如何使用页码计算偏移量 (How to calculate the OFFSET using Page Number)

To calculate the number of the pages, you can hardcode the value of LIMIT and OFFSET, or you can pass the value of page number in the variables. We can calculate the value of OFFSET using the value of page number. It is a little bit more secure because we are passing the value of page number, and someone cannot create a page that has all the records by manipulating the value of the input variable.

要计算页数,可以对LIMITOFFSET的值进行硬编码,也可以在变量中传递页码的值。 我们可以使用页码的值来计算OFFSET的值。 因为我们正在传递页码的值,而且有人无法通过操纵输入变量的值来创建包含所有记录的页,所以它更加安全。

The following is the formula:

公式如下:

Records_Per_page= 10;
offset_value = (page_Number-1) * Records_Per_page;

Records_Per_page = 10;
offset_value =(page_Number-1)* Records_Per_page;

For example, you want to display the name of 30 actors in 3 pages. Therefore, the formula must be as shown below:

例如,您要在3页中显示30个演员的名称。 因此,公式必须如下所示:

Records_Per_page= 10;
offset_value_1 = (1-1) * 10;
offset_value_2 = (2-1) * 10;
offset_value_3 = (3-1) * 10;

Records_Per_page = 10;
offset_value_1 =(1-1)* 10;
offset_value_2 =(2-1)* 10;
offset_value_3 =(3-1)* 10;

The value of Records_per_pages is used in the LIMIT clause and the values of offset_value_1, offset_value_2, offset_value_3 is used in the OFFSET clause.

Records_per_pages的值LIMIT子句中使用,并且offset_value_1,offset_value_2的值,offset_value_3是偏移子句中使用。

The query should be written as follows:

该查询应编写如下:

select * from actor LIMIT 10 OFFSET 0;
select * from actor LIMIT 10 OFFSET 10;
select * from actor LIMIT 10 OFFSET 20;

Following is the output of the first query:

以下是第一个查询的输出:

Following is the output of the second query:

以下是第二个查询的输出:

Following is the output of the third query:

以下是第三个查询的输出:

性能开销 (The performance overheads)

The pagination is very useful when you want to design an application that displays a large dataset into multiple pages. While designing the application, we populate the entire dataset from the database server and then perform pagination on the application/web server. Now, when you perform this on the database server, you can achieve performance improvement. But sometimes it becomes an additional overhead on the database server. Especially when you are sharing the same database server between multiple client databases, to avoid the performance issue, we can use the following alternative.

当您要设计一个将大型数据集显示为多个页面的应用程序时,分页非常有用。 在设计应用程序时,我们从数据库服务器填充整个数据集,然后在应用程序/ Web服务器上执行分页。 现在,当您在数据库服务器上执行此操作时,可以提高性能。 但是有时这会成为数据库服务器上的额外开销。 特别是在多个客户端数据库之间共享同一数据库服务器时,为避免性能问题,我们可以使用以下替代方法。

使用ORDER BY实现分页(避免丢弃记录) (Implement the paging using ORDER BY (avoid discarding the records))

As mentioned, the pagination populates all the records of the table, displays the number of records specified in the LIMIT clause, and discards all the previous records specified in the OFFSET clause. If the table contains millions of records, then the performance of the query can be reduced significantly. To avoid that, we can specify the range in the WHERE clause and limit the records using the LIMIT clause. For example, we want to populate the name of the top ten actors whose actor_id is greater than 10. The query should be written as follows:

如前所述,分页将填充表的所有记录,显示LIMIT子句中指定的记录数,并丢弃OFFSET子句中指定的所有先前记录。 如果表包含数百万条记录,则查询的性能可能会大大降低。 为了避免这种情况,我们可以在WHERE子句中指定范围,并使用LIMIT子句限制记录。 例如,我们要填充actor_id大于10的前十个actor的名称。 该查询应编写如下:

select * from actor where actor_id > 10 LIMIT 10;

Below is the output:

以下是输出:

摘要 (Summary)

The pagination is a very interesting concept in MySQL and can be achieved easily by LIMIT and OFFSET clauses. It is very useful when you want to display the large recordset on multiple pages. In this article, I have explained about the pagination and how we can implement it, how we can calculate the OFFSET using page number, and the number of records per page. I have also explained how it can impact the performance and the possible alternative to it.

分页是MySQL中一个非常有趣的概念,可以通过LIMITOFFSET子句轻松实现。 当您要在多个页面上显示大型记录集时,此功能非常有用。 在本文中,我已经解释了 分页以及如何实现它,如何使用页码计算偏移量 ,以及每页的记录数。 我还解释了它如何影响性能以及可能的替代方案。

目录 (Table of contents)

Learn MySQL: Querying data from MySQL server using the SELECT statement
Learn MySQL: What is pagination
Learn MySQL: Sorting and Filtering data in a table
学习MySQL:使用SELECT语句从MySQL服务器查询数据
学习MySQL:什么是分页
学习MySQL:对表中的数据进行排序和过滤

翻译自: https://www.sqlshack.com/learn-mysql-what-is-pagination/

mysql分页是物理分页

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

mysql分页是物理分页_学习MySQL:什么是分页 的相关文章

随机推荐

  • centos下部署pyspider(精简版)

    centos7部署pyspider 一 安装依赖 yum install gcc gcc c 43 43 python pip python devel python distribute libxml2 libxml2 devel pyt
  • centos7分布式部署pyspider(精简)

    0理论 以下内容摘自框架设计 pyspider 的架构主要分为 scheduler xff08 调度器 xff09 fetcher xff08 抓取器 xff09 processor xff08 脚本执行 xff09 xff1a 各个组件间
  • 菜鸟笔记---startx执行后显示Only console users are allowed to run the X server

    输入startx amp 后报错信息 span class token operator span usr span class token operator span lib span class token operator span
  • 【supervisor】安装和使用,超级详细,开机自启动,以及其它的注意细节

    1 安装supervisor yum pip的方式都可以 yum y install supervisor 已加载插件 xff1a fastestmirror Loading mirror speeds from cached hostfi
  • poj1664-放苹果

    放苹果 把N个同样的苹果放在M个同样的盘子里 xff0c 允许有的盘子空着不放 xff0c 问共有多少种不同的分法 xff1f xff08 用K表示 xff09 5 xff0c 1 xff0c 1和1 xff0c 5 xff0c 1 是同一
  • VS报错——无法打开文件XXX.lib

    添加lib文件 xff1a 配置属性 gt 链接器 gt 输入 gt 附加依赖项加入库名 xff08 lib xff09 xff1b 或者是在cpp源文件中用 pragma comment lib lib 来代替 此时编译会提示 xff1a
  • Kafka与Flink结合使用

    Kafka与Flink结合使用 本地连接kafka 首先可以先以本地模式来对kafka进行操作 当我们在系统 xff08 可以是windows xff0c 尽量linux xff09 上部署好了Zookeeper和kafka集群 xff0c
  • 当前脑电入门

    资料 1 公众号脑机接口社区汇总 2 MNE 3 EEG科普 4 公众号BrainTechnology 5 公众号路同学 6 知乎Alex 7 程序员大本营 xff0c 很多相关 数据集 上海交大SEED数据集 BCI EEG数据集 xff
  • 使用Diskspd测试SQL Server存储子系统

    In this article we will learn how to test our storage subsystems performance using Diskspd The storage subsystem is one
  • Python读取mat文件——matlab的数据

    Python处理mat文件 1 读取2 数据格式 1 读取 很多人使用MATLAB并将数据存储在 mat文件中 这些文件是什么 这些文件包含MATLAB工作空间分配给它们的变量和对象列表 它作为字典导入到Python中 xff0c 其中键是
  • 我的毕设4.24—

    EEG 1 寻找数据2 读入数据3 了解数据3 处理数据 1 寻找数据 要找到关于情感的EEG数据集 by CSDN 2 读入数据 因为获取的数据集是mat文件 xff0c 解决Python读取mat文件 by 文本检索图书馆w pytho
  • 关于提高信息传输率三个方法之一——增加频带宽度

    转载原作 以下解释为什么增加频带宽度可减弱码间串扰 主要由于高频信号分量对波形影响是变得接近数字信号 xff0c 分界垂直 xff0c 减小斜坡 信道极限容量 任何实际的信道都不是理想的 xff0c 在传输信号时会产生各种失真以及带来多种干
  • CSMA/CD计算机网络(第七版)谢希仁 3-25 CSMA/CD

    计算机网络 xff08 第七版 xff09 谢希仁 3 25 假定站点A和B在同一个10Mb s以太网网段上 这两个站点之间的传播时延为225比特时间 站点A和B在t 61 0时同时发送了数据帧 当t 61 225比特时间 xff0c A和
  • L3-019 代码排版 (30 分) 别看 没改完

    太复杂了啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊 不做了 include lt iostream gt include lt cstdio gt using namespace std string s bo
  • 无人机航线规划

    非最短非最长 xff0c 因为只有10个点 xff0c 直接搜 标题 xff1a 无人机航线规划 某海岛遭遇飓风灾害 xff0c 道路设施受损严重 救援部门为尽快探明主要交通干道的受损情况 xff0c 在地图上划定了主要交通干道的侦察点 x
  • 17蓝桥C语言B组 7.日期问题

    标题 xff1a 日期问题 小明正在整理一批历史文献 这些历史文献中出现了很多日期 小明知道这些日期都在1960年1月1日至2059年12月31日 令小明头疼的是 xff0c 这些日期采用的格式非常不统一 xff0c 有采用年 月 日的 x
  • 安装Scipy。。。。。。。。

    放弃pip转用conda戳 2019 4 21 安装Scipy 以下所有安装过程假设电脑中安装有pip xff0c 如果没有就请装一个 首先 xff0c windows正常通过pip命令安装numpy和scipy的时候 xff1a pip
  • 《Python编程:从入门到实践》文件资料下载

    感谢分享者 下载地址 往下翻 下载压缩包
  • 01、BUCK电路的参数计算

    案例 xff1a 设计一个Buck电路 xff0c 满足如下性能指标要求 xff1a 一 性能指标要求 1 输入电压 2 输出电压 3 输出电压纹波 4 电流纹波 5 开关频率 二 需要计算的参数 三 BUCK电路拓扑 四 BUCK电路工作
  • mysql分页是物理分页_学习MySQL:什么是分页

    mysql分页是物理分页 In this article I am going to explain that in MySQL what is pagination and how we can implement it When we