search
HomeDatabaseMysql TutorialThe meaning and method of paging technology principles and implementation (1)_MySQL

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

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

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

 对比这两种写法,绝大多数的情况下,第一个查询的效率比第二个高得多。
 这是由于CBO优化模式下,Oracle可以将外层的查询条件推到内层查询中,以提高内层查询的执行效率。对于第一个查询语句,第二层的查询条件WHERE 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
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
MySQL's Role: Databases in Web ApplicationsMySQL's Role: Databases in Web ApplicationsApr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

MySQL: Building Your First DatabaseMySQL: Building Your First DatabaseApr 17, 2025 am 12:22 AM

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL: A Beginner-Friendly Approach to Data StorageMySQL: A Beginner-Friendly Approach to Data StorageApr 17, 2025 am 12:21 AM

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.

Is MySQL Beginner-Friendly? Assessing the Learning CurveIs MySQL Beginner-Friendly? Assessing the Learning CurveApr 17, 2025 am 12:19 AM

MySQL is suitable for beginners because: 1) easy to install and configure, 2) rich learning resources, 3) intuitive SQL syntax, 4) powerful tool support. Nevertheless, beginners need to overcome challenges such as database design, query optimization, security management, and data backup.

Is SQL a Programming Language? Clarifying the TerminologyIs SQL a Programming Language? Clarifying the TerminologyApr 17, 2025 am 12:17 AM

Yes,SQLisaprogramminglanguagespecializedfordatamanagement.1)It'sdeclarative,focusingonwhattoachieveratherthanhow.2)SQLisessentialforquerying,inserting,updating,anddeletingdatainrelationaldatabases.3)Whileuser-friendly,itrequiresoptimizationtoavoidper

Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

See all articles

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools