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

Introduction to the use of alt and title attributes of HTML img tags

When browser vendors bend the standards and take i...

CSS3 realizes text relief effect, engraving effect, flame text

To achieve this effect, you must first know a pro...

Windows Server 2016 Quick Start Guide to Deploy Remote Desktop Services

Now 2016 server supports multi-site https service...

Detailed explanation of common MySQL operation commands in Linux terminal

Serve: # chkconfig --list List all system service...

About vue component switching, dynamic components, component caching

Table of contents 1. Component switching method M...

Detailed explanation of the process of installing msf on Linux system

Or write down the installation process yourself! ...

Detailed explanation of Vuex overall case

Table of contents 1. Introduction 2. Advantages 3...

Experience in solving tomcat memory overflow problem

Some time ago, I submitted a product version to t...

Detailed explanation of Linux text processing tools

1. Count the number of users whose default shell ...

MySQL 8.0.13 decompression version installation graphic tutorial under Windows

This article shares with you the MySQL 8.0.13 ins...

Detailed explanation of how to synchronize data from MySQL to Elasticsearch

Table of contents 1. Synchronization Principle 2....

Detailed example of mysql similar to oracle rownum writing

Rownum is a unique way of writing in Oracle. In O...