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

How to use environment variables in nginx configuration file

Preface Nginx is an HTTP server designed for perf...

How to set npm to load packages from multiple package sources at the same time

Table of contents 1. Build local storage 2. Creat...

js method to delete a field in an object

This article mainly introduces the implementation...

An example of implementing a simple infinite loop scrolling animation in Vue

This article mainly introduces an example of Vue ...

A brief discussion on the underlying principle of mysql join

Table of contents join algorithm The difference b...

Summary of Common Problems with Mysql Indexes

Q1: What indexes does the database have? What are...

Detailed explanation of Linux commands and file search

1. Perform file name search which (search for ...

A universal nginx interface to implement reverse proxy configuration

1. What is a proxy server? Proxy server, when the...

Excel export always fails in docker environment

Excel export always fails in the docker environme...

Introduction and usage of Angular pipeline PIPE

Preface PIPE, translated as pipeline. Angular pip...

Detailed explanation of viewing and setting SQL Mode in MySQL

Viewing and Setting SQL Mode in MySQL MySQL can r...

CSS code abbreviation div+css layout code abbreviation specification

Using abbreviations can help reduce the size of yo...

Gearman + MySQL to achieve persistence operation example

This article uses the gearman+mysql method to imp...

Detailed explanation of CSS sticky positioning position: sticky problem pit

Preface: position:sticky is a new attribute of CS...