First, let me explain the version of MySQL: mysql> select version(); +-----------+ | version() | +-----------+ | 5.7.17 | +-----------+ 1 row in set (0.00 sec) Table structure: mysql> desc test; +--------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------------+------+-----+---------+----------------+ | id | bigint(20) unsigned | NO | PRI | NULL | auto_increment | | val | int(10) unsigned | NO | MUL | 0 | | | source | int(10) unsigned | NO | | 0 | | +--------+---------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) id is the auto-increment primary key and val is a non-unique index. Pour in a large amount of data, a total of 5 million: mysql> select count(*) from test; +----------+ | count(*) | +----------+ |5242882| +----------+ 1 row in set (4.25 sec) We know that when mysql> select * from test where val=4 limit 300000,5; +---------+-----+--------+ | id | val | source | +---------+-----+--------+ | 3327622 | 4 | 4 | | 3327632 | 4 | 4 | | 3327642 | 4 | 4 | | 3327652 | 4 | 4 | | 3327662 | 4 | 4 | +---------+-----+--------+ 5 rows in set (15.98 sec) In order to achieve the same purpose, we usually rewrite it as follows: mysql> select * from test a inner join (select id from test where val=4 limit 300000,5) b on a.id=b.id; +---------+-----+--------+---------+ | id | val | source | id | +---------+-----+--------+---------+ | 3327622 | 4 | 4 | 3327622 | | 3327632 | 4 | 4 | 3327632 | | 3327642 | 4 | 4 | 3327642 | | 3327652 | 4 | 4 | 3327652 | | 3327662 | 4 | 4 | 3327662 | +---------+-----+--------+---------+ 5 rows in set (0.38 sec) The time difference is obvious. Why does the above result appear? Let's take a look at the query process of The index leaf node data is queried. Similar to the following picture: As shown above, you need to query the index node Someone will definitely ask: Since the index is used at the beginning, why not first query along the index leaf nodes to the last 5 nodes required, and then query the actual data in the clustered index. This only requires 5 random I/Os, similar to the process in the following picture: Confirmed: Let's actually do some operations to confirm the above inference: In order to prove I can only confirm this indirectly: select * from test where val=4 limit 300000,5 mysql> select index_name,count(*) from information_schema.INNODB_BUFFER_PAGE where INDEX_NAME in('val','primary') and TABLE_NAME like '%test%' group by index_name; Empty set (0.04 sec) It can be seen that there is currently no data page about the test table in mysql> select * from test where val=4 limit 300000,5; +---------+-----+--------+ | id | val | source | +---------+-----+--------+ | 3327622 | 4 | 4 | | 3327632 | 4 | 4 | | 3327642 | 4 | 4 | | 3327652 | 4 | 4 | | 3327662 | 4 | 4 | +---------+-----+--------+ 5 rows in set (26.19 sec) mysql> select index_name,count(*) from information_schema.INNODB_BUFFER_PAGE where INDEX_NAME in('val','primary') and TABLE_NAME like '%test%' group by index_name; +------------+----------+ | index_name | count(*) | +------------+----------+ | PRIMARY | 4098 | | val | 208 | +------------+----------+ 2 rows in set (0.04 sec) It can be seen that at this time there are mysqladmin shutdown /usr/local/bin/mysqld_safe & mysql> select index_name,count(*) from information_schema.INNODB_BUFFER_PAGE where INDEX_NAME in('val','primary') and TABLE_NAME like '%test%' group by index_name; Empty set (0.03 sec) Run sql: mysql> select * from test a inner join (select id from test where val=4 limit 300000,5) b on a.id=b.id; +---------+-----+--------+---------+ | id | val | source | id | +---------+-----+--------+---------+ | 3327622 | 4 | 4 | 3327622 | | 3327632 | 4 | 4 | 3327632 | | 3327642 | 4 | 4 | 3327642 | | 3327652 | 4 | 4 | 3327652 | | 3327662 | 4 | 4 | 3327662 | +---------+-----+--------+---------+ 5 rows in set (0.09 sec) mysql> select index_name,count(*) from information_schema.INNODB_BUFFER_PAGE where INDEX_NAME in('val','primary') and TABLE_NAME like '%test%' group by index_name; +------------+----------+ | index_name | count(*) | +------------+----------+ | PRIMARY | 5 | | val | 390 | +------------+----------+ 2 rows in set (0.03 sec) We can clearly see the difference between the two: the first sql loads 4098 data pages into And this will cause a problem: loading a lot of data pages that are not very hot into Problems encountered: To ensure that This concludes this article on why using limit in MySQL affects performance. For more information on the performance impact of using limit in MySQL, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: zabbix custom monitoring nginx status implementation process
>>: Summary of events that browsers can register
1. Construction components 1. A form must contain...
The computer system has been reinstalled, and the...
<br />Based on the original width-and-height...
Table of contents 01 What is Kubernetes? 02 The d...
Vue3.0 has been out for a while, and it is necess...
Table of contents Create a new user Authorize new...
title XML/HTML CodeCopy content to clipboard <...
This article shares the specific code of jQuery t...
Table of contents Constructor new Operator Implem...
introduction In recent years, the call for TypeSc...
Table of contents MySQL Client/Server Protocol If...
Table of contents Preface The relationship betwee...
Start the centos8 virtual machine and press the u...
One environment Alibaba Cloud Server: CentOS 7.4 ...
When I was working on a project recently, I found...