一,前言
首先说明一下MySQL的版本:
mysql> select version();
+-----------+
| version() |
+--------J { / N 3 0 V---+
| 5.7.17 |
+-----------+
1 row in set (0.00 sec)mysql> select version();+-----------+| version()b i r D |+-----------+| 5.7.17 |+-----------+1 row in set (0.00 sec)
表结构:
mysql> desc test;
+--------+----- ^ D X c Y Y S--$ w 8 6 ! b % s i--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--s u : . v n k ] W----0 l p 0 .--+---------------------+------+-----+---------+----------------+
|y L U = R @ Q 5 id | bigint(20) unsig5 L ( } - M ) # gned | NO7 w t | PRI | NULL | auto_increment |
| val | int(10) u! 5 @ X g )nsigned | NO N R B X k * 9 C | MUL | 0 | |
| source | int(10) unsigned | NO | | 0 | |
+--------+---------------------+------+-----+m H . = E 8---------+----------------+
3 rows in set (0.00 sec)
i= b 7 %d为自增主键,val为非唯一索引。
灌入大量数据c _ Q O 8,共5P B u # _ L I r t00万@ 1 S q a:
mysql> select count(*) from test;
+---------Y P v r _ - 4-+
| count(*) |
+-d N C / W e f 6 z---------u r V $ ^ 7 { ++
| 5242882 |
+----------+
1 row in set (4.25 sec)
我们知道,当limit offr e ~ Nset rows中的offset很大时,会出现效率问题:
mysql> sel S B l @ q M 6ef Z : e * 1 o [ `ct * frv b y p f ~om test where val=4 limit 300000,5;
+w 8 z M 9 h #---------+-----+--------@ k % o Z ( q D+
| id |L - M e L ] o val | source |
+---------+-----+--------G = - / ~ v+
| 3327622 | 4 | 4 |
| 3327632 | 4 | 4 |
| 3327642 | 4 | 4 |
| 3327652 | 4 | 4 |
| 3327662 |s ! 7 f 4 | 4 |
+---------+-----+--------+
5 rows in5 K y B h b 5 ~ 4 s. Q | B T ret (15.98 sec)
为了达到相同的目的,我们一般会I d t C改写( X a G ( 7成如下y Y Z d -语句R N c N ( { c:
mysql> select * from test a in% F [ m 1 - J Oner join (select id from test where val=4 limit 300000,5) b on a.id=b.id;
+---------+-----+---H 7 : F q-----+---------+
| id | val | source | id |
+-d Q V U 8--------+---j / p F i %--+--------+---------+
| 3327622 | 4 | 4 | 3327622 |
| 3327632 | 4 | 4 | 3327632 |
| 3327642 | 4 | 4 | 3327= u + [ ! - 6 e642 |
| 3327652 | 4 | 4 | 3327652 |
| 3327662 | 4 | 4 | 3327662 5 w 2 ] ] i = 2 |
+-------` O D 0 g * H o w--+-----+--------+H & | ! * - H &---------+
5 rows in set (0.38 sec)
时间相差很明显。
为什么会出现上面的结果?M Q # b !我们看一下select * from test where val=4 limit 300000,5;的查询过程:
- 查询到索引叶子~ Z v o节点数据。
- 根据叶子节点上的主键值去聚簇索引上查询需要的全部字段值。
类似于下面这张图:
image
像上面这样,需要查询300005次索引节点,查询300005次聚簇N [ v 1索引的数据,最后再将结果过滤掉前300000条,取出最后5条。MySQL耗费了大量随机I/O在查询聚簇索引的数据上,而有300000次随机I/O查询到的数据是不会出^ ` P 8 ! Y l V现在结果集当中的。
肯定会有人问:既然一开始是利用索引的,为什么不先沿着索引叶子节点查询到最后需要的5个节点,然后再去聚x ^ x l ~ K ?簇索引中查询实际数据。这样只需要5次随机I/O,类似于下面图片的过程:
image
其实我也想O | / & N X &问这个问题。
证实
下面我们实际操作一下来证实上述的推论:
为了证实select * from teO I | V -st where val=4 limit 300000,5是扫描300005个索引节点和3H R i f X k P +00005个聚簇索引上的数据节点,d g % U 5我们需要知道MySQL有没有办法统计在一个sql中通过索引节点查询# Z $ C d G 2 !数据节点的次数。我先试了Handler_read_*系列,很遗憾没有一个变量能满足条件。
我, 5 W / t A ] 只能通过间接的方式来证实:
InnoDB中有buffer3 K D J e pool。里面d _ N g r t 存有最近访问过的数据页,包括* X & / 7 a数据页和索引页。所以我们需要运行两个sql,d Q q e p T 2来比较buffer pool中的数据页的数量。预测结果是运行select * from test a inner join (select id from test where val=4 limit 300000,5) b>之后,buffer pool中的数据页的数量远远少于select * from test where val=4 limit 300000,5;对应的数量,因为前一个sql只访问5次数据页,而后一, W P A C 9 y个sql访问300005次数据页。
mysql> sea V ` m ` ? , nlect * from test a inner join (sel. L ?ect id from test where val=4 limit 300000,5) b on a.id=b.id;+---------+-----+--------+---------+| id | val | source | id! Z z j a ~ n |+---------+-----+--------+---------+| 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)
可以看出,目前buffer pool中没有关于test表的数据页。
mysqp c v F ; s o nl> select * from test where val=4 limit 300000,5;
+---c S ^ C P------+-----+--------m X . 6 0+
| id | val | s7 g Z c rource |
+---/ L L = N + 6------+-----+-----I W | C ^---+
| 3327622 | 4 | 4 |
| 3327632 | 4 | 4 |
| 3327642 | 4 | 4 |
| 33E x F R t @ O # N27652 | 4 | 4 |
| 3327662 | 4 | 4 |
+---------+-----+--------+
5 rows in set (2i R -6.19 sec)
m: 5 E =ysql> select index_name,count(*) from information_schema.% F cINNODB_BUFFER_PAGE where INDEX_NAME in(\'val\',\'primary\') and TABLE_NAME like \'%test%\' group by index_name;
+------------+---------L B O H ` $ %-+
| index_name | c . 5 M [ } 2oua ^ Z 0nt(*) |
+------------+----------+
| PRIMARY | 4098 |
| val | 208 |
+------------+----------+
2 roww 9 u y H k Ws in set (0.04 sec)
可以看出,此时buffer pool中关于test表有4098个数据页,208个索引页。
`selV X P ] ? Sect * from test a inn+ y T t 4er join (sell [ W t 2ect id from test wW . b 3 Vhere val=4 limit 300000,5)`` b>为了防止上次试验的影响,我们需要清空bufy 7 9 {fer pool,重启mysC X b :ql。
mysqlai D j K 7 m Jdmin shutdown
/usr/local/bin/mysqld_safe &
mysql> select ini 9 ) | * ~ & V :dex_namev x f z ,,count(*) from informat[ : X - ^ ;ion_schema.INNODB_BUFFER_PAGE where INDEX_NAME in(\'val\',\'primary\') and TABLE_NAME like \'%t~ 9 u ] k d Rest%\' group by index_name;
Empty set (0.03 sec)
运行sql:
mysql> select * fro* P u ^ hm test a inner join (select id frop - 8 _ o 6 z am test w) % E c S ( | # ahere val=4 limit 300000,5y h ; w k 6 8) b on a.id=b.id;
+---------+-----+--------+-------p k 6 U ? A--+
| id | val | source | id |
+---------+-----+--------+-----1 x % y----+
| 3327622 | 4 | 4 | 3327622 |
| 3327632 | 4 | 4 | 3327632 |
| 3327642 | 4 | 4 | 3327642 |
| 3327652 | 4 | 4 | 3327652 |
| 3327662 | 4 | 4 | 33276Q o D % [62 |
+---------+-----+--------+---------+
5 rows. - ` R : in set (0.09 sec)
mysql> select index_name,count(*) from information_schema.INNODB_BUFFER_y M _ a J [ ePAGE where INDEX_NAME in(\'val\',\'primary\') and TABLE_NAME like \'%test%\' group by index_name;
+------------+------P R y D 1 - M 2 #----+
| index_name | coun, ^ P O p g / = $t(*) |
+---------k ? ] 5 { W p---+----------+
| PRIMARY | 5 |
| val | 390 |
+------------+----------+
2 rows in set (0.03 sec)
我们可以看明显的看出两者的差别:第一个sql加载了4098个数据页到buffer pool,而第二个sql只加载了5个数据页F @ 3 ` ( 0到buffer pool。符合我们的预测。也证实了为什么第一个s} d w / e Q a Hql会慢:读取大量的无用S ] { L `数据行(300000),最后却抛弃掉。
而且这会造成一个问题:加载了很多热点不是很高的数据页到buffer pool,会造成buffer pool的污染,占用buffer pool的空间。
遇到的问题
为了在每次重启时确保清空buffer pool,我们需要关闭innodb_buffer_pool_dump_at_shutdown和innodb_buffer_pool_load_at_startup,这两Z f # % O n个选项能够控制数据库关闭时dump出buffer pool中的数据$ f S和在数据库开启时载入在磁盘上备份buffer pool的数据。