Optimizing the performance of paging query for MySQL with tens of millions of data

Optimizing the performance of paging query for MySQL with tens of millions of data

When the amount of data in MySQL is large, limit paging is used. As the page number increases, the query efficiency decreases.

experiment

1. Directly use the limit start, count paging statement:

select * from order 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 order limit 10, 20 0.016 seconds select * from order limit 100, 20 0.016 seconds select * from order limit 1000, 20 0.047 seconds select * from order limit 10000, 20 0.094 seconds

We have seen that as the number of starting records increases, the time also increases. This shows that the paging statement limit is closely related to the starting page number. So let's change the starting record to 40w and see.

select * from order limit 400000, 20 3.229秒

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

select * from order limit 800000, 20 37.44秒

Obviously this time is unbearable.

We can also conclude two things from this:

1) The query time of the limit statement is proportional to the position of the starting record

2) 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

Use 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 order limit 800000, 20 0.2秒

Compared to the 37.44 seconds it takes to query all columns, the speed is increased by more than 100 times.

If we also want to query all columns, there are two ways, one is id>= form, the other is to use join, see the actual situation:

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

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

Another way to write

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

The query time is also very short

You may also be interested in:
  • MySQL paging query optimization techniques
  • MySQL optimization tutorial: large paging query
  • MySQL paging query method for millions of data volumes and its optimization suggestions
  • MySQL million-level data paging query optimization solution
  • Detailed explanation of Mysql Limit paging query optimization
  • Implementation of MySQL large page query optimization for millions of data

<<:  Detailed explanation of the adaptive adaptation problem of Vue mobile terminal

>>:  Detailed explanation of system input and output management in Linux

Recommend

Sample code for implementing Alipay sandbox payment with Vue+SpringBoot

First, download a series of things from the Alipa...

Detailed steps to install Hadoop cluster under Linux

Table of contents 1. Create a Hadoop directory in...

Example of how to identify the user using a linux Bash script

It is often necessary to run commands with sudo i...

JS realizes the front-end paging effect

This article example shares the specific code of ...

The process of installing and configuring nginx in win10

1. Introduction Nginx is a free, open source, hig...

How to use provide to implement state management in Vue3

Table of contents Preface How to implement Vuex f...

Summary of the use of vue Watch and Computed

Table of contents 01. Listener watch (1) Function...

css Get all elements starting from the nth one

The specific code is as follows: <div id="...

Element sample code to implement dynamic table

Table of contents 【Code background】 【Code Impleme...

How to manage cached pages in Vue

Table of contents Problem 1: Destruction 1. How t...

Detailed explanation of bash command usage

On Linux, bash is adopted as the standard, which ...

Table paging function implemented by Vue2.0+ElementUI+PageHelper

Preface I have been working on some front-end pro...

HTML marquee tag usage examples

This tag is not part of HTML3.2 and only supports ...