How to use MySQL limit and solve the problem of large paging

How to use MySQL limit and solve the problem of large paging

Preface

In daily development, when we use MySQL to implement paging, we always use MySQL limit syntax. But how to use it is very particular, let's summarize it today.

limit syntax

The limit syntax supports two parameters, offset and limit. The former indicates the offset, and the latter indicates taking the first limit data.

For example:

## Return the first 10 statements that meet the conditions select * from user limit 10

## Return the 11th to 20th data that meet the conditions select * from user limit 10,20

It can also be seen from the above that limit n is equivalent to limit 0,n.

Performance Analysis

In actual use, we will find that the loading of some pages behind the paging will become slower, that is to say:

select * from user limit 1000000,10

The statement executes slowly. So let's test it first.

First, we take 100 data with a small offset (the total amount of data is about 200). Then we gradually increase the offset.

select * from user limit 0,100 ---------Time consumption 0.03s
select * from user limit 10000,100 ---------Time consumption 0.05s
select * from user limit 100000,100 ---------Time consumption 0.13s
select * from user limit 500000,100 ---------Time consumption 0.23s
select * from user limit 1000000,100 ---------Time consumption 0.50s
select * from user limit 1800000,100 ---------Time consumption 0.98s

It can be seen that as the offset increases, the performance becomes worse and worse.

Why is this? Because the syntax of limit 10000,10 actually means that MySQL finds the first 10010 rows of data and then discards the first 10000 rows. This step is actually a waste.

optimization

Optimize with id

First find the maximum ID of the last paging, and then use the index on the id to query, similar to select * from user where id>1000000 limit 100.
This is very efficient because the primary key is indexed, but it has a disadvantage that the IDs must be consecutive and the query cannot have a where statement because the where statement will filter the data.

Optimizing with covering indexes

When a MySQL query completely hits an index, it is called a covering index, which is very fast because the query only needs to search on the index and then can return directly without going back to the data table to get the data. Therefore, we can first find the index ID and then get the data based on the ID.

select * from (select id from job limit 1000000,100) a left join job b on a.id = b.id;

This took 0.2 seconds.

Summarize

It is indeed difficult to use MySQL to paginate large amounts of data, but there are some ways to optimize it, which requires more testing in combination with business scenarios.
When the user turns to page 10,000, why don't we just return empty? Is that so boring?

Well, that’s all for this article. I hope the content of this article will be of certain reference value to your study or work. Thank you for your support of 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
  • 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
  • A brief discussion on the implementation of MySQL's limit paging optimization solution
  • The impact of limit on query performance in MySQL

<<:  Linux bash: ./xxx: Unable to execute binary file error

>>:  Several common methods of sending requests using axios in React

Recommend

Detailed explanation of MySQL custom functions and stored procedures

Preface This article mainly introduces the releva...

Solve the problem of MySql8.0 checking transaction isolation level error

Table of contents MySql8.0 View transaction isola...

Detailed explanation of Angular parent-child component communication

Table of contents Overview 1. Overview of input a...

Example of how to change the line spacing of HTML table

When using HTML tables, we sometimes need to chan...

In-depth study of MySQL multi-version concurrency control MVCC

MVCC MVCC (Multi-Version Concurrency Control) is ...

HTML code example: detailed explanation of hyperlinks

Hyperlinks are the most frequently used HTML elem...

Analysis of the Principles of MySQL Slow Query Related Parameters

MySQL slow query, whose full name is slow query l...

How to use Docker Swarm to build WordPress

cause I once set up WordPress on Vultr, but for w...

Two ways to implement HTML to randomly drag content positions

Test: Chrome v80.0.3987.122 is normal There are t...

Summary of several error logs about MySQL MHA setup and switching

1: masterha_check_repl replica set error replicat...

Detailed explanation of MySQL data rows and row overflow mechanism

1. What are the formats of lines? You can see you...

Linux's fastest text search tool ripgrep (the best alternative to grep)

Preface Speaking of text search tools, everyone m...