如何获取 MySQL 中一列所占用的字节数?

问题描述

我需要知道 MysqL 中一列占用了多少字节。

考虑以下架构 -

+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id    | bigint(20) unsigned | NO   | PRI | NULL    |       |
| data  | longblob            | YES  |     | NULL    |       |
+-------+---------------------+------+-----+---------+-------+

忽略 id 字段,让我们谈谈 data 字段。考虑这些元组 -

+-------+---------------------+
| id    | data                |
+-------+---------------------+
|   1   | {ab₹}               |
|   2   | {ab}                |
+-------+---------------------+

所以,我需要的是字节大小而不是像-

+---------------------+
| size_in_bytes       | 
+---------------------+
|                   7 |     // {->(1),a->(1),b->(1),₹->(3),}->(1)
|                   4 |     // {->(1),}->(1)
+---------------------+

经过数小时的搜索,我发现很少有函数只会导致没有字符。

select OCTET_LENGTH(data) from table_name;
+--------------------+
| OCTET_LENGTH(data) |
+--------------------+
|                  5 |
|                  4 |
+--------------------+

SELECT LENGTH(data) from table_name;
+--------------+
| LENGTH(data) |
+--------------+
|            5 |
|            4 |
+--------------+

SELECT char_length(data) from table_name;
+-------------------+
| char_length(data) |
+-------------------+
|                 5 |
|                 4 |
+-------------------+

类似的问题 -> How to get size of column in mysql table 但没有一个答案以字节为单位。

How to get the sizes of the tables of a MySQL database? 这是表格的大小。

MysqL 版本 -> 8.0

enter image description here

解决方法

计数位

select BIT_LENGTH (N'{ab₹}')/8;

返回 7.0000

db<>fiddle