Practical Optimization of MySQL Paging Limit

Practical Optimization of MySQL Paging Limit

Preface

When we use query statements, we often need to return the first few or middle rows of data. What should we do at this time? Don't worry, MySQL already provides such a function for us.

SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset

The LIMIT clause can be used to force a SELECT statement to return a specified number of records. LIMIT accepts one or two numeric arguments. The argument must be an integer constant. If two arguments are given, the first argument specifies the offset of the first returned row, and the second argument specifies the maximum number of returned rows. The initial row offset is 0 (not 1): For compatibility with PostgreSQL, MySQL also supports the syntax: LIMIT # OFFSET #.

Therefore, we usually use limit paging when querying data, because this avoids full table query and improves query efficiency. However, when the amount of data in a table increases, paging query will become slower. Let's take a look at the detailed introduction below.

MySQL Paging Limit Optimization

Create a test table card with 20 million data

mysql> select count(*) from card;
+----------+
| count(*) |
+----------+
| 20000000 |
+----------+
1 row in set (0.00 sec)

-First test the query speed of the first 1000 rows

mysql> select * from card limit 1000,10;
+---------+--------------------------------------+
| card_id | card_number |
+---------+--------------------------------------+
| 1001 | 13fc90a6-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1002 | 13fc923e-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1003 | 13fc93d5-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1004 | 13fc956a-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1005 | 13fc9702-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1006 | 13fc9899-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1007 | 13fc9a31-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1008 | 13fc9bc6-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1009 | 13fc9d5e-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1010 | 13fc9ef5-2e3b-11e8-ae62-9c5c8e6e37cf |
+---------+--------------------------------------+
10 rows in set (0.00 sec)

-Test the query after 1 million

mysql> select * from card limit 1000000,10;
+---------+--------------------------------------+
| card_id | card_number |
+---------+--------------------------------------+
| 1000001 | 2d87021a-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000002 | 2d8703ac-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000003 | 2d87053b-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000004 | 2d8706cd-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000005 | 2d87085f-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000006 | 2d8709f1-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000007 | 2d870b83-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000008 | 2d870d18-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000009 | 2d870eaa-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000010 | 2d871039-2e3b-11e8-ae62-9c5c8e6e37cf |
+---------+--------------------------------------+
10 rows in set (0.18 sec)

-Test the query after 10 million

mysql> select * from card limit 10000000,10;
+----------+--------------------------------------+
| card_id | card_number |
+----------+--------------------------------------+
| 10000001 | b11ad76c-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000002 | b11aefd5-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000003 | b11af868-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000004 | b11b0031-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000005 | b11b07ad-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000006 | b11b0f0f-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000007 | b11b1669-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000008 | b11b1db2-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000009 | b11b24fa-2e49-11e8-ae62-9c5c8e6e37cf |
| 10000010 | b11b2c37-2e49-11e8-ae62-9c5c8e6e37cf |
+----------+--------------------------------------+
10 rows in set (1.29 sec)

It can be seen that the query efficiency will be lower as time goes by. Because when querying data after 1 million, MySQL will first query 1 million and 10 pieces of data, and then intercept the following ten pieces of data. These will cause performance degradation.

So how to avoid scanning 1 million pieces of data? We can clearly know that the primary key after 1 million is greater than 1 million. So we can rewrite the SQL to use the index and reduce the number of rows scanned

mysql> select * from card where card_id>=1000000 limit 10;
+---------+--------------------------------------+
| card_id | card_number |
+---------+--------------------------------------+
| 1000000 | 2d870088-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000001 | 2d87021a-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000002 | 2d8703ac-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000003 | 2d87053b-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000004 | 2d8706cd-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000005 | 2d87085f-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000006 | 2d8709f1-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000007 | 2d870b83-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000008 | 2d870d18-2e3b-11e8-ae62-9c5c8e6e37cf |
| 1000009 | 2d870eaa-2e3b-11e8-ae62-9c5c8e6e37cf |
+---------+--------------------------------------+
10 rows in set (0.00 sec)

This can greatly improve query efficiency

Summarize

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

You may also be interested in:
  • Mysql sorting and paging (order by & limit) and existing pitfalls
  • MySQL uses limit to implement paging example method
  • How to use MySQL limit and solve the problem of large paging
  • A brief discussion on the performance issues of MySQL paging limit
  • 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
  • Detailed explanation of MySQL limit usage and performance analysis of paging query statements
  • Usage and points to note of mysql limit paging
  • Optimization method for MYSQL paging limit speed is too slow
  • Reasons and optimization solutions for slow MySQL limit paging with large offsets

<<:  Implementing license plate input function in WeChat applet

>>:  Coexistence of python2 and python3 under centos7 system

Recommend

Introduction to Kubernetes (k8s)

I had always wanted to learn Kubernetes because i...

Get a list of your top 10 most frequently used terminal commands in Linux

I think the commands I use most often are: Choice...

Three.js realizes Facebook Metaverse 3D dynamic logo effect

Table of contents background What is the Metavers...

mysql 5.7.11 winx64.zip installation and configuration method graphic tutorial

Install and configure the MySql database system. ...

Detailed explanation of Vue's custom event content distribution

1. This is a bit complicated to understand, I hop...

An example of how Vue implements four-level navigation and verification code

Effect: First create five vue interfaces 1.home.v...

Detailed tutorial on installing MYSQL under WINDOWS

1. Download the installation package -Choose the ...

Javascript basics about built-in objects

Table of contents 1. Introduction to built-in obj...

Best Practices Guide for MySQL Partitioned Tables

Preface: Partitioning is a table design pattern. ...

React internationalization react-intl usage

How to achieve internationalization in React? The...

Complete MySQL Learning Notes

Table of contents MyISAM and InnoDB Reasons for p...

Steps to deploy Docker project in IDEA

Now most projects have begun to be deployed on Do...

Vue implements zip file download

This article example shares the specific code of ...

Use Angular CDK to implement a Service pop-up Toast component function

Table of contents 1. Environmental Installation 2...