Prefix support and lengths of prefixes (where supported) are storage engine dependent. For example, a prefix can be up to 767 bytes long for InnoDB tables or 3072 bytes if the innodb_large_prefix option is enabled. For MyISAM tables, the prefix length limit is 1000 bytes.
https://dev.mysql.com/doc/refman/5.7/en/create-index.html
utf8mb4 是 MySQL 5.5.3 之后增加的编码,mb4 就是 most bytes 4 的意思,专门用来兼容四字节的 unicode ,utf8mb4 是 utf8 的超集。之所以需要 utf8mb4 ,是因为之前的 utf8 编码最大字符长度为 3 字节,如果遇到 4 字节的宽字符就会插入异常了。三个字节的 UTF-8 最大能编码的 Unicode 字符是 0xffff ,也就是 Unicode 中的基本多文种平面(BMP)。因此,任何不在基本多文种平面的 Unicode 字符,都无法使用 MySQL 的 utf8 字符集存储,这就包括 Emoji 表情( Emoji 是一种特殊的 Unicode 编码,常见于手机上),和很多不常用的汉字,以及任何新增的 Unicode 字符等(这些都是 utf8 的缺点)。
InnoDB ,如果需要建索引,就不能超过 767bytes ,utf8 编码,2553=765 bytes ,是能建索引情况下的最大值,utf8mb4 编码,默认字符长度则应该是767除以4向下取整,就是191。如果设置了 innodb_large_prefix ,最大长度是3072字节,utf8编码 ,10243=3072 bytes ,utf8mb4编码 ,768*4=3072 。
我们可以测下 MyISAM ,utf8mb4 编码表 test1 ,250长度的字段 c1 ,251长度的字段c2 ,
CREATE TABLE test1 (c1 varchar(250), c2 varchar(251)) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
c1 能创建索引,c2 创建出错,utf8mb4 编码,varchar 最大限制 250 个字符,
/* 可执行 */
create index idx_test1_01 on test1(c1);
/*SQL 错误 [1071] [42000]: Specified key was too long; max key length is 1000 bytes*/
create index idx_test1_02 on test1(c2);
utf8 编码表 test2 ,333 长度的字段c1 ,334 长度的字段 c2 ,
CREATE TABLE test2 (c1 varchar(333), c2 varchar(334)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
c1 能创建索引,c2 创建出错,utf8 编码,varchar 最大限制 333 个字符,
/* 可执行 */
create index idx_test2_01 on test2(c1);
/*SQL 错误 [1071] [42000]: Specified key was too long; max key length is 1000 bytes*/
create index idx_test2_02 on test2(c2);
create index idx_test_01 on test(c1(250(含)以下的值))
但是1亿数据量,创建非常慢,
新特性解读 | 来聊聊 MySQL8.0 的 json 模式校验
社区近期动态