MyCat_分片&ER分片及其查询插入更新异常等问题

分片&ER分片

分片&ER分片说明

  • 分片会根据分片规则定位到不同的node中
  • ER分片的子表的parentId如果就是父表的分片字段,会直接根据此字段进行分片
  • ER分片的子表的parentId如果不是父表的分片字段,会查找父表所在的分片来确定字表的分片

order字段有限制

所有order的字段必须都在select字段中 select id form table1 order by name,查询报异常

分片数据更新异常

分片的表,update时不允许更新分片字段即便更新的内容与原内容一致也不允许

ER分片插入异常

当父表和子表数据在一个事务中提交时,如果字表的parentId不是父表的分片字段,
例如基本配置中的parentId为父表的ID字段,但是父表的分片字段为user_code不是ID。同时提交报错如下:
[Err] 1064 - can't find (root) parent sharding node for sql:。 ER分片需要查询父表所在的分片,如果是ER关系的表是在一个事务中插入数据是不可以的,只能分开提交。 或者使用冗余字段放弃使用ER分片。

配置如下:

<!-- rule.xml -->
    <tableRule name="test-rule">
        <rule>
            <columns>user_code</columns>
            <algorithm>test-rule-func</algorithm>
        </rule>
    </tableRule>
<!-- schema.xml -->
        <table name="z_user" primaryKey="ID" rule="test-rule" dataNode="dn0,dn1,dn2">
            <childTable parentKey="ID" name="z_order" joinKey="p_id"/>
        </table>

测试代码:

    public static void testAdd(DataSourceFactory ds) throws SQLException {
        int index = 4;
        Connection connection = ds.getConnection();
        connection.setAutoCommit(false);
        PreparedStatement ps1 = connection.prepareStatement("insert z_user (id,user_code,name) values (?,?,?) ");
        ps1.setString(1, "" + index);
        ps1.setString(2, "user" + index);
        ps1.setString(3, "用户" + index);
        ps1.executeUpdate();
        // 此处不提交,两个数据一起提交 
        PreparedStatement ps2 = connection.prepareStatement("insert z_order (id,p_id,name) values (?,?,?) ");
        ps2.setString(1, "" + index);
        ps2.setString(2, "" + index);
        ps2.setString(3, "订单" + index);
        ps2.executeUpdate();
        connection.commit();
        connection.close();
    }

转载于:https://my.oschina.net/sandant/blog/698890

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值