本文介绍RDS MySQL函数group_concat相关问题。
group_concat返回结果的长度
函数group_concat返回结果的长度受参数group_concat_max_len控制,默认值为1024,即默认返回1024字节长度结果。
|
参数名称 |
默认值 |
取值范围 |
说明 |
|
group_concat_max_len |
1024 |
4-1844674407370954752 |
group_concat函数返回结果的最大长度,单位:Byte。 |
说明
您可以设置参数group_concat_max_len在全局生效或会话级别生效:
-
全局生效:在控制台的参数设置页面修改。
-
会话级别生效:
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 -- 查询结果的长度(xxx rds.aliyuncs.com) [xxx]> set group_concat_max_len=90; Query OK, 0 rows affected (0.01 sec) (xxx rds.aliyuncs.com) [xxx]> show variables like 'group_concat_max_len'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | group_concat_max_len | 90 | +----------------------------+-------+ 1 row in set (0.00 sec) (xxx rds.aliyuncs.com) [xxx]> 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. row *************************** group_concat(distinct concat_ws(' ', t1.col0, t1.col2, t1.col3, t1.col4) separator "---"): 45321 05182 45220 88257---75139 55255 97128 19379---97171 89436 20661 85001---59262 73787 1 row in set, 1 warning (0.01 sec) (xxx rds.aliyuncs.com) [xxx]> 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 *************************** 1. row *************************** length(group_concat(distinct concat_ws(' ', t1.col0, t1.col2, t1.col3, t1.col4) separator "---")): 90 1 row in set, 1 warning (0.02 sec)
group_concat(distinct) 去除重复数据失效的处理
失效原因
当设置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 -- 查询结果
(xxx.rds.aliyuncs.com) | > select group_concat(distinct concat_ws(' ', t1.col0, t1.col1, t1.col2, t1.col3, t1.col4) separator "---") from grp_con_test t1
, grp_con_test t1
*************************** 1. row ***************************
group_concat(distinct concat_ws(' ', t1.col0, t1.col1, t1.col2, t1.col3, t1.col4) separator "---"): 45321 05182 45220 88257---75139 55255 97128 19379---97131 88436 20661 85001---59362 23383 2
9041 80979---89864 87401 31529 07261---54624 49396 42912 06426---08688 74608 74890 40322---89467 14718 07344 73821---41783 14037 73678 87972---55996 32716 24598 08967---26178 81003 8
4733 78349---26047 76574 30695 42175---65277 93700 97433 42240---26097 47425 32176 41845---69564 49893 02433 47177---23629 25468 46071 67624---01025 88001 65114 41035---76123 62943 9
9610 56133---89515 16463 89633 24658---84970 31186 00867 43406---62661 28722 43255 73109---53961 60677 97953 97942---30573 56741 44436 20167---55002 79526 45470 51010---57662 73225 3
4549 07016---94770 82850 97352 68191---38597 08585 14319 46719---19560 13597 84008 10845---57317 11517 42312 43456---69855 41684 67835 36600---47618 99504 11335 85815---00229 79654 9
6269 42159---26178 27701 01952 36001---43767 12497 94788 31917---96370 98736 52494 05387---47299 97934 93395---90863 12133 15651 53028---35414 86467 12126 21752---15002 34570 22555 7
7741 86824---14333 10545 91593 99990---14863 74292 77703 80457---76695 93280 61204 43576---06234 62609 70594 59759---30696 75553 54027 78801 69795---97551 24691 64351---43882 64622 3
4531 70661---74399 34264 92401 49426---02511 19197 06782 72994---15555 65691 50972 07553 85827---36156 82073 39756 35502---90696 33553 85027 67958 73516---14845 19770 14176 62054 3
1647 54177---61410 22130 51062 93596---07174 83914 41673 14240---81317 14072 97939 90165---47553 81128 85626 60317---43915 71367 69649 46553 18895---59057 70160 56400 69795---44356 97410 2
6454 83132---80694 55283 90437 52629---61785 33552 84728 70045---69419 80816 30320 88121---41358 79545 54771 41222 58963 87801---20672---18060 85477 77177 01353 22015 81880 2
4389 47370---17812 50711 10862 53783---04046 27686 65441 60029---13515 77064 13184 40566 85496 76231 40821---01496---14160 80478 18630---26025 81833 2
1312 21258---34619 91590 74933 79973---41384 16243 74100 44331---64602 55976 43456---68015 34323 43377 09834 39924---21177 01353 74965 11705 83772 3
8939 44233---99714 84563 13293 41276---57883 05165 37533---27408 70968 34566---06815 43456 40110 40112 09834 39924---21177 01353 74965 11705 83772 3
5032 46711---68700 11802 14888 56778---83980 65 39562 73331---27408 70968 95 34456---68015 34323 43377 09834 39924---21177 01353 74965 11705 83772 3
9136 41710---81916 19 98001 33326 07847---52883 50432 49495 89325 08725 10993 43456 40110 40112
7128 19379---97171 89436 20661 85001---59262 23383 77387 13955---84874 47801 06451---54624 49396 42912 10696
3678 87972---55996 32716 24598 08967---26178 81003 84733 78349---26047 76574 30695 62751---65277 93700 97433 42240---26097 47425 32176 41845---69564 20439 47177---23629 22568 4
6071 67624---01025 88001 65114 41035---76123 62943 06426 89515---41345 89633 24658---84970 41186 80867 43406---22661 28722 43255 73109---30563 96677 79593---30535 64741 6
4436 20167---55002 79526 45470 51010---57662 73225 14549 07016---94770 97352 08191---38597 41684 13435 19770---14176 62054 9
7835 36600---47618 99504 11335 85815---00229 79654 62159---26178 27701 36001---47317 12427 94788 36001---47299 97934 93395---90863 12133 12555 7
5651 53028---35414 84667 12126 21752---15002 34570 67741 86824---14333 10545 15593 99990---18463 74292 77703 80457---76695 93280 24576 53234---26609 70592---09367 39924 2
6801 40920---95751 55991 24618 04351---49882 54653 47361 56177---64399 34264 92401 44926---02111 19197 14845 19770 14176 62054 3
4027 85272---60336 69758 33576 36023---41065 90733 13461 54177---61410 25082 93596---07174 83914 41673 14240 41072 97939 90165---47553
9854 01895---59057 17040 56400 69995---45356 97410 24654 83132---80694 55283 41785 35552 07553 85827---36156 82073
7801 60072---48010 16089 54977 11560---13505 86722 44389 47370---77812 50711 10862 53783---04046 27 83686 65441 60029---05602 23385---41488 07904 4
3921 28583---06154 80822 86078 80393---62025 81834 21312 21258---34619 91590 73543---41384 16243 74100 44331---64602 52976 81833 7
4796 43098---49636 03705 87837 22894---24186 98 09633 44233---93714 43529 13293---41276 58553 85827 83755 73516---45835 43456 3
9113 15934---78084 75226 61800 81268---25701 19182 06394 46711---81916 47618 16 19801 06553 90565 73373---27408 79683 88167 41845---68015 55235 81003 3
3657 99934---21312 75477 87933 77434---10991 43299 09136 41710---81916 98001---53847 45825 89301 53866 49835 85855 89467 14570 1
7507 87441---45321 05182 45220 88257---75139 55255 97128 19379---97171 89436 20661 85001---59262 73287 79041 80979---88864 84807 89529---26178 41529---54624 69485 14608 7
4890 40322---89467 14718 07344 73821---41783 14037 13678 87972---55996 23176 24598 80967---26178 84139---26047 74656 10345 63069---65277 17703 42240---26097 4
2176 41845---69564 49893 20439 47177---23629 22568 40671 67624---01025 68001 15114 41035---76123 09610 56133 89515 16463 10867 43406---26261 2
可以看到,结果中出现了多个重复值。出现这个问题的原因当group_concat返回结果集比较大,会出现内存临时表无法承载全部结果集数据,进而会使用磁盘临时表;而group_concat在使用磁盘临时表时会触发BUG导致无法去除重复数据。
解决方法
调整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
说明
您也可以在控制台的参数设置页面修改参数tmp_table_size。
group_concat和concat结合使用返回异常
异常原因
group_concat和concat结合使用某些情况下会出现返回BLOB字段类型的情况,例如:
select concat('{' ,group_concat(concat('\"payMoney' ,t.signature ,'\":' ,ifnull(t.money,0))) ,'}') payType
from my_money t
where cash_id='989898989898998898'
group by cash_id;
+---------+
| payType |
+---------+
| BLOB |
+---------+
1 row in set (0.00 sec)
这是由于函数concat按字节返回结果,如果concat的输入有多种类型,其结果是不可预期的。
解决方法
通过cast函数进行约束,使concat返回结果为字符串类型,将上面示例修改为:
select concat('{' ,cast(group_concat(concat('\"payMoney' ,t.signature ,'\":' ,ifnull(t.money,0))) as char) ,'}') payType
from my_money y t
where cash_id='989898989898998898'
group by cash_id;
+-------------------------+
| payType |
+-------------------------+
| {"payMoney1":500000.00} |
+-------------------------+
1 row in set (0.00 sec)
该文章对您有帮助吗?