有时候,了解事务的大小很重要。默认情况下,事务的大小是有限的,以保证集群的最佳行为。
首先要将事务分成两种类型:
1.生成数据的事务(写操作,比如insert、delete、update等DML操作)
2.只读的事务(查询操作)
Size of DML
要知道DML事务的大小,我们唯一的可能是解析二进制日志(或查询binlog事件)。
我们需要从binlog文件中检查binlog事件,然后计算它的大小。为了说明这一点,让我们尝试查找由特定GTID标识的事务: 17f6a975-e2b4-11ec-b714-c8cb9e32df8e:7541914
SQL > \P grep 'Gtid\|COMMIT' ;
Pager has been set to 'grep 'Gtid\|COMMIT' ;'.
SQL > show BINLOG EVENTS in 'binlog.000064' ;
| binlog.000064 | 213 | Gtid | 1 | 298 | SET @@SESSION.GTID_NEXT= '17f6a975-e2b4-11ec-b714-c8cb9e32df8e:7541914' |
| binlog.000064 | 53904723 | Xid | 1 | 53904754 | COMMIT /* xid=75 */ |
SQL > \pager
Pager has been disabled.
SQL > select format_bytes(53904754-213);
+----------------------------+
| format_bytes(53904754-213) |
+----------------------------+
| 51.41 MiB |
+----------------------------+
1 row in set (0.0005 sec)
我们可以看到,该事务生成了51MB的binlog事件。
这种方法可能很复杂,尤其是当您需要解析多个binlog文件来查找所需的事务时。
希望如此,性能模式又能让我们的生活变得更轻松。事实上,我们可以解析这个表二进制日志事务压缩统计了解交易规模的信息。即使我们不使用二进制日志压缩:
select format_bytes(UNCOMPRESSED_BYTES_COUNTER/TRANSACTION_COUNTER) size,
format_bytes(COMPRESSED_BYTES_COUNTER/TRANSACTION_COUNTER) compressed,
TRANSACTION_COUNTER
from performance_schema.binary_log_transaction_compression_stats;
+-----------+------------+---------------------+
| size | compressed | TRANSACTION_COUNTER |
+-----------+------------+---------------------+
| 51.38 MiB | 51.38 MiB | 1 |
+-----------+------------+---------------------+
这 TRANSACTION_COUNTER 列非常重要,如果它大于1,则值是平均值。
因此,如果您真的需要知道一个事务的确切大小,您需要在运行DML之前首先截断该表。
让我们来看看这个例子:
SQL> select format_bytes(UNCOMPRESSED_BYTES_COUNTER/TRANSACTION_COUNTER) size,
format_bytes(COMPRESSED_BYTES_COUNTER/TRANSACTION_COUNTER) compressed,
TRANSACTION_COUNTER
from performance_schema.binary_log_transaction_compression_stats;
+-----------+------------+---------------------+
| size | compressed | TRANSACTION_COUNTER |
+-----------+------------+---------------------+
| 17.13 MiB | 17.13 MiB | 6 |
+-----------+------------+---------------------+
1 row in set (0.0004 sec)
SQL > truncate table performance_schema.binary_log_transaction_compression_stats;
Query OK, 0 rows affected (0.0018 sec)
SQL > update sbtest1 set k=k+4;
Query OK, 132188 rows affected (1.3213 sec)
Rows matched: 132188 Changed: 132188 Warnings: 0
SQL > select format_bytes(UNCOMPRESSED_BYTES_COUNTER/TRANSACTION_COUNTER) size,
format_bytes(COMPRESSED_BYTES_COUNTER/TRANSACTION_COUNTER) compressed,
TRANSACTION_COUNTER
from performance_schema.binary_log_transaction_compression_stats;
+-----------+------------+---------------------+
| size | compressed | TRANSACTION_COUNTER |
+-----------+------------+---------------------+
| 51.38 MiB | 51.38 MiB | 1 |
+-----------+------------+---------------------+
1 row in set (0.0017 sec)
我们仍然有可能使用MySQL外壳插件从二进制日志中列出所有事务大小:
JS > check.showTrxSizeSort()
Transactions in binary log binlog.000064 orderer by size (limit 10):
51 mb - 17f6a975-e2b4-11ec-b714-c8cb9e32df8e:7541926
51 mb - 17f6a975-e2b4-11ec-b714-c8cb9e32df8e:7541925
51 mb - 17f6a975-e2b4-11ec-b714-c8cb9e32df8e:7541921
51 mb - 17f6a975-e2b4-11ec-b714-c8cb9e32df8e:7541916
51 mb - 17f6a975-e2b4-11ec-b714-c8cb9e32df8e:7541915
51 mb - 17f6a975-e2b4-11ec-b714-c8cb9e32df8e:7541918
51 mb - 17f6a975-e2b4-11ec-b714-c8cb9e32df8e:7541917
51 mb - 17f6a975-e2b4-11ec-b714-c8cb9e32df8e:7541914
257 bytes - 17f6a975-e2b4-11ec-b714-c8cb9e32df8e:7541924
257 bytes - 17f6a975-e2b4-11ec-b714-c8cb9e32df8e:7541923
MySQL Shell Plugin的下载地址:
https://github.com/lefred/mysqlshell-plugins/wiki/check#showtrxsizesor
怎么获取transaction GTID呢?
如果MySQL支持服务器返回的信息,它有可能将GTID返回给客户机。MySQL Shell支持该特性!
为了启用它,我们使用 session_track_gtids:
SQL > set session_track_gtids='OWN_GTID';
Query OK, 0 rows affected (0.0001 sec)
SQL > update sbtest1 set k=k+1;
Query OK, 132183 rows affected (5.6854 sec)
Rows matched: 132183 Changed: 132183 Warnings: 0
GTIDs: 17f6a975-e2b4-11ec-b714-c8cb9e32df8e:7541914
可以看到,MySQL Shell返回了事务的GTID(使用auto_commit更新)。
Size of DQL
但是我们有可能知道选择的大小吗?
要确定SELECT的大小,我们可以这样计算服务器发送给客户端的字节数:
SQL > select variable_value
from performance_schema.status_by_thread
join performance_schema.threads using(thread_id)
where processlist_id=CONNECTION_ID()
and variable_name='Bytes_sent' into @before;
SQL > select * from sbtest1;
SQL > select format_bytes(variable_value - @before) query_size
from performance_schema.status_by_thread
join performance_schema.threads using(thread_id)
where processlist_id=CONNECTION_ID()
and variable_name='Bytes_sent' ;
+------------+
| query_size |
+------------+
| 26.08 MiB |
+------------+
1 row in set (0.0010 sec)
总结:
正如你所看到的,MySQL服务器通过Performance_Schema和二进制日志。通过解析这些信息,您可以检索DML事务或DQL的大小。
参考:https://blogs.oracle.com/mysql/post/query-and-transaction-size-in-mysql