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. 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:
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
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:
|
<<: How to monitor Windows performance on Zabbix
>>: Detailed example of jQuery's chain programming style
Initialize Data DROP TABLE IF EXISTS `test_01`; C...
Preface mysqlslap is a diagnostic program designe...
This article uses examples to explain the concept...
Static files Nginx is known for its high performa...
A singly linked list can only be traversed from t...
Table of contents 1. Solution 1 (UDF) Demo Case 2...
Docker has been very popular in the past two year...
When a web project gets bigger and bigger, its CS...
Table of contents Preface cause Phenomenon why? A...
1. Install nginx in docker: It is very simple to ...
1. What is scaffolding? 1. Vue CLI Vue CLI is a c...
Table of contents Simple CASEWHEN function: This ...
Regarding how to create this thin-line table, a s...
Since Uniapp does not have DingTalk authorization...
A composite index (also called a joint index) is ...