MySQL索引到底支持多少字节?

    xiaoxiao2022-07-09  74

    那么我们来看一下MySQL varchar类型的索引到底能盛多少字节的东西。 MySQL的varchar索引只支持不超过768个字节  atin1 = 1 byte = 1 character uft8 = 3 byte = 1 character gbk = 2 byte = 1 character 如果是GBK,也就是双字节,那么这个索引能盛 768/2=384字节。 如果创建一个大字段的索引的时候就需要注意这些,否则就会报错,如下: mysql> create index sql_q on slow_log(sql_md5,sql_info); ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes mysql> 这种情况就可以使用前缀索引来解决这个问题: alter table XXX add index ind_contentl(content(100)) //100 或其它的数  MySQL 5.5 之前, UTF8 编码只支持1-3个字节  从MYSQL5.5开始,可支持4个字节UTF编码utf8mb4,一个字符最多能有4字节,utf8mb4兼容utf8,所以能支持更多的字符集。  单列索引限制767,起因是256×3-1。这个3是字符最大占用空间(utf8)。 5.5以后,开始支持4个字节的utf8。255×4>767, 于是增加了一个参数叫做 innodb_large_prefix。 这个参数默认值是OFF。当改为ON时,允许列索引最大达到3072。 但是开启该参数后还需要开启表的动态存储或压缩: Enable this option to allow index key prefixes longer than 767 bytes (up to 3072 bytes) for InnoDB tables that use DYNAMIC or COMPRESSED row format. (Creating such tables also requires the option values innodb_file_format=barracuda and innodb_file_per_table=true.) See Section 14.8.1.7, “Limits on InnoDB Tables” for maximums associated with index key prefixes under various settings.
    最新回复(0)