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
Table of contents 1. Knowledge description of the...
This article shares the installation of MySQL 5.7...
MySQL has the following logs: Error log: -log-err...
1. Page requirements 1) Use standard headers and ...
1. Add Maria source vi /etc/yum.repos.d/MariaDB.r...
Every website usually encounters many non-search ...
Implemented according to the online tutorial. zab...
I will explain the installation of MySQL under Wi...
Find the problem After upgrading MySQL to MySQL 5...
When changing the time zone under Linux, it is al...
Today, when installing nginx on the cloud server,...
ElementUI implements the table list paging effect...
How to use CSS to control the arc movement of ele...
####Management of input and output in the system#...
In life, the Internet is everywhere. We can play ...