Home Database Mysql Tutorial The meaning and method of paging technology principles and implementation (1)_MySQL

The meaning and method of paging technology principles and implementation (1)_MySQL

Jul 06, 2016 pm 01:32 PM
Pagination

What is paging technology
Paging is a technology that displays all data to the user in segments. What the user sees each time is not all the data, but a part of it. If the content they want for self-study is not found in it, the user can Convert the visible content by specifying page numbers or turning pages until you find the content you want. In fact, this is very similar to when we read a book.
The following page shows two common paging methods:

The meaning of pagination
Paging is indeed effective, but it will definitely increase the complexity of the system, but can it be avoided? If the amount of data is small, of course it is possible. But for enterprise information systems, the amount of data will not be limited to a small range. If you desperately select * from a certain table and then throw the returned data to the client, even if the client can tolerate thousands of dazzling tabular data, the busy network and the nervous server will Put forward their silent protests, and sometimes even end with an outright strike. This ending is a bit like the story between an ancient tyrant and his intolerable subjects.
The programmer is not a tyrant. He hopes that the program will make life better rather than worse. Considering the fact that most enterprise information systems have three-layer or even more-layer architectures, programmers should adopt the form of paging when displaying data to customers. .if he doesn't want to be inundated with complaints or woken up by phone calls in the middle of the night.

The entire process from request initiation to data return
Now that you have made up your mind to paginate, before you start writing the code, let us recall that in a typical three-tier architecture, the entire process from request initiation to return data is as follows:

Where to paginate
From the above picture, we can observe that after the SQL statement is processed, the database, WebApplication and Browser can all perform paging. So where is the best place to do paging?
The criterion for judgment is speed. Obviously, there is a network between the database server, Web application server and client. If the amount of data transmitted by the network is smaller, the client will get a response faster. And generally speaking, the database server and Web The processing power of the application server is generally much stronger than that of the client. From these two points, the solution of paging on the client is the most undesirable.
Secondly, there are only two methods left: paging on the Web server side and paging on the database side. If you choose to paging on the Web server side, most of the filtered data will still be transmitted to the Web application server. This is not as good as Perform paging directly on the database side.
Therefore, a better paging practice should be to retrieve only page-sized blocks of data from the database each time the page is turned. In this way, although the database needs to be queried every time the page is turned, the number of records queried is very small, and the amount of data transmitted over the network is not large. If a connection pool is used, the most time-consuming process of establishing a database connection can be skipped. On the database side, there are various mature optimization technologies used to improve query speed, which are much more effective than caching at the application server layer.

Paging SQL statement
If we access the database through JDBC, then it is necessary to use different SQL paging statements according to the database type. For the MySQL database, we can use the limit statement for paging. For the Oracle database, we can use the rownum method. Pagination.
(1) MySql’s Limit m,n statement
Among the two parameters after Limit, parameter m is the starting index, which starts from 0; parameter n is the number of records returned. If we need paging, just specify these two values
(2) rownum of Oracle database
In Oracle database, the paging method is not as simple as MySql. It needs to rely on rownum to achieve it.
Rownum represents the row number of a record. It is worth noting that it is assigned after each row is obtained. Therefore, if you want to specify the range of rownum to obtain paging data, it cannot be done in a layer of query statements. To do paging, you have to do it again. Query.

 SELECT * FROM 
(
SELECT A.*, ROWNUM RN 
FROM (SELECT * FROM TABLE_NAME) A 
WHERE ROWNUM <= 40
)
WHERE RN >= 21
Copy after login

其中最内层的查询SELECT * FROM TABLE_NAME表示不进行翻页的原始查询语句。ROWNUM <= 40和RN >= 21控制分页查询的每页的范围。
上面给出的这个分页查询语句,在大多数情况拥有较高的效率。分页的目的就是控制输出结果集大小,将结果尽快的返回。在上面的分页查询语句中,这种考虑主要体现在WHERE ROWNUM <= 40这句上。
选择第21到40条记录存在两种方法,一种是上面例子中展示的在查询的第二层通过ROWNUM <= 40来控制最大值,在查询的最外层控制最小值。而另一种方式是去掉查询第二层的WHERE ROWNUM <= 40语句,在查询的最外层控制分页的最小值和最大值。这是,查询语句如下:

 SELECT * FROM 
(
SELECT A.*, ROWNUM RN 
FROM (SELECT * FROM TABLE_NAME) A 
)
WHERE RN BETWEEN 21 AND 40
Copy after login

 对比这两种写法,绝大多数的情况下,第一个查询的效率比第二个高得多。
 这是由于CBO优化模式下,Oracle可以将外层的查询条件推到内层查询中,以提高内层查询的执行效率。对于第一个查询语句,第二层的查询条件WHERE ROWNUM <= 40就可以被Oracle推入到内层查询中,这样Oracle查询的结果一旦超过了ROWNUM限制条件,就终止查询将结果返回了。
 而第二个查询语句,由于查询条件BETWEEN 21 AND 40是存在于查询的第三层,而Oracle无法将第三层的查询条件推到最内层(即使推到最内层也没有意义,因为最内层查询不知道RN代表什么)。因此,对于第二个查询语句,Oracle最内层返回给中间层的是所有满足条件的数据,而中间层返回给最外层的也是所有数据。数据的过滤在最外层完成,显然这个效率要比第一个查询低得多。
 上面分析的查询不仅仅是针对单表的简单查询,对于最内层查询是复杂的多表联合查询或最内层查询包含排序的情况一样有效。

精彩专题分享:

SQL Server分页技术总结:http://www.bitsCN.com/Special/604.htm

ASP.NET分页功能操作:http://www.bitsCN.com/Special/149.htm

javascript分页功能操作:http://www.bitsCN.com/Special/469.htm

jquery分页功能操作:http://www.bitsCN.com/Special/465.htm

php分页功能操作:http://www.bitsCN.com/Special/236.htm

 其实分页技术还有很多其它技术,比如:Struts、hibernate等等都可实现。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP development: How to implement table data sorting and paging functions PHP development: How to implement table data sorting and paging functions Sep 20, 2023 am 11:28 AM

PHP development: How to implement table data sorting and paging functions In web development, processing large amounts of data is a common task. For tables that need to display a large amount of data, it is usually necessary to implement data sorting and paging functions to provide a good user experience and optimize system performance. This article will introduce how to use PHP to implement the sorting and paging functions of table data, and give specific code examples. The sorting function implements the sorting function in the table, allowing users to sort in ascending or descending order according to different fields. The following is an implementation form

How to create custom pagination in CakePHP? How to create custom pagination in CakePHP? Jun 04, 2023 am 08:32 AM

CakePHP is a powerful PHP framework that provides developers with many useful tools and features. One of them is pagination, which helps us divide large amounts of data into several pages, making browsing and manipulation easier. By default, CakePHP provides some basic pagination methods, but sometimes you may need to create some custom pagination methods. This article will show you how to create custom pagination in CakePHP. Step 1: Create a custom pagination class First, we need to create a custom pagination class. this

Using JavaScript to implement paging display of table data Using JavaScript to implement paging display of table data Jun 16, 2023 am 10:00 AM

As data continues to grow, tabular display becomes more difficult. Most of the time, the amount of data in a table is so large that it becomes slow to load and users need to constantly browse the page to find the data they want. This article will introduce how to use JavaScript to realize paginated display of table data, making it easier for users to find the data they want. 1. Dynamically create tables. In order to make the paging function more controllable, tables need to be created dynamically. In the HTML page, add a table element similar to the one below.

How to use JavaScript to implement table paging function? How to use JavaScript to implement table paging function? Oct 20, 2023 pm 06:19 PM

How to use JavaScript to implement table paging function? With the development of the Internet, more and more websites use tables to display data. In some cases where the amount of data is large, the data needs to be displayed in pages to improve user experience. This article will introduce how to use JavaScript to implement table paging function and provide specific code examples. 1. HTML structure First, we need to prepare an HTML structure to host tables and paging buttons. We can use &lt;tab

Detailed explanation of the principle of MyBatis paging plug-in Detailed explanation of the principle of MyBatis paging plug-in Feb 22, 2024 pm 03:42 PM

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis

How to use Layui to develop a data display page with paging function How to use Layui to develop a data display page with paging function Oct 24, 2023 pm 01:10 PM

How to use Layui to develop a data display page with paging function Layui is a lightweight front-end UI framework that provides simple and beautiful interface components and a rich interactive experience. During development, we often encounter situations where we need to display large amounts of data and perform paging. The following is an example of a data display page with paging function developed using Layui. First, we need to introduce Layui related files and dependencies. Add the following code to the &lt;head&gt; tag of the html page

Vue component practice: paging component development Vue component practice: paging component development Nov 24, 2023 am 08:56 AM

Vue component practice: Introduction to paging component development In web applications, the paging function is an essential component. A good paging component should be simple and clear in presentation, rich in functions, and easy to integrate and use. In this article, we will introduce how to use the Vue.js framework to develop a highly customizable paging component. We will explain in detail how to develop using Vue components through code examples. Technology stack Vue.js2.xJavaScript (ES6) HTML5 and CSS3 development environment

How to implement paging function in Vue technology development How to implement paging function in Vue technology development Oct 09, 2023 am 09:06 AM

Vue is a popular JavaScript framework for building user interfaces. In the development of Vue technology, implementing paging function is a common requirement. This article will introduce how to use Vue to implement paging function and provide specific code examples. Before we start, we need to prepare some basic knowledge in advance. First, we need to understand the basic concepts and syntax of Vue. Secondly, we need to know how to use Vue components to build our application. Before we start, we need to install a paging plug-in in the Vue project,

See all articles