MySQL InnoDB 存储引擎, utf8mb4 字符集, varchar(100)到底可以存多少个汉字

157 天前
 vamilk3344

索引是不是只能支持 varchar(768)

2434 次点击
所在节点    MySQL
21 条回复
beneo
157 天前
utf8mb4 是一种变长字符编码,可以用 1 到 4 个字节来表示一个字符,一般是 3 个字节。
dode
157 天前
汉字长度可能不一定
simonlu9
157 天前
100 个
chanyan
157 天前
For example, a VARCHAR(255) column can hold a string with a maximum length of 255 characters. Assuming that the column uses the latin1 character set (one byte per character), the actual storage required is the length of the string (L), plus one byte to record the length of the string. For the string 'abcd', L is 4 and the storage requirement is five bytes. If the same column is instead declared to use the ucs2 double-byte character set, the storage requirement is 10 bytes: The length of 'abcd' is eight bytes and the column requires two bytes to store lengths because the maximum length is greater than 255 (up to 510 bytes).

The effective maximum number of bytes that can be stored in a VARCHAR or VARBINARY column is subject to the maximum row size of 65,535 bytes, which is shared among all columns. For a VARCHAR column that stores multibyte characters, the effective maximum number of characters is less. For example, utf8mb4 characters can require up to four bytes per character, so a VARCHAR column that uses the utf8mb4 character set can be declared to be a maximum of 16,383 characters.
chanyan
157 天前
@chanyan #4 mysql8
siweipancc
157 天前
1=1, 长字符串建议另开表,以前爆过,贼爽
https://dev.mysql.com/doc/refman/8.0/en/innodb-limits.html
vamilk3344
157 天前
@chanyan 65535 是最大行大小吧
pkoukk
157 天前
100 个
991547436
157 天前
在 MySQL InnoDB 存储引擎中,使用 utf8mb4 字符集的情况下,一个字符可能占用最多 4 个字节。varchar(N) 中的 N 表示该字段最大能够存储的字符数。

因此,对于 utf8mb4 字符集,varchar(100) 的最大存储空间是 100 * 4 = 400 个字节。

在汉字的情况下,由于 utf8mb4 中一个汉字占用 3 个或 4 个字节,实际能够存储的汉字数量可能会受到这个限制。如果每个汉字占用 4 个字节,那么 varchar(100) 可以存储的最大汉字数量将是 400 / 4 = 100 个汉字。

需要注意的是,MySQL 5.7.7 版本之前的 utf8 字符集只支持最多 3 字节的 UTF-8 编码,因此在使用 utf8mb4 时,如果你的数据库版本较老,要确保使用了支持 utf8mb4 的版本。在使用较老版本 MySQL 的情况下,varchar(100) 的最大存储空间可能仅限于 300 个字节。
justplaymore
157 天前
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.html

The 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.
justplaymore
157 天前
你完全可以在一个 varchar(255) 字段上去创建一个前缀索引 INDEX(column_name(10)),这里的 10 就是前缀索引的长度,这个值表示对 varchar(255) 字段里的前 10 个字符做索引。
xieren58
157 天前
换 pg 就行啦... mysql 就是麻烦...
xuanbg
157 天前
100 个,英文字母或数字、符号也是 100 个
dorothyREN
157 天前
text 不香吗 为啥要用 varchar
ShikiSuenVEVO
157 天前
UTF8 有些高万字会占据两个汉字的资料空间。
gg1025
157 天前
100 个
gg1025
157 天前
mysql 支持对字段指定字符集,一般来说做索引的字段大部门不会超 utf8mb3 范围
Kobayashi
157 天前
singularity9866
157 天前
如果是纯汉子的话 就是 100 个
可以使用这个 SQL 判断查看你的字符个数
SELECT CHAR_LENGTH(column) FROM table;
dettan
137 天前
@siweipancc 爆的什么 好奇

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/997751

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX