记一次OceanBase的线上问题排查

发布时间 2023-12-02 18:16:14作者: dongzhiwei-blog

问题是什么

数据库报错 Error 1366 (HY000): Incorrect string value

具体情况复现

插入语句

insert ignore into user(
    name, 
    disc_content
) 
select
    t1.name,
    group_concat(
        concat('{"评论人":"', t1.author, '","解决人":"', t1.resolved_name, '","评论时间":"', t1.note_time, '","评论内容":"', t1.body, '"}')
        ) as disc_content,
    form
        date_detail as t1
    group by user_id

出现错误原因

concat()函数默认1024字节,超过1024,可能导致数据截断。从而使出现一些OceanBase不能插入的字符(mysql并不会出现同样原因)

解决方案

使用json_object()函数替换

insert ignore into user(
    name, 
    disc_content
) 
select
    t1.name,
    json_arrayagg(
        json_object(
            '评论人', t1.author,
            '解决人', t1.resolved_name,
            '评论时间', t1.note_time,
            '评论内容', t1.body
        )
    ) as disc_content,
    form
        date_detail as t1
    group by user_id