legegecoder
legegecoder
发布于 2024-01-22 / 9 阅读 / 0 评论 / 0 点赞

Shardingjdbc踩过的坑

Shardingjdbc踩过的坑

此文针对于使用Shardingjdbc 4.x 版本

慎用MP的 updateById

除非你的分表规则是通过id来分片,其余情况都不能用updateById,Shardingjdbc在你的sql中找不到分片键会遍历每张表,如果你用了updateById 就对更新每张表 (4.0 没有对应机制开关)

出现 Can not find owner from table报错

// 重点是外层SQL不要出现 * ,不要使用别名,需要的字段都写清楚(内外层sql都要写清楚),才可以查出数据,不然要么是数据为空,要么是报错 Can not find owner from table.
// 正常工作
select resId,resType from (
        select id as resId, type as resType from test
) res where resId > 0 GROUP BY resType
// 没有数据
select * from (
        select id as resId, type as resType from test
) res where resId > 0 GROUP BY resType
// 没有数据
select * from (
        select * from test
)res where id > 0 GROUP BY res.type
// IllegalStateException : Can not find owner from table.
select * from (
        select * from test
) res where res.id > 0 GROUP BY res.type

UNION 场景

多表UNION Shardingjdbc只会替换第一个表的表名,如需要UNION 只能自己传表的index自己做多表查询

mybatis注释

sharding-jdbc不支持sql中的<!-- -->注释,如必须使用则写在sql前,或使用:/* */


评论