今天组里有个同事说可以查看innodb buffer pool每个表和索引占的大小,为此我搜了下,还真有方法,记录下。

innodb buffer pool有几个目的:

  • 缓存数据--众所周知,这个占了buffer pool的大半空间
  • 缓存目录--数据字典
  • insert buffer
  • 排序的内部结构--比如自适应hash的结构或者一些行锁

1.buffer pool是怎样分配空间的?

SELECT engine,
count(*) as TABLES,
concat(round(sum(table_rows)/1000000,2),'M') rows,
concat(round(sum(data_length)/(1024*1024*1024),2),'G') DATA,
concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx,
concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size,
round(sum(index_length)/sum(data_length),2) idxfrac
FROM information_schema.TABLES
WHERE table_schema not in ('mysql', 'performance_schema', 'information_schema')
GROUP BY engine
ORDER BY sum(data_length+index_length) DESC LIMIT 10;

得到的结果:

+--------+--------+----------+---------+--------+------------+---------+
| engine | TABLES | rows | DATA | idx | total_size | idxfrac |
+--------+--------+----------+---------+--------+------------+---------+
| InnoDB | 71608 | 1644.51M | 130.79G | 82.76G | 213.55G | 0.63 |
+--------+--------+----------+---------+--------+------------+---------+

2.获取buffer pool占的page个数:

select count(*) from information_schema.innodb_buffer_page;

结果:

+----------+
| count(*) |
+----------+
| 262142 |
+----------+

聪明的同学自己算下使用的buffer pool是多大吧。

3.获取page类型:

select
page_type as Page_Type,
sum(data_size)/1024/1024 as Size_in_MB
from information_schema.innodb_buffer_page
group by page_type
order by Size_in_MB desc;

结果:

+-------------------+--------------+
| Page_Type | Size_in_MB |
+-------------------+--------------+
| INDEX | 158.66378689 |
| UNKNOWN | 0.00000000 |
| TRX_SYSTEM | 0.00000000 |
| SYSTEM | 0.00000000 |
| FILE_SPACE_HEADER | 0.00000000 |
| IBUF_BITMAP | 0.00000000 |
| EXTENT_DESCRIPTOR | 0.00000000 |
| ALLOCATED | 0.00000000 |
| INODE | 0.00000000 |
| BLOB | 0.00000000 |
| UNDO_LOG | 0.00000000 |
| IBUF_FREE_LIST | 0.00000000 |
| IBUF_INDEX | 0.00000000 |
+-------------------+--------------+

从这里可以看到数据和索引占了buffer pool的大部分空间。也可以看出来这里有几种重要的页类型:

  • INDEX: B-Tree index
  • IBUF_INDEXInsert buffer index
  • UNKNOWN: not allocated / unknown state
  • TRX_SYSTEM: transaction system data

眼亮的同学可能会问,你上面不是说会缓存数据吗?怎么这里出来只有INDEX类型占多半buffer pool?数据哪里去了?数据在INDEX里!!!数据在聚簇索引的叶子节点上。

4.buffer pool里每个索引的使用

select
table_name as Table_Name, index_name as Index_Name,
count(*) as Page_Count, sum(data_size)/1024/1024 as Size_in_MB
from information_schema.innodb_buffer_page
group by table_name, index_name
order by Size_in_MB desc;

结果:

+--------------------------------------------+-----------------+------------+-------------+
| Table_Name | Index_Name | Page_Count | Size_in_MB |
+--------------------------------------------+-----------------+------------+-------------+
| `magento`.`core_url_rewrite` | PRIMARY | 2829 | 40.64266014 |
| `magento`.`core_url_rewrite` | FK_CORE_URL_... | 680 | 6.67517281 |
| `magento`.`catalog_product_entity_varchar` | PRIMARY | 449 | 6.41064930 |
| `magento`.`catalog_product_index_price` | PRIMARY | 440 | 6.29357910 |
| `magento`.`catalog_product_entity` | PRIMARY | 435 | 6.23898315 |
+--------------------------------------------+-----------------+------------+-------------+

5.一个典型的buffer pool使用监控:

从这里图里我们可以看到buffer pool几乎是被填满的,另外预留了10%的空间用来做其他用途。

6.一般怎么设置buffer pool大小呢?

warm rows data size + warm indexes size (excl. clustered) + 20%

7.如何预热buffer pool?

在InnoDB上面执行select语句:

  • 对于聚簇索引来说,大多数情况通过SELECT COUNT(*) 加载到buffer pool中了。
  • 对于二级索引来说,要执行一些简单的语句来抓取全部数据,比如select * from tbname where 索引的第一列。或者select * from tbname force index(二级索引) where colname <>0.

另外,MySQL5.7支持动态修改buffer pool:

mysql> SET GLOBAL innodb_buffer_pool_size=size_in_bytes;

8.Dump & restore

在MySQL (5.6+), Percona Server (5.5.10+) or MariaDB (10.0+)可以通过以下配置把buffer pool里面的数据dump出来,并在启动的时候加载到内存中:

  • innodb_buffer_pool_dump_at_shutdown=ON
  • innodb_buffer_pool_load_at_startup=ON

参考资料:

https://michael.bouvy.net/blog/en/2015/01/18/understanding-mysql-innodb-buffer-pool-size/

http://www.speedemy.com/mysql/17-key-mysql-config-file-settings/innodb_buffer_pool_size/

理解innodb buffer pool的更多相关文章

  1. innodb buffer pool小解

    INNODB维护了一个缓存数据和索引信息到内存的存储区叫做buffer pool,他会将最近访问的数据缓存到缓冲区.通过配置各个buffer pool的参数,我们可以显著提高MySQL的性能. INN ...

  2. MySQL · 引擎特性 · InnoDB Buffer Pool

    前言 用户对数据库的最基本要求就是能高效的读取和存储数据,但是读写数据都涉及到与低速的设备交互,为了弥补两者之间的速度差异,所有数据库都有缓存池,用来管理相应的数据页,提高数据库的效率,当然也因为引入 ...

  3. [转]MySQL innodb buffer pool

    最近在对公司的 MySQL 服务器做性能优化, 一直对 innodb 的内存使用方式不是很清楚, 乘这机会做点总结. 在配置 MySQL 的时候, 一般都会需要设置 innodb_buffer_poo ...

  4. innodb buffer pool相关特性

    背景 innodb buffer pool作为innodb最重要的缓存,其缓存命中率的高低会直接影响数据库的性能.因此在数据库发生变更,比如重启.主备切换实例迁移等等,innodb buffer po ...

  5. MySQL · 性能优化· InnoDB buffer pool flush策略漫谈

    MySQL · 性能优化· InnoDB buffer pool flush策略漫谈 背景 我们知道InnoDB使用buffer pool来缓存从磁盘读取到内存的数据页.buffer pool通常由数 ...

  6. Innodb buffer pool/redo log_buffer 相关

    InnoDB存储引擎是基于磁盘存储的,并将其中的记录按照页的方式进行管理.在数据库系统中,由于CPU速度和磁盘速度之前的鸿沟,通常使用缓冲池技术来提高数据库的整体性能. 1. Innodb_buffe ...

  7. 14.6.3.5 Configuring InnoDB Buffer Pool Flushing

    14.6.3.5 Configuring InnoDB Buffer Pool Flushing InnoDB 执行某些任务在后台, 包括脏叶的刷新(那些已经发生改变的pages 但是没有写入到数据文 ...

  8. 14.6.3.4 Configuring InnoDB Buffer Pool Prefetching (Read-Ahead) 配置InnoDB Buffer pool 预取

    14.6.3.4 Configuring InnoDB Buffer Pool Prefetching (Read-Ahead) 配置InnoDB Buffer pool 预取 一个预读请求是一个I/ ...

  9. 14.6.3.1 The InnoDB Buffer Pool

    14.6.3.1 The InnoDB Buffer Pool InnoDB 保持一个存储区域被称为buffer pool 用于cache数据和索引在内存里, 知道InnoDB buffer pool ...

随机推荐

  1. python与shell的3种交互方式介绍

    [目录] 1.os.system(cmd) 2.os.popen(cmd) 3.利用subprocess模块 4.subprocessor模块进阶 [概述] 考虑这样一个问题,有hello.py脚本, ...

  2. soui中,列表控件动态高度的使用注意

    1.listview的模板template中,需要增加defHeight属性,即默认高度,同时,不能出现itemHeight属性,否则动态高度会失效 2.数据适配器中,重写getViewDesired ...

  3. elasticsearch运维实战之2 - 系统性能调优

    elasticsearch性能调优 集群规划 独立的master节点,不存储数据, 数量不少于2 数据节点(Data Node) 查询节点(Query Node),起到负载均衡的作用 Linux系统参 ...

  4. centeros bash: ifconfig: command not found

    如果ifconfig命令不存在 yum upgrade yum install net-tools

  5. 百度编辑器ueditor 的 submit 表单提交

    页面中表单提交代码: <input type="submit" name="Submit" value="修改保存"> 提交的结 ...

  6. percona-toolkit中在线ddl

    percona-toolkit中在线ddl percona-toolkit工具提供了一组用于mysql操作的工具,比如主从复制,在线更改mysql表ddl等 一.安装1.安装perl(略)2.BI&a ...

  7. python httprequest, locust

    r = self.client.get("/orders", headers = {"Cookie": self.get_user_cookie(user[0] ...

  8. 快速原型设计工具-Axure RP的介绍及简单使用(生产初期向客户展示设计产品的原型-也就是展示产品)

    啧啧~~ 给大家介绍一款超棒的原型设计工具--美国Axure Software Solution公司旗舰产品Axure RP 这款工具通俗的说呢,就是在项目整体需求考察后对整体设计一个简要性概括!设计 ...

  9. Oracle之ORDER BY

    ------------基本查询--1.查询出的是一张虚拟的结果表-----基本语法---- * 所有列(字段)select * from emps; -----查询指定字段select employ ...

  10. java实现excel表格导出数据

    /** * 导出清单 eb中 firstRow(EntityBean) 列表第一行数据,键值对(不包含序号)例:("name","姓名") * data(Ent ...