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 convert MySQL horizontally to vertically and vertically to horizontally

Initialize Data DROP TABLE IF EXISTS `test_01`; C...

MySQL index cardinality concept and usage examples

This article uses examples to explain the concept...

JavaScript data structure bidirectional linked list

A singly linked list can only be traversed from t...

A brief discussion on the synchronization solution between MySQL and redis cache

Table of contents 1. Solution 1 (UDF) Demo Case 2...

Summary of 7 reasons why Docker is not suitable for deploying databases

Docker has been very popular in the past two year...

You really need to understand the use of CSS variables var()

When a web project gets bigger and bigger, its CS...

MySQL should never write update statements like this

Table of contents Preface cause Phenomenon why? A...

Detailed explanation of the process of modifying Nginx files in centos7 docker

1. Install nginx in docker: It is very simple to ...

Vue scaffolding learning project creation method

1. What is scaffolding? 1. Vue CLI Vue CLI is a c...

This article will show you how to use SQL CASE WHEN in detail

Table of contents Simple CASEWHEN function: This ...

A simple example of creating a thin line table in html

Regarding how to create this thin-line table, a s...

Uniapp implements DingTalk scan code login sample code

Since Uniapp does not have DingTalk authorization...

In-depth study of MySQL composite index

A composite index (also called a joint index) is ...