このトピックでは、ApsaraDB RDS for MySQLインスタンスのgroup_concat関数に関連する問題を処理する方法について説明します。
group_concatの戻り値の長さ
group_concat関数の戻り値の長さは、group_concat_max_lenパラメーターで指定します。 パラメーターのデフォルト値は1024です。これは、戻り値の長さが1,024バイトであることを示します。
パラメーター | デフォルト値 | 値の範囲 | 説明 |
group_concat_max_len | 1024 | 4-1844674407370954752 | group_concat関数の戻り値の最大長。 単位はバイトです。 |
group_concat_max_lenパラメーターの設定を設定して、グローバルまたは特定のセッションで有効にすることができます。
パラメーター設定をグローバルに有効にするには、ApsaraDB RDSコンソールの [パラメーター] ページでパラメーター設定を変更する必要があります。
特定のセッションでパラメーター設定を有効にするには、次のコマンドを実行する必要があります。
set group_concat_max_len=90; -- Set the group_concat_max_len parameter to 90 for the current session. show variables like 'group_concat_max_len'; -- Query the value of the group_concat_max_len parameter of the current session. 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 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 -- Query the length of the return value.
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 -- Query the return value.戻り値には、重複データの複数のエントリが含まれます。 group_concat関数が大きな結果セットを返した場合、メモリ一時テーブルは結果セット内のすべてのデータを処理できません。 この場合、ディスク一時テーブルが使用されます。 group_concat関数がディスク一時テーブルを照会すると、バグが発生し、重複データを削除できません。
解決策
次のコマンドを実行してtmp_table_sizeパラメーターを変更し、メモリ一時テーブルの最大サイズを大きくします。
set tmp_table_size=1*1024*1024 -- Set the tmp_table_size parameter to 1 MB for the current session.
show variables like 'tmp_table_size' -- Query the value of the tmp_table_size parameter of the current session.
select group_concat(distinct concat_ws(' ', t1.col0, t1.col2, t1.col3, t1.col4) separator "---")
from grp_con_test t1, grp_con_test t2 \GApsaraDB RDSコンソールの [パラメーター] ページで、tmp_table_sizeパラメーターを変更することもできます。
group_concatをconcatと一緒に使用すると、異常な値が返される問題の解決策
発生原因
concat関数と共にgroup_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;concat関数はバイト単位で値を返します。 関数に複数のデータ型のパラメーターが指定されている場合、関数の戻り値は予測できません。
解決策
キャスト関数を使用して、concat関数の戻り値をSTRING型に変換できます。 例:
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;