gpt4 book ai didi

mysql - MySQL/InnoDB 内部如何表示 NULL 值?

转载 作者:行者123 更新时间:2023-11-29 01:19:49 24 4
gpt4 key购买 nike

在 MySQL 中(或者我应该说:使用 MySQL 的 InnoDB 引擎)——空值是如何表示的?即,如果一个列允许有 NULL,那么表(或单个记录,如果它在记录级别)的表示如何改变?

如果不同的列数据类型不同 - 要么解释表示 NULL 的各种方法,要么只选择一种数据类型(例如 INT)。

最佳答案

引用

https://dev.mysql.com/doc/refman/5.7/en/innodb-physical-record.html

引用和解释

ROW_FORMAT=REDUNDANT:

An SQL NULL value reserves one or two bytes in the record directory. Besides that, an SQL NULL value reserves zero bytes in the data part of the record if stored in a variable length column. In a fixed-length column, it reserves the fixed length of the column in the data part of the record. Reserving the fixed space for NULL values enables an update of the column from NULL to a non-NULL value to be done in place without causing fragmentation of the index page.

也就是说,1 bit/col 用于 NULL,不节省数据。

ROW_FORMAT=COMPACT:

The variable-length part of the record header contains a bit vector for indicating NULL columns. If the number of columns in the index that can be NULL is N, the bit vector occupies CEILING(N/8) bytes. (For example, if there are anywhere from 9 to 15 columns that can be NULL, the bit vector uses two bytes.) Columns that are NULL do not occupy space other than the bit in this vector. The variable-length part of the header also contains the lengths of variable-length columns. Each length takes one or two bytes, depending on the maximum length of the column. If all columns in the index are NOT NULL and have a fixed length, the record header has no variable-length part.

即1 bit/col,数据零空间。

在没有证据的情况下,我怀疑 DYNAMICCOMPRESSED 类似于 COMPACT

列长度

每一列前面都有一个 1 或 2 字节的长度。 1 或 2 的选择基于最大潜在列宽。 (注意:虽然 LONGTEXT 需要 4 个字节的长度,但“长度”实际上是指存储在记录中的数量,而不是溢出中的数量。)

溢出存储

虽然我在讨论这个主题,但这里有一些关于“长”字符串/blob 发生的情况的信息——无论是在记录中,还是存储在其他地方:

  • <= 40 bytes (in a given column): 存储在记录中。
  • 如果整个记录适合大约 8KB:存储在记录中。
  • 否则,COMPACT:长列为 768+20
  • 否则,DYNAMICCOMPRESSED:长列为 20

“768”表示记录中存储了text/blob的前768个字节; “20”表示一个 20 字节的“指针”,指向存储其余部分(或全部)的位置。

KEY_BLOCK_SIZE 控制在聚集索引中存储多少列数据,以及将多少列数据放在溢出页上。

(我将 REDUNDANT 排除在外,因为我没有详细信息。)

经验法则

每个 InnoDB 行有 20-30 字节的开销。

BTree(包括 InnoDB 的数据,加上每个二级索引)随着 block split 等而被吸引到 69%。

“Data_free”严重不完整;不要相信它。

MyISAM 在空间上非常简朴;计算 MyISAM 表的空间很容易。从那里乘以 2-3 以获得 InnoDB 所需的空间。 (也有异常(exception),经常涉及MyISAM分片、PK聚类等)

关于mysql - MySQL/InnoDB 内部如何表示 NULL 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45909030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com