Sharing of SQL optimization experience when offset is too large during MySQL paging

Sharing of SQL optimization experience when offset is too large during MySQL paging

Find the problem

When we display the contents in a list, we will inevitably encounter paging problems, because the number of contents in the list may be large, but the size of the interface that the user can see at one time is limited. It is impossible to display all the contents in one interface. Fetching too much data from the backend at one time will also cause additional pressure on the backend.

Usually the following statement is used for paging query:

SELECT
*
FROM table
where condition1 = 0
and condition2 = 0
and condition3 = -1
and condition4 = -1
order by id asc
LIMIT 2000 OFFSET 50000

When the offset is particularly large, the execution efficiency of this statement will be significantly reduced, and the efficiency decreases as the offset increases.

The reasons are:

MySQL does not skip the offset row, but takes the offset+N rows, then returns the previous offset row and returns N rows. When the offset is particularly large and the single data is also large, the more data needs to be obtained each time the query is made, the slower it will be.

Optimization plan:

SELECT
*
FROM table
JOIN
(select id from table
where condition1 = 0
and condition2 = 0
and condition3 = -1
and condition4 = -1
order by id asc
LIMIT 2000 OFFSET 50000)
as tmp using(id)

or

SELECT a.* FROM table a, 
(select id from table
where condition1 = 0
and condition2 = 0
and condition3 = -1
and condition4 = -1
order by id asc
LIMIT 2000 OFFSET 50000) b 
where a.id = b.id

First obtain the primary key list, and then query the target data by the primary key. Even if the offset is large, many primary keys are obtained instead of all field data. Relatively speaking, the efficiency will be greatly improved.

Summarize

The above is the full content of this article. I hope that the content of this article can bring some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • How to use MySQL limit and solve the problem of large paging
  • A simple paging optimization method for MySQL in the case of big data
  • MySQL million-level paging optimization (MySQL ten million-level fast paging)
  • Optimization method for MYSQL paging limit speed is too slow
  • MySQL limit paging optimization method sharing
  • MySQL million-level data paging query optimization solution
  • Optimize Mysql limit, reference the compound index of fast paging from one million to ten million and apply it to lightweight framework
  • MySQL paging optimization analysis
  • Optimizing the performance of paging query for MySQL with tens of millions of data
  • MySQL optimization tutorial: large paging query

<<:  How to recover accidentally deleted messages files in Linux

>>:  Detailed explanation of the pitfalls of add_header in nginx configuration tutorial

Recommend

Summary of various forms of applying CSS styles in web pages

1. Inline style, placed in <body></body&g...

Detailed explanation of react setState

Table of contents Is setState synchronous or asyn...

Example of how to implement underline effects using Css and JS

This article mainly describes two kinds of underl...

Vue implements the shake function (compatible with ios13.3 and above)

Recently, I made a function similar to shake, usi...

Detailed tutorial for downloading, installing and configuring MySQL 5.7.27

Table of contents 1. Download steps 2. Configure ...

Hide HTML elements through display or visibility

Sometimes we need to control whether HTML elements...

Detailed explanation of how to create an updateable view in MySQL

This article uses an example to describe how to c...

Detailed explanation of primary keys and transactions in MySQL

Table of contents 1. Comments on MySQL primary ke...

Install zip and unzip command functions under Linux and CentOS (server)

Install zip decompression function under Linux Th...

Detailed steps for installing and using vmware esxi6.5

Table of contents Introduction Architecture Advan...

A collection of information about forms and form submission operations in HTML

Here we introduce the knowledge about form elemen...