V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
vamilk3344
V2EX  ›  MySQL

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

  •  
  •   vamilk3344 · 145 天前 · 2403 次点击
    这是一个创建于 145 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    21 条回复    2023-12-26 21:09:49 +08:00
    beneo
        1
    beneo  
       145 天前
    utf8mb4 是一种变长字符编码,可以用 1 到 4 个字节来表示一个字符,一般是 3 个字节。
    dode
        2
    dode  
       145 天前
    汉字长度可能不一定
    simonlu9
        3
    simonlu9  
       145 天前
    100 个
    chanyan
        4
    chanyan  
       145 天前
    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
        5
    chanyan  
       145 天前
    @chanyan #4 mysql8
    siweipancc
        6
    siweipancc  
       145 天前 via iPhone
    1=1, 长字符串建议另开表,以前爆过,贼爽
    https://dev.mysql.com/doc/refman/8.0/en/innodb-limits.html
    vamilk3344
        7
    vamilk3344  
    OP
       144 天前
    @chanyan 65535 是最大行大小吧
    pkoukk
        8
    pkoukk  
       144 天前
    100 个
    991547436
        9
    991547436  
       144 天前   ❤️ 1
    在 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
        10
    justplaymore  
       144 天前   ❤️ 2
    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
        11
    justplaymore  
       144 天前
    你完全可以在一个 varchar(255) 字段上去创建一个前缀索引 INDEX(column_name(10)),这里的 10 就是前缀索引的长度,这个值表示对 varchar(255) 字段里的前 10 个字符做索引。
    xieren58
        12
    xieren58  
       144 天前
    换 pg 就行啦... mysql 就是麻烦...
    xuanbg
        13
    xuanbg  
       144 天前   ❤️ 1
    100 个,英文字母或数字、符号也是 100 个
    dorothyREN
        14
    dorothyREN  
       144 天前   ❤️ 1
    text 不香吗 为啥要用 varchar
    ShikiSuenVEVO
        15
    ShikiSuenVEVO  
       144 天前
    UTF8 有些高万字会占据两个汉字的资料空间。
    gg1025
        16
    gg1025  
       144 天前
    100 个
    gg1025
        17
    gg1025  
       144 天前
    mysql 支持对字段指定字符集,一般来说做索引的字段大部门不会超 utf8mb3 范围
    singularity9866
        19
    singularity9866  
       144 天前
    如果是纯汉子的话 就是 100 个
    可以使用这个 SQL 判断查看你的字符个数
    SELECT CHAR_LENGTH(column) FROM table;
    dettan
        20
    dettan  
       124 天前
    @siweipancc 爆的什么 好奇
    siweipancc
        21
    siweipancc  
       123 天前 via iPhone
    @dettan 子父关系用前缀树,行数据满了,这时候来新需求了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5445 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 06:02 · PVG 14:02 · LAX 23:02 · JFK 02:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.