MySQL - 行大小如何大于 65535

MySQL - How is the row size above 65535

我了解到 mysql 中的最大行限制为 65535,等于 (2 ^ 16) - 1。我也明白,像这样有非常长的行是糟糕的数据库设计。但是,这是我的架构

CREATE TABLE mytable(
  a VARCHAR(20000),
  b VARCHAR(20000),
  c VARCHAR(20000),
  d VARCHAR(5535)
) CHARACTER SET=latin1;

这是我得到的输出

Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

我们再算一算

20000 + 20000 + 20000 + 5535 = 65535

等于且不超过限制。作为记录,d 列有效的最大值是 5526.

我不明白那额外的9个字符是从哪里来的。

来自:http://dev.mysql.com/doc/refman/5.7/en/column-count-limit.html

         row length = 1
         + (sum of column lengths)
         + (number of NULL columns + delete_flag + 7)/8
         + (number of variable-length columns)

尝试将列类型更改为 VARCHAR (20000) NOT NULL

VARCHAR的大小是这样计算的:

len + 1 字节,如果列为 0 – 255 字节,len + 2 字节,如果列可能需要超过 255 字节

所以

CREATE TABLE mytable(
  a VARCHAR(20000),       -- 20002
  b VARCHAR(20000),       -- 20002
  c VARCHAR(20000),       -- 20002
  d VARCHAR(5535)         --  5537
) CHARACTER SET=latin1;--    65543 !!!! 8 Bytes to much

看到这个https://mariadb.com/kb/en/mariadb/data-type-storage-requirements/