开发者社区> 问答> 正文

RDS MySQL函数group_concat相关问题的处理


Group_concat 返回结果的长度


函数 group_concat 返回结果的长度受参数 group_concat_max_len 控制,默认值是 1024,即默认返回1024字节长度结果。

#参数名称默认值最小值最大值作用
1group_concat_max_len102441844674407370954752group_concat 函数返回结果的最大长度,单位字节

注:该参数可以在控制台 》 参数设置中配置(全局生效),也可以在会话级别设置(当前会话生效)。 set group_concat_max_len=90; -- 设置当前会话 group_concat_max_len 为 90 字节

show variables like 'group_concat_max_len'; -- 查看当前会话的 group_concat_max_len 值


select group_concat(distinct concat_ws(' ', t1.col0, t1.col2, t1.col3, t1.col4) separator "---")
from grp_con_test t1, grp_con_test t2 \G  -- 查询结果

select length(group_concat(distinct concat_ws(' ', t1.col0, t1.col2, t1.col3, t1.col4) separator "---"))
from grp_con_test t1, grp_con_test t2 \G  -- 查询结果的长度




2. Group_concat(distinct) 去除重复数据失效的处理



2.1 失效原因


当设置 group_concat_max_len 为较大值时,会出现使用 group_concat 和 distinct 来去除结果中重复数据失效的情况,比如: select group_concat(distinct concat_ws(' ', t1.col0, t1.col2, t1.col3, t1.col4) separator "---")
from grp_con_test t1, grp_con_test t2 \G -- 查询结果



可以看到,结果中出现多个重复值。
出现这个问题的原因是,当 group_concat 返回结果集比较大,则会出现内存临时表无法承载全部结果集数据,进而会使用磁盘临时表;而 group_concat 在使用磁盘临时表时会触发 bug 导致无法去除重复数据。 show variables like 'tmp_table_size'; -- 显示当前会话 tmp_table_size 参数设置



2.2 解决方法


调整 tmp_table_size 参数设置,增大内存临时表的最大尺寸。 set tmp_table_size=1*1024*1024 -- 设置当前会话 tmp_table_size 为 1 MB

show variables like 'tmp_table_size' -- 查看当前会话 tmp_table_size 的设置

select group_concat(distinct concat_ws(' ', t1.col0, t1.col2, t1.col3, t1.col4) separator "---")
from grp_con_test t1, grp_con_test t2 \G



注:
  • 1. Bug 信息请参考:Bug 68145
  • 2. 参数 tmp_table_size 可以在控制台 》参数设置中设置(全局生效,但对设置前已经连接的会话不生效),也可以在会话级别设置(当前会话生效)。

展开
收起
云栖大讲堂 2017-11-03 14:39:54 3350 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
搭建电商项目架构连接MySQL 立即下载
搭建4层电商项目架构,实战连接MySQL 立即下载
PolarDB MySQL引擎重磅功能及产品能力盛大发布 立即下载

相关镜像