varchar(100) 中的 100 指的是字符数,这个字段最多可以存 100 个字符,不论这个字符是具体哪个语言的。
utf8mb4 是变长字符集,一个字符最多可以用 4 个字节表示。
索引长度指的是字节数,在 REDUNDANT or COMPACT row format 下,最多支持 767 个字节。
在 utf8mb4 字符集下按每个字符最多占用 4 个字节来计算,那就是最多支持 191 个字符。
具体看官方文档:
https://dev.mysql.com/doc/refman/8.0/en/innodb-limits.htmlThe index key prefix length limit is 3072 bytes for InnoDB tables that use DYNAMIC or COMPRESSED row format.
The index key prefix length limit is 767 bytes for InnoDB tables that use the REDUNDANT or COMPACT row format. For example, you might hit this limit with a column prefix index of more than 191 characters on a TEXT or VARCHAR column, assuming a utf8mb4 character set and the maximum of 4 bytes for each character.
Attempting to use an index key prefix length that exceeds the limit returns an error.
If you reduce the InnoDB page size to 8KB or 4KB by specifying the innodb_page_size option when creating the MySQL instance, the maximum length of the index key is lowered proportionally, based on the limit of 3072 bytes for a 16KB page size. That is, the maximum index key length is 1536 bytes when the page size is 8KB, and 768 bytes when the page size is 4KB.
The limits that apply to index key prefixes also apply to full-column index keys.