Analysis of the Poor Performance Caused by Large Offset of LIMIT in MySQL Query

Analysis of the Poor Performance Caused by Large Offset of LIMIT in MySQL Query

Preface

We all know that MySQL query uses the select command, and with the limit and offset parameters, it can read records in a specified range. However, the reason why the offset is too large affects the query performance and the optimization method

We inevitably need paging in our business systems. When you think of paging, you will definitely think of using LIMIT in SQL to implement it. However, incorrect use of LIMIT can lead to performance issues (SQL execution is slow and may bring down the server) and may be criticized by your supervisor; so let's take a look at how to use LIMIT correctly.

Let’s take a look at the detailed introduction.

LIMIT OFFSET, ROW_COUNT to implement paging

Ways to have performance issues

SELECT * FROM myTable ORDER BY `id` LIMIT 1000000, 30

The person who wrote this SQL statement must have thought: the MySQL database will directly locate the 1,000,000th number that meets the conditions, and then fetch 30 pieces of data.
However, this is not how MySQL actually works.

LIMIT 1000000, 30 means: scan 1000030 rows that meet the condition, discard the first 1000000 rows, and then return the last 30 rows.

A better way

SELECT t.*
FROM (
  SELECT id
  FROM myTable
  ORDER BY
    id
  LIMIT 1000000, 30
  ) q
JOIN myTable t
ON t.id = q.id

The general principle is:

  • The subquery only uses the index column and does not fetch actual data, so it does not involve disk IO. Therefore, even with a relatively large offset, the query speed will not be too slow.

Friends who are interested in the specific principle analysis can read this article: MySQL ORDER BY / LIMIT performance: late row lookups

postscript

To be continued.

References

  • Why does MYSQL higher LIMIT offset slow the query down?
  • MySQL ORDER BY / LIMIT performance: late row lookups

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:
  • Solution to data duplication when using limit+order by in MySql paging
  • 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
  • Reasons and optimization solutions for slow MySQL limit paging with large offsets
  • 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
  • MySQL limit performance analysis and optimization
  • Why does using limit in MySQL affect performance?

<<:  How to monitor Windows performance on Zabbix

>>:  Detailed example of jQuery's chain programming style

Recommend

How to enable remote access permissions in MYSQL

1. Log in to MySQL database mysql -u root -p View...

Implementation of vue-nuxt login authentication

Table of contents introduce Link start Continue t...

How to use Nginx to handle cross-domain Vue development environment

1. Demand The local test domain name is the same ...

Detailed explanation of the functions of each port of Tomcat

From the tomcat configuration file, we can see th...

Solution to the CSS height collapse problem

1. High degree of collapse In the document flow, ...

Detailed explanation of viewing and setting SQL Mode in MySQL

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

How to use CSS to achieve two columns fixed in the middle and adaptive

1. Use absolute positioning and margin The princi...

Example of horizontal arrangement of li tags in HTMl

Most navigation bars are arranged horizontally as...

Getting Started Tutorial for Beginners ⑨: How to Build a Portal Website

Moreover, an article website built with a blog pro...

JavaScript object built-in objects, value types and reference types explained

Table of contents Object Object Definition Iterat...

Introduction to the use of common Dockerfile commands

Table of contents 01 CMD 02 ENTRYPOINT 03 WORKDIR...

MySQL 5.7.17 installation graphic tutorial (windows)

I recently started learning database, and I feel ...

Perfect solution to the problem of webpack packaging css background image path

Inside the style tag of the vue component, there ...

Understand the principles and applications of JSONP in one article

Table of contents What is JSONP JSONP Principle J...