A brief discussion on the implementation of MySQL's limit paging optimization solution

A brief discussion on the implementation of MySQL's limit paging optimization solution

Mysql limit paging statement usage

Compared with Oracle and MS SqlServer, MySQL's paging method is so simple that it makes people want to cry.

--grammar:

SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset

--Example:

select * from table limit 5; --Return the first 5 rows select * from table limit 0,5; --Same as above, return the first 5 rows select * from table limit 5,10; --Return rows 6-15

How to optimize limit

When a query statement has a large offset, such as select * from table limit 10000,10 , it is best not to use limit directly. Instead, get the offset id first, and then use limit size directly to get the data. The effect will be much better.

like:

select * From customers Where customer_id >=(
select customer_id From customers Order By customer_id limit 10000,1
) limit 10;

1. Test experiment

MySQL paging directly uses limit start, count paging statements:

select * from product limit start, count

When the starting page is small, the query has no performance issues. Let's look at the execution time of paging from 10, 100, 1000, and 10000 (20 records per page), as follows:

select * from product limit 10, 20 0.016 seconds select * from product limit 100, 20 0.016 seconds select * from product limit 1000, 20 0.047 seconds select * from product limit 10000, 20 0.094 seconds

We have seen that as the starting record increases, the time also increases, which shows that the paging statement limit is closely related to the starting page number.
Then we change the starting record to 40w (about half of the record)

select * from product limit 400000, 20 3.229 seconds

Let's look at the time when we took the last page of records.

select * from product limit 866613, 20 37.44 seconds

For a page with the largest pagination number like this, this time is obviously unbearable.

We can also conclude two things from this:

  • The query time of the limit statement is proportional to the position of the starting record.
  • The MySQL limit statement is very convenient, but it is not suitable for direct use for tables with many records.

2. Performance optimization method for limit paging problem

2.1 Using the table's covering index to speed up paging queries

We all know that if the statement that uses an index query only contains that index column (covering index), the query will be very fast.

Because index search has an optimized algorithm and the data is on the query index, there is no need to look for the relevant data address, which saves a lot of time.

In addition, MySQL also has related index cache, and the effect will be better if the cache is used when the concurrency is high.

In our example, we know that the id field is the primary key, so the default primary key index is included. Now let's see how the query using the covering index performs:
This time we query the data of the last page (using a covering index, which only contains the id column), as follows:

select id from product limit 866613, 20

The query time is 0.2 seconds, which is about 100 times faster than the 37.44 seconds required to query all columns.

If we also want to query all columns, there are two ways.

id>= format:

SELECT * FROM product 
WHERE ID > =(select id from product limit 866613, 1) limit 20

The query time is 0.2 seconds, which is a qualitative leap.

Using join

SELECT * FROM product a 
JOIN (select id from product limit 866613, 20) b ON a.ID = b.id

The query time is also very short, great!

In fact, both use the same principle, so the effects are similar.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • MySQL query optimization: LIMIT 1 avoids full table scan and improves query efficiency
  • Why does MySQL paging become slower and slower when using limit?
  • MySQL optimization query_cache_limit parameter description
  • Detailed explanation of the pitfalls of mixing MySQL order by and limit
  • Simple example of limit parameter of mysql paging
  • Reasons and optimization solutions for slow MySQL limit paging with large offsets
  • Mysql sorting and paging (order by & limit) and existing pitfalls
  • How to use MySQL limit and solve the problem of large paging
  • Detailed explanation of the problem of mixed use of limit and sum functions in MySQL
  • How to improve MySQL Limit query performance
  • Detailed explanation of MySQL Limit performance optimization and paging data performance optimization
  • The impact of limit on query performance in MySQL

<<:  Analysis of Hyper-V installation CentOS 8 problem

>>:  Several ways to introduce pictures in react projects

Recommend

How to display a small icon in front of the browser URL

When you browse many websites, you will find that ...

A brief discussion on the VUE uni-app development environment

Table of contents 1. Through HBuilderX visual int...

How to start jar package and run it in the background in Linux

The Linux command to run the jar package is as fo...

Problems and solutions when installing MySQL8.0.13 on Win10 system

Operating system: Window10 MySQL version: 8.0.13-...

v-for directive in vue completes list rendering

Table of contents 1. List traversal 2. The role o...

HTML+CSS+JS to implement the Don't Step on the Whiteboard game

Table of contents Background 1. Thought Analysis ...

Double loading issue when the page contains img src

<br />When the page contains <img src=&qu...

Detailed steps to install the specified version of docker (1.12.6) using rpm

1. Reasons If the system is Centos7.3, the Docker...

11 Reasons Why Bootstrap Is So Popular

Preface Bootstrap, the most popular front-end dev...

Detailed explanation of how to install PHP curl extension under Linux

This article describes how to install the PHP cur...

React implements dynamic pop-up window component

When we write some UI components, if we don't...