How do I handle the issues related to the group_concat function in an ApsaraDB RDS for MySQL instance?

Updated at:

GROUP_CONCAT() aggregates multiple rows into a single delimited string. Three issues commonly occur in ApsaraDB RDS for MySQL: truncated output, failed deduplication when using DISTINCT, and an unexpected BLOB return type when combined with CONCAT(). All three trace back to the group_concat_max_len parameter.

Return value length

The group_concat_max_len parameter controls the maximum length of the string returned by GROUP_CONCAT(). The default is 1024 bytes, which truncates results silently when exceeded.

Parameter Default value Value range Description
group_concat_max_len 1024 4-1844674407370954752 Maximum length of the GROUP_CONCAT() return value. Unit: bytes.

The parameter can take effect globally or per session:

  • Global: Modify the parameter on the Parameters page of the ApsaraDB RDS console.

  • Per session: Run the following SQL commands.

-- Set group_concat_max_len to 90 for the current session
SET group_concat_max_len = 90;

-- Verify the value
SHOW VARIABLES LIKE 'group_concat_max_len';

-- Query the return value
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

-- Query the length of the return value
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

group_concat(distinct) fails to remove duplicate data

Cause

When group_concat_max_len is set to a large value, GROUP_CONCAT(DISTINCT ...) may return duplicate rows. The causal chain is:

  1. A large group_concat_max_len produces a large result set.

  2. The result set exceeds the memory temporary table capacity, so MySQL spills to a disk temporary table.

  3. A MySQL bug (ID 68145) in the disk temporary table code path prevents DISTINCT from removing duplicates.

Solution

Increase tmp_table_size to keep the temporary table in memory, which avoids the bug.

-- Set tmp_table_size to 1 MB for the current session
SET tmp_table_size = 1 * 1024 * 1024;

-- Verify the value
SHOW VARIABLES LIKE 'tmp_table_size';

-- Re-run the query to confirm deduplication works
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
To apply the change globally, modify the tmp_table_size parameter on the Parameters page of the ApsaraDB RDS console.

GROUP_CONCAT() returns BLOB when used inside CONCAT()

Cause

CONCAT() operates on bytes. When its arguments have mixed data types — including the BLOB type returned by GROUP_CONCAT() — the return type of CONCAT() is unpredictable and often resolves to BLOB.

Example query that triggers this behavior:

SELECT concat('{', group_concat(concat('"payMoney', t.signature, '":', ifnull(t.money, 0))), '}') AS payType
FROM my_money t
WHERE cash_id = '989898989898998898'
GROUP BY cash_id;

Solution

Use CAST(... AS CHAR) to convert the GROUP_CONCAT() result to the CHAR type before passing it to CONCAT().

SELECT concat('{', cast(group_concat(concat('"payMoney', t.signature, '":', ifnull(t.money, 0))) AS CHAR), '}') AS payType
FROM my_money y t
WHERE cash_id = '989898989898998898'
GROUP BY cash_id;