深入聊聊MySQL中各种对象的大小长度限制

今天给大家介绍一下 mysql 8.0 中的各种对象的大小、长度以及数量限制。

标识符的长度限制

下表描述了各种不同类型标识符的最大长度。

标识符 最大长度(字符)
数据库 64(包括 ndb cluster 8.0.18 以及更高版本)
64(包括 ndb cluster 8.0.18 以及更高版本)
字段 64
索引 64
约束 64
存储过程 64
视图 64
表空间 64
服务器 64
日志文件组 64
别名 256(参见下文)
复合语句标签 16
自定义变量 64
资源组 64

create view 语句中字段的别名最多允许 64 个字符,而不是 256 个字符。

如果定义约束时没有指定约束名,mysql 自动基于相关的表名生成一个内部名称。例如,自动生成的外键和 check 约束名由表名加上 _ibfk_ 或者 _chk_ 以及一个数字组成。如果表名的长度接近约束名的长度限制(64 个字符),加上其他字符之后可能会超长,从而导致错误。

标识符使用 unicode(utf-8)存储,包括表定义中的标识符和 mysql 数据库中权限表中存储的标识符。权限表中标识符字段的大小使用字符做为单位。我们可以使用多字节字符(例如中文)作为标识符,允许的长度不会受到影响。

ndb 8.0.18 之前的版本中,ndb cluster 允许的数据库名和表名最长为 63 个字符。从 ndb 8.0.18 开始删除了这个限制。

mysql 账户名中的用户名和主机名是字符串,而不是标识符。关于权限表中的这些字段的最大长度参考下一节内容。

权限表中范围字段的长度限制

权限表中的范围字段用于存储字符串,默认值为空字符串。下表列出了这些字段允许存储的最大字符串长度。

字段名 允许的最长字符串
host, proxied_host 255(mysql 8.0.17 之前为 60)
user, proxied_user 32
db 64
table_name 64
column_name 64
routine_name 64

host 和 proxied_host 字符串在存储之前会转换为小写形式。

为了检查访问权限,user、proxied_user、authentication_string、db 以及 table_name 字符串的比较区分大小写。host、proxied_host、column_name 以及 routine_name 字符串的比较不区分大小写。

数据库和表的数量限制

mysql 不限制数据库的数量。不过,底层文件系统可能会限制目录(一个目录对应一个数据库)的数量。

mysql 不限制表的数量。不过,底层文件系统可能会限制文件(表使用文件存储)的数量。不同存储引擎可能存在特殊的限制,innodb 允许创建 40 亿个表。

表大小的限制

mysql 数据库中表的大小实际上取决于操作系统文件大小的限制,而不是 mysql 内部的限制。

对于 windows 用户而言,fat 和 vfat (fat32) 文件系统不适合在生产环境中使用,推荐使用 ntfs 文件系统存储 mysql 数据库。

如果出现表容量已满的错误,可能的原因有以下几点:

  • 磁盘已满。
  • 存储引擎为 innodb,表空间文件已满。表空间的最大大小同样也是表的最大大小。具体内容可以参考 innodb 存储引擎的大小限制。一般而言,当表的大小超过 1 tb 时才推荐考虑使用跨表空间文件的分区表。
  • 到达操作系统文件大小限制。例如,操作系统最大支持 2 gb 的文件,同时使用了 myisam 存储引擎,数据文件或者索引文件到达了这个限制。

you are using a myisam table and the space required for the table exceeds what is permitted by the internal pointer size. myisam permits data and index files to grow up to 256tb by default, but this limit can be changed up to the maximum permissible size of 65,536tb (2567 − 1 bytes).

if you need a myisam table that is larger than the default limit and your operating system supports large files, the create table statement supports avg_row_length and max_rows options. see create table statement. the server uses these options to determine how large a table to permit.

if the pointer size is too small for an existing table, you can change the options with alter table to increase a table’s maximum permissible size. see alter table statement.

alter table tbl_name max_rows=1000000000 avg_row_length=nnn;

you have to specify avg_row_length only for tables with blob or text columns; in this case, mysql cannot optimize the space required based only on the number of rows.

to change the default size limit for myisam tables, set the myisam_data_pointer_size, which sets the number of bytes used for internal row pointers. the value is used to set the pointer size for new tables if you do not specify the max_rows option. the value of myisam_data_pointer_size can be from 2 to 7. for example, for tables that use the dynamic storage format, a value of 4 permits tables up to 4gb; a value of 6 permits tables up to 256tb. tables that use the fixed storage format have a larger maximum data length. for storage format characteristics, see myisam table storage formats.

you can check the maximum data and index sizes by using this statement:

show table status from db_name like ‘tbl_name’;

you also can use myisamchk -dv /path/to/table-index-file. see show statements, or myisamchk — myisam table-maintenance utility.

other ways to work around file-size limits for myisam tables are as follows:

if your large table is read only, you can use myisampack to compress it. myisampack usually compresses a table by at least 50%, so you can have, in effect, much bigger tables. myisampack also can merge multiple tables into a single table. see myisampack — generate compressed, read-only myisam tables.

mysql includes a merge library that enables you to handle a collection of myisam tables that have identical structure as a single merge table. see the merge storage engine.

you are using the memory (heap) storage engine; in this case you need to increase the value of the max_heap_table_size system variable. see server system variables.

字段数量和数据行大小的限制

字段数量限制

mysql 中每个表最多包含 4096 个字段,不过实际上的字段数量限制比这个值更小。这个限制取决于几个因素:

  1. 数据行的最大大小限制了字段的数量(以及可能的大小),因为所有字段的总长度不能超过这个大小。参见下一节。
  2. 每个字段的存储需求限制了字段的数量。某些数据类型的存储需求取决于存储引擎、存储格式以及字符集等因素。具体内容可以参考官方文档。
  3. 存储引擎可能会额外限制表中字段的数量。例如,innodb 表最多允许 1017 个字段。其他存储引擎相关的限制可以参考官方文档。
  4. 函数索引的实现利用了隐藏的虚拟计算存储列功能,因此每个函数索引也会占用一个字段数量。

数据行大小限制

一行数据的大小限制有以下几个因素决定:

  • mysql 表中每一行数据的内部存储上限是 65535 字节,即使存储引擎可以支持更大的存储。blob 和 text 类型只占 9 到 12 个字节,因为它们的实际内容是单独存储的。
  • innodb 表数据行(数据库页本地存储的数据)的最大大小略小于 innodb_page_size(4kb、8kb、16kb 以及 32kb)的一半。例如,对于默认的 16kb 页大小配置,数据行的最大大小为略少于 8kb。对于 64kb 数据页,最大的数据行大小略小于 16kb。
    如果一行中的变长字段超过了 innodb 数据行大小限制,innodb 会使用页外(off-page)存储的方式保存某些变长字段,直到数据行能够满足 innodb 数据行大小限制。对于页外存储的变长字段,本地存储的数据内容取决于数据行的格式,详细信息可以参考“innodb 数据行格式”。
  • 不同存储引擎使用不同的页头和尾部数据,从而会影响到数据行实际可用的存储空间。

数据行大小限制示例

以下 innodb 和 myisam 示例演示了 mysql 最大行大小 65535 字节的限制。该限制和存储引擎无关,即使存储引擎可以支持更大的数据行,也需要遵循该限制。

mysql> create table t (a varchar(10000), b varchar(10000),
       c varchar(10000), d varchar(10000), e varchar(10000),
       f varchar(10000), g varchar(6000)) engine=innodb character set latin1;
error 1118 (42000): 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
mysql> create table t (a varchar(10000), b varchar(10000),
       c varchar(10000), d varchar(10000), e varchar(10000),
       f varchar(10000), g varchar(6000)) engine=myisam character set latin1;
error 1118 (42000): 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

以下 myisam 示例将其中一个字段修改为 text,可以避免超过 65535 字节的限制,因为 blob 和 text 字段只在数据行大小中占用 9 到 12 个字节。

mysql> create table t (a varchar(10000), b varchar(10000),
       c varchar(10000), d varchar(10000), e varchar(10000),
       f varchar(10000), g text(6000)) engine=myisam character set latin1;
query ok, 0 rows affected (0.02 sec)

以下 innodb 表可以创建成功是因为将字段修改为 text 可以避免超过 65535 字节的限制,同时 innodb 页外存储可以避免超过 innodb 数据行大小的限制。

mysql> create table t (a varchar(10000), b varchar(10000),
       c varchar(10000), d varchar(10000), e varchar(10000),
       f varchar(10000), g text(6000)) engine=innodb character set latin1;
query ok, 0 rows affected (0.02 sec)

变长字段的存储包含了长度信息,这个内容也会被计算到数据行大小中。例如,一个 varchar(255) character set utf8mb3 字段需要使用 2 个字节存储数据的长度,因此每个数值最多可能占用 767 个字节。

以下语句能够成功创建表 t1,因为它的字段需要 32765 + 2 字节加上 32766 + 2 字节,能够满足 65535 字节的限制:

mysql> create table t1
       (c1 varchar(32765) not null, c2 varchar(32766) not null)
       engine = innodb character set latin1;
query ok, 0 rows affected (0.02 sec)

以下语句创建表 t2 失败,因为虽然字段的长度没有超过 65535 字节的限制,但是增加 2 个记录长度的字节之后超过了该限制:

mysql> create table t2
       (c1 varchar(65535) not null)
       engine = innodb character set latin1;
error 1118 (42000): 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
reducing the column length to 65,533 or less permits the statement to succeed.
mysql> create table t2
       (c1 varchar(65533) not null)
       engine = innodb character set latin1;
query ok, 0 rows affected (0.01 sec)

对于 myisam 表,可空字段需要占用一个额外的空间记录数据是否为空值。每个可空字段需要 1 个额外的比特,最终向上舍入到字节。

以下语句创建表 t3 失败,因为 myisam 需要额外的空间存储可空字段,从而导致数据行大小超过了 65535 字节:

mysql> create table t3
       (c1 varchar(32765) null, c2 varchar(32766) null)
       engine = myisam character set latin1;
error 1118 (42000): 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
for information about innodb null column storage, see innodb row formats.

innodb 限制行大小(数据库页内存储的本地数据)为略小于数据库页的一半。以下语句失败的原因是全部字段长度超过了一个 innodb 页 16 kb 的数据行大小限制。

mysql> create table t4 (
       c1 char(255),c2 char(255),c3 char(255),
       c4 char(255),c5 char(255),c6 char(255),
       c7 char(255),c8 char(255),c9 char(255),
       c10 char(255),c11 char(255),c12 char(255),
       c13 char(255),c14 char(255),c15 char(255),
       c16 char(255),c17 char(255),c18 char(255),
       c19 char(255),c20 char(255),c21 char(255),
       c22 char(255),c23 char(255),c24 char(255),
       c25 char(255),c26 char(255),c27 char(255),
       c28 char(255),c29 char(255),c30 char(255),
       c31 char(255),c32 char(255),c33 char(255)
       ) engine=innodb row_format=dynamic default charset latin1;
error 1118 (42000): row size too large (> 8126). changing some columns to text or blob may help.
in current row format, blob prefix of 0 bytes is stored inline.

总结

到此这篇关于mysql中各种对象的大小长度限制的文章就介绍到这了,更多相关mysql对象大小长度限制内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐