MySQL - max_binlog_cache_size vs binlog_cache_size

Yas*_*uru 2 mysql mysqlbinlog

在MySQL的官方文档中,对这些变量的描述存在很多混淆.

根据它,max_binlog_cache_size意味着,

如果事务需要超过这么多字节的内存,则服务器生成的Multi语句事务需要超过'max_binlog_cache_size'字节的存储错误.

max_binlog_cache_size仅设置事务高速缓存的大小

binlog_cache_size意味着,

用于在事务期间保留对二进制日志的更改的缓存大小.

binlog_cache_size仅设置事务高速缓存的大小

在阅读文档时,我发现这两者之间没有区别.在文档中也有一些非常令人困惑的事情,比如

在MySQL 5.7中,max_binlog_cache_size会话的可见性与binlog_cache_size系统变量的会话可见性相匹配; 换句话说,更改其值只会影响在值更改后启动的新会话.

当我查询服务器变量时,它会显示两者.我有MySQL 5.6MySQL 5.7.我需要知道的是,我应该考虑哪个变量并为哪个服务器配置.

用于MySQL 5.6的binlog_cache_size和用于MySQL 5.7的max_binlog_cache_size ??

还有其他与相关的混淆变量max_binlog_stmt_cache_sizebinlog_stmt_cache_size.

ele*_*nst 8

两个变量都可以在两个版本中配置,它们具有不同的含义.手册和帮助中的定义令人困惑; 这是一个更好的解释:http://dev.mysql.com/doc/refman/5.6/en/binary-log.html

binlog_cache_size定义缓冲区可以使用的最大内存量.如果事务增长超过此值,它将使用临时磁盘文件.请注意,缓冲区是按连接分配的.

max_binlog_cache_size定义事务的最大总大小.如果事务增长超过此值,则失败.

以下是对差异的简单演示.

建立:

MariaDB [test]> select @@binlog_cache_size, @@max_binlog_cache_size, @@binlog_format;
+---------------------+-------------------------+-----------------+
| @@binlog_cache_size | @@max_binlog_cache_size | @@binlog_format |
+---------------------+-------------------------+-----------------+
|               32768 |                   65536 | ROW             |
+---------------------+-------------------------+-----------------+
1 row in set (0.01 sec)

MariaDB [test]> show create table t1 \G
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `a` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

1.交易规模低于@@ binlog_cache_size

(事务成功,使用缓存,不使用磁盘)

MariaDB [test]> flush status;
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> begin;
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> insert into t1 values (repeat('a',20000));
Query OK, 1 row affected (0.01 sec)

MariaDB [test]> insert into t1 values (repeat('a',10000));
Query OK, 1 row affected (0.04 sec)

MariaDB [test]> commit;
Query OK, 0 rows affected (0.05 sec)

MariaDB [test]> show status like 'Binlog_cache%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| Binlog_cache_disk_use | 0     |
| Binlog_cache_use      | 1     |
+-----------------------+-------+
2 rows in set (0.01 sec)
Run Code Online (Sandbox Code Playgroud)

2.事务大小高于@@ binlog_cache_size,但低于@@ max_binlog_cache_size

(事务使用缓存,缓存使用磁盘)

MariaDB [test]> flush status;
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> begin;
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> insert into t1 values (repeat('a',20000));
Query OK, 1 row affected (0.10 sec)

MariaDB [test]> insert into t1 values (repeat('a',20000));
Query OK, 1 row affected (0.10 sec)

MariaDB [test]> commit;
Query OK, 0 rows affected (0.03 sec)

MariaDB [test]> show status like 'Binlog_cache%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| Binlog_cache_disk_use | 1     |
| Binlog_cache_use      | 1     |
+-----------------------+-------+
2 rows in set (0.01 sec)
Run Code Online (Sandbox Code Playgroud)

3.交易规模超过@@ max_binlog_cache_size

(交易失败)

MariaDB [test]> flush status;
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> begin;
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> insert into t1 values (repeat('a',20000));
Query OK, 1 row affected (0.12 sec)

MariaDB [test]> insert into t1 values (repeat('a',20000));
Query OK, 1 row affected (0.15 sec)

MariaDB [test]> insert into t1 values (repeat('a',20000));
Query OK, 1 row affected (0.12 sec)

MariaDB [test]> insert into t1 values (repeat('a',20000));
ERROR 1197 (HY000): Multi-statement transaction required more than 'max_binlog_cache_size' bytes of storage; increase this mysqld variable and try again
Run Code Online (Sandbox Code Playgroud)

因此,如果您的事务很大,但没有太多连接,您可能希望增加@@ binlog_cache_size以避免过多的磁盘写入.

如果您有许多并发连接,则应该小心避免连接尝试同时为缓存分配太多内存.

如果要确保事务不会变得太大,可能需要限制@@ max_binlog_cache_size.

@@ binlog_stmt_cache_size和@@ max_binlog_stmt_cache_size应该以类似的方式工作,区别在于%binlog_cache%值用于事务更新,%binlog_stmt_cache%用于非事务性更新.

在进行实验时,请注意,这些值并非100%精确,最初分配的大小存在一些隐藏的细微差别.它实际上并不重要,但是当你玩低值时可能会让人感到困惑.