Paging is often used in our programming. This article will lead everyone to discuss the performance of mysql paging, hoping to help everyone.
Several common paging methods:
1. Escalator method

The escalator method usually only provides two modes of previous page/next page in navigation. Some products do not even provide the previous page function, only provide a "more/more" method, and there are also drop-down automatic loading. More methods can technically be summarized as escalator methods.
The escalator method is relatively simple and efficient in terms of technical implementation. Just get one page further based on the offset of the last item on the current page. Written as SQL it may be similar to
SELECT*FROMLIST_TABLEWHEREid> offset_id LIMIT n;
1. Elevator method

Another data acquisition method is reflected in the product as a precise page turning method, such as 1,2,3...n. At the same time, the user can also input directly to n pages in the navigation. Most scenes in China use elevators, but the technical implementation cost of elevators is relatively high.
In MySQL, the b-tree usually mentioned is usually b+tree in terms of storage engine implementation.
When using the elevator method, when the user specifies to turn to the nth page, there is no direct method to address the location. Instead, it needs to count one by one from the first floor and scan to count*page to obtain the data. It has just started, so the efficiency is not high.
Traditional paging technology (elevator method)
First, the front end needs to pass you the paging entity and query conditions
//分页实体
structFinanceDcPage{
1:i32 pageSize,//页容量
2:i32 pageIndex,//当前页索引
}
Then you need to return the total number of queries to the front end;
SELECTCOUNT(*)FROMmy_tableWHEREx= y ORDERBYid;
Then return the number of items on the specified page To the front-end:
SELECT*FROMmy_tableWHEREx= y ORDERBYdate_colLIMIT (pageIndex - 1)* pageSize, pageSize;
The results queried from the above two sql statements need to be returned to the front-end paging entity and the single-page result set
//分页实体
structFinanceDcPage{
1:i32 pageSize,//页容量
2:i32 pageIndex,//当前页索引
3:i32 pageTotal,//总页数
4:i32 totalRecod,//总条数
}
In the traditional query method, only the pageIndex value changes with each request, which is the offset of limit offset, num
For example, limit 0,10; limit 10, 10; …. limit10000,10;
The above changes will cause a deviation in the execution time of each query. The larger the offset value, the longer it takes. For example, limit10000,10 needs to read 10010 pieces of data. Only then can you get the 10 pieces of data you want.
Optimization method
In the traditional method, we learned that the key to efficiency is that the program traverses a lot of unnecessary data and finds the key Click So start here.
If there is no need to use the elevator, we can use the escalator to improve performance.
But in most cases, the elevator form can better meet the needs of users, so we need to find another way to optimize the elevator form.
Optimization based on traditional methods
The optimization methods mentioned above are either difficult to meet the needs of users or too complicated to implement, so If the amount of data is not particularly large, such as millions of pieces of data, there is actually no need to use the above optimization method.
The traditional method is sufficient, but the traditional method may also need optimization. For example:
orderby optimization
SELECT*FROMpa_dc_flowORDERBYsubject_codeDESCLIMIT100000,5
The ORDERBY keyword is used in this statement , then what to sort is very important. If you are sorting auto-incrementing IDs, then this statement does not need to be optimized. If it is an index or even a non-index, then it needs to be optimized.
First of all, you have to make sure it is an index, otherwise it will be really slow. Then if it is an index, but it is not as ordered as an auto-incrementing ID, then it must be rewritten as the following statement.
SELECT*FROMpa_dc_flowINNERJOIN(SELECTidFROMpa_dc_flowORDERBYsubject_codeDESCLIMIT100000,5)ASpa_dc_flow_idUSING(id);
The following is the EXPLAIN for two sql


We can see from the picture that the second sql can scan many fewer pages.
In fact, this involves the optimization issue of order by. The subject_code index is not used in the first sql. If you select subject_code instead... the index is used. The following is the optimization of order by.
order by后的字段,如果要走索引,须与where 条件里的某字段建立复合索引!!或者说orcerby后的字段如果要走索引排序,它要么与where条件里的字段建立复合索引【这里建立复合索引的时候,需要注意复合索引的列顺序为(where字段,order by字段),这样才能满足最左列原则,原因可能是order by字段并能算在where 查询条件中!】,要么它自身要在where条件里被引用到!
表asubject_code为普通字段,上面建有索引,id是自增主键
select*fromaorderbysubject_code//用不上索引 selectidfromaorderbysubject_code//能用上索引 selectsubject_codefromaorderbysubject_code//能用上索引 select*fromawheresubject_code= XX orderbysubject_code//能用上索引
意思是说order by 要避免使用文件系统排序,要么把order by的字段出现在select后,要么使用order by字段出现在where 条件里,要么把order by字段与where条件字段建立复合索引!
第二条sql就是巧妙的利用第二种方式利用上了索引。 select id from a order bysubject_code,这种方式
count优化
当数据量非常大时,其实可以输出总数的大概数据,利用explain语句,他并没有真正去执行sql,而是进行的估算。
相关推荐:
The above is the detailed content of Mysql paging performance exploration. For more information, please follow other related articles on the PHP Chinese website!
How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AMMySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.
MySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AMThe MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.
Real-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AMMySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.
SQL Commands in MySQL: Practical ExamplesApr 14, 2025 am 12:09 AMSQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.
How does InnoDB handle ACID compliance?Apr 14, 2025 am 12:03 AMInnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.
MySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AMMySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen
MySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AMMySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.
What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AMInnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools






