深圳网站建设电话咨询,怎么做北京赛车网站,代理赚钱,网站改版 打造企业文化点击上方“武培轩”#xff0c;选择“设为星标”技术文章第一时间送达#xff01;一#xff0c;前言首先说明一下MySQL的版本#xff1a;mysql select version();-----------| version() |-----------| 5.7.17 |-----------1 row in set (0.00 sec)表结构#xff1a;m…点击上方“武培轩”选择“设为星标”技术文章第一时间送达一前言首先说明一下MySQL的版本mysql select version();-----------| version() |-----------| 5.7.17 |-----------1 row in set (0.00 sec)表结构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为自增主键val为非唯一索引。灌入大量数据共500万mysql select count(*) from test;----------| count(*) |----------| 5242882 |----------1 row in set (4.25 sec)我们知道当limit offset rows中的offset很大时会出现效率问题mysql select * from test where val4 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)为了达到相同的目的我们一般会改写成如下语句mysql select * from test a inner join (select id from test where val4 limit 300000,5) b on a.idb.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)时间相差很明显。为什么会出现上面的结果我们看一下select * from test where val4 limit 300000,5;的查询过程查询到索引叶子节点数据。根据叶子节点上的主键值去聚簇索引上查询需要的全部字段值。类似于下面这张图 像上面这样需要查询300005次索引节点查询300005次聚簇索引的数据最后再将结果过滤掉前300000条取出最后5条。MySQL耗费了大量随机I/O在查询聚簇索引的数据上而有300000次随机I/O查询到的数据是不会出现在结果集当中的。肯定会有人问既然一开始是利用索引的为什么不先沿着索引叶子节点查询到最后需要的5个节点然后再去聚簇索引中查询实际数据。这样只需要5次随机I/O类似于下面图片的过程 其实我也想问这个问题。证实下面我们实际操作一下来证实上述的推论为了证实select * from test where val4 limit 300000,5是扫描300005个索引节点和300005个聚簇索引上的数据节点我们需要知道MySQL有没有办法统计在一个sql中通过索引节点查询数据节点的次数。我先试了Handler_read_*系列很遗憾没有一个变量能满足条件。我只能通过间接的方式来证实InnoDB中有buffer pool。里面存有最近访问过的数据页包括数据页和索引页。所以我们需要运行两个sql来比较buffer pool中的数据页的数量。预测结果是运行select * from test a inner join (select id from test where val4 limit 300000,5) b之后buffer pool中的数据页的数量远远少于select * from test where val4 limit 300000,5;对应的数量因为前一个sql只访问5次数据页而后一个sql访问300005次数据页。select * from test where val4 limit 300000,5mysql 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)可以看出目前buffer pool中没有关于test表的数据页。mysql select * from test where val4 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)可以看出此时buffer pool中关于test表有4098个数据页208个索引页。select * from test a inner join (select id from test where val4 limit 300000,5) b为了防止上次试验的影响我们需要清空buffer pool重启mysql。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)运行sqlmysql select * from test a inner join (select id from test where val4 limit 300000,5) b on a.idb.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)我们可以看明显的看出两者的差别第一个sql加载了4098个数据页到buffer pool而第二个sql只加载了5个数据页到buffer pool。符合我们的预测。也证实了为什么第一个sql会慢读取大量的无用数据行(300000)最后却抛弃掉。而且这会造成一个问题加载了很多热点不是很高的数据页到buffer pool会造成buffer pool的污染占用buffer pool的空间。遇到的问题为了在每次重启时确保清空buffer pool我们需要关闭innodb_buffer_pool_dump_at_shutdown和innodb_buffer_pool_load_at_startup这两个选项能够控制数据库关闭时dump出buffer pool中的数据和在数据库开启时载入在磁盘上备份buffer pool的数据。作者zhangyachen来源https://github.com/zhangyachen/zhangyachen.github.io/issues/117觉得不错点个在看~