1. Group_concat 返回结果的长度
函数 group_concat 返回结果的长度受参数 group_concat_max_len 控制,默认值是 1024,即默认返回1024字节长度结果。
#
参数名称
默认值
最小值
最大值
作用
1
group_concat_max_len
1024
4
1844674407370954752
group_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 可以在控制台 参数设置中设置(全局生效,但对设置前已经连接的会话不生效),也可以在会话级别设置(当前会话生效)。
文章转载自:http://yun.jinre.com/newsinfo/781539.html