› 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 · Dec 5, 2023 · 4494 views
    This topic created in 1075 days ago, the information mentioned may be changed or developed.

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

    20 replies  •  2023-12-26 21:09:49 +08:00
    beneo
    beneo
    Dec 5, 2023
    1
    utf8mb4 是一种变长字符编码,可以用 1 到 4 个字节来表示一个字符,一般是 3 个字节。
    dode
    dode
    Dec 5, 2023
    2
    汉字长度可能不一定
    simonlu9
    simonlu9
    Dec 5, 2023
    3
    100 个
    chanyan
    chanyan
    Dec 5, 2023
    4
    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
    chanyan
    Dec 5, 2023
    5
    @chanyan #4 mysql8
    siweipancc
    siweipancc
    Dec 5, 2023 via iPhone
    6
    1=1, 长字符串建议另开表,以前爆过,贼爽
    https://dev.mysql.com/doc/refman/8.0/en/innodb-limits.html
    vamilk3344
    vamilk3344
    OP
    Dec 5, 2023
    7
    @chanyan 65535 是最大行大小吧
    pkoukk
    pkoukk
    Dec 5, 2023
    8
    100 个
    991547436
    991547436
    Dec 5, 2023❤️1
    9
    在 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
    justplaymore
    Dec 5, 2023❤️2
    10
    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
    justplaymore
    Dec 5, 2023
    11
    你完全可以在一个 varchar(255) 字段上去创建一个前缀索引 INDEX(column_name(10)),这里的 10 就是前缀索引的长度,这个值表示对 varchar(255) 字段里的前 10 个字符做索引。
    xieren58
    xieren58
    Dec 5, 2023
    12
    换 pg 就行啦... mysql 就是麻烦...
    xuanbg
    xuanbg
    Dec 5, 2023❤️1
    13
    100 个,英文字母或数字、符号也是 100 个
    dorothyREN
    dorothyREN
    Dec 5, 2023❤️1
    14
    text 不香吗 为啥要用 varchar
    gg1025
    gg1025
    Dec 5, 2023
    15
    100 个
    是
    gg1025
    gg1025
    Dec 5, 2023
    16
    mysql 支持对字段指定字符集,一般来说做索引的字段大部门不会超 utf8mb3 范围
    singularity9866
    singularity9866
    Dec 6, 2023
    18
    如果是纯汉子的话 就是 100 个
    可以使用这个 SQL 判断查看你的字符个数
    SELECT CHAR_LENGTH(column) FROM table;
    dettan
    dettan
    Dec 26, 2023
    19
    @siweipancc 爆的什么 好奇
    siweipancc
    siweipancc
    Dec 26, 2023 via iPhone
    20
    @dettan 子父关系用前缀树,行数据满了,这时候来新需求了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1027 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 22:54 · PVG 06:54 · LAX 14:54 · JFK 17:54
    ♥ Do have faith in what you're doing.