Home Database Mysql Tutorial Mysql paging performance exploration

Mysql paging performance exploration

Dec 08, 2017 am 11:52 AM
mysql Pagination explore

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;
Copy after login

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,//当前页索引
}
Copy after login

Then you need to return the total number of queries to the front end;

SELECTCOUNT(*)FROMmy_tableWHEREx= y ORDERBYid;
Copy after login

Then return the number of items on the specified page To the front-end:

SELECT*FROMmy_tableWHEREx= y ORDERBYdate_colLIMIT (pageIndex - 1)* pageSize, pageSize;
Copy after login

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,//总条数
}
Copy after login

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
Copy after login

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);
Copy after login

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//能用上索引
Copy after login

意思是说order by 要避免使用文件系统排序,要么把order by的字段出现在select后,要么使用order by字段出现在where 条件里,要么把order by字段与where条件字段建立复合索引!

第二条sql就是巧妙的利用第二种方式利用上了索引。 select id from a order bysubject_code,这种方式

count优化

当数据量非常大时,其实可以输出总数的大概数据,利用explain语句,他并没有真正去执行sql,而是进行的估算。

相关推荐:

MySQL分页性能优化指南

php mysql分页类(php新手入门)

php+mysql分页代码详解_PHP教程

The above is the detailed content of Mysql paging performance exploration. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL'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

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

See all articles