mysql中包含长字段索引的优化
不同于oracle,在mysql的Innodb存储引擎中,对索引的总长度有限制。在mysql 5.7中(https://dev.mysql.com/doc/refman/5.7/en/innodb-restrictions.html),默认为3072。
If innodb_large_prefix
is enabled (the default), the index key prefix limit is 3072 bytes for InnoDB
tables that use DYNAMIC
orCOMPRESSED
row format. If innodb_large_prefix
is disabled, the index key prefix limit is 767 bytes for tables of any row format.
innodb_large_prefix
is deprecated and will be removed in a future release. innodb_large_prefix
was introduced in MySQL 5.5 to disable large index key prefixes for compatibility with earlier versions of InnoDB
that do not support large index key prefixes.
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 255 characters on a TEXT
or VARCHAR
column, assuming a utf8mb3
character set and the maximum of 3 bytes for each character.
Attempting to use an index key prefix length that exceeds the limit returns an error. To avoid such errors in replication configurations, avoid enabling innodb_large_prefix
on the master if it cannot also be enabled on slaves.
The limits that apply to index key prefixes also apply to full-column index keys.
所以在GBK编码中,最大可以到1536个字符,utf8/utf8mb3,1024个字符,utf8mb4为768个字符。这一限制为天然限制(mariadb因为采用Innodb引擎,该限制同样存在),无法绕开。
drop table big_index_test;
create table big_index_test(id int,content varchar(1000)) ROW_FORMAT=DYNAMIC CHARACTER set = utf8mb4 ;
create index idx_big_index on big_index_test(content);
[SQL]create index idx_big_index on big_index_test(content);
[Err] 1071 - Specified key was too long; max key length is 3072 bytes
相对于oracle来说,如果定义存在这种情况,即使记录没有或很少,仍然无法利用索引覆盖查询实现一些宽表的优化。
因此,对于mysql而言,对于一些定义很长的detail,最好的方式是垂直拆表,也就是将id和detail字段存储在单独的表中,然后业务代码中二次select的方式优化。如果detail字段是json类型的话,可以直接存储为json类型或存储在NoSQL中如mongodb。
注:当年5.6的时候,帮一个产品优化过mysql,mysql在大字段的处理上性能非常低下。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)