MyCat 学习笔记 第十篇.数据分片 之 ER分片

1 应用场景

这篇来说下mycat中自带的er关系分片,所谓er关系分片即可以理解为有关联关系表之间数据分片。类似于订单主表与订单详情表间的分片存储规则。

本文所说的er分片分为两种:

a. 依据主键进行数据分片,验证发现主表数据保存在第1个datanode中,子表数据根据分片规则存储。

b. 依据分片关键字段进行分片,验证发现主表与子表根据分片规则存储,且保存在相同的分片内。

接下来,可以下实际配置与数据验证

2 环境说明

参考  《MyCat 学习笔记》第六篇.数据分片 之 按月数据分片  http://www.cnblogs.com/kaye0110/p/5160826.html

3 参数配置

3.1 server.xml 配置

同上参考

3.2 schema.xml 配置

<schema name="RANGEDB" checkSQLschema="false" sqlMaxLimit="100">
  <!-- 方案a. 以主键进行分片 ,采用 sharding-by-pid 规则(根据 t_er_child_1.cid 的值 / 256 进行分片) -->

  <table name="t_er_parent_1" dataNode="dn$4-7" rule="sharding-by-pid">
    <childTable name="t_er_child_1" primaryKey="cid" joinKey="pid" parentKey="pid" />
  </table>

  <!-- 方案b.根据主表分片字段分页,子表数据会跟着主表走 -->

  <table name="t_er_order" dataNode="dn$4-7" rule="sharding-by-long">
    <childTable name="t_er_detail" primaryKey="detail_id" joinKey="order_id" parentKey="order_id" />
  </table>

</schema>

3.3 rule.xml 配置

<tableRule name="sharding-by-long">
  <rule>
    <columns>sharding_long</columns>
    <algorithm>func2</algorithm>
  </rule>
</tableRule>

<!--  partitionCount 与 partitionLength 相乘的值需要等于 1024  -->

<function name="func2" class="org.opencloudb.route.function.PartitionByLong">
  <property name="partitionCount">4</property>
  <property name="partitionLength">256</property>
</function>

4 数据验证

4.1 方案a的验证

CREATE TABLE `t_er_parent_1` (
`pid` INT NOT NULL,
`pname` VARCHAR(45) NULL,
PRIMARY KEY (`pid`));

CREATE TABLE `t_er_child_1` (
`cid` INT NOT NULL,
`pid` INT NOT NULL,
`cname` VARCHAR(45) NULL,
PRIMARY KEY (`cid`));

insert into t_er_parent_1 (pid,pname) values (1,‘parent 255‘);
insert into t_er_child_1(cid,pid,cname) values (1,255,‘child 255‘);

insert into t_er_parent_1 (pid,pname) values (2,‘parent 256‘);
insert into t_er_child_1(cid,pid,cname) values (2,256,‘child 256‘);

insert into t_er_parent_1 (pid,pname) values (3,‘parent 512‘);
insert into t_er_child_1(cid,pid,cname) values (3,512,‘child 512‘);

insert into t_er_parent_1 (pid,pname) values (4,‘parent 768‘);
insert into t_er_child_1(cid,pid,cname) values (4,768,‘child 768‘);

mysql> select * from t_er_parent_1;
+-----+------------+
| pid | pname |
+-----+------------+
| 1 | parent 255 |
| 2 | parent 256 |
| 3 | parent 512 |
| 4 | parent 768 |
+-----+------------+
4 rows in set (0.01 sec)

mysql> select * from t_er_child_1;
+-----+-----+-----------+
| cid | pid | cname |
+-----+-----+-----------+
| 1 | 255 | child 255 |
| 4 | 768 | child 768 |
| 2 | 256 | child 256 |
| 3 | 512 | child 512 |
+-----+-----+-----------+
4 rows in set (0.01 sec)

看下方案a的日志 ,所有主表数据都进入了第1个数据结点dn4,不过子表数据进入dn7

01/28 21:44:25.559 DEBUG [$_NIOREACTOR-0-RW] (ServerQueryHandler.java:56) -ServerConnection [id=2, schema=RANGEDB, host=0:0:0:0:0:0:0:1, user=test,txIsolation=3, autocommit=true, schema=RANGEDB]insert into t_er_parent_1 (pid,pname) values (4,‘parent 768‘)
01/28 21:44:25.559 DEBUG [$_NIOREACTOR-0-RW] (NonBlockingSession.java:113) -ServerConnection [id=2, schema=RANGEDB, host=0:0:0:0:0:0:0:1, user=test,txIsolation=3, autocommit=true, schema=RANGEDB]insert into t_er_parent_1 (pid,pname) values (4,‘parent 768‘), route={
1 -> dn4{insert into t_er_parent_1 (pid,pname) values (4,‘parent 768‘)}
} rrs
01/28 21:44:25.562 DEBUG [$_NIOREACTOR-1-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=15, lastTime=1453988665553, user=root, schema=range_db_4, old shema=range_db_4, borrowed=true, fromSlaveDB=false, threadId=179, charset=utf8, txIsolation=3, autocommit=true, attachment=dn4{insert into t_er_parent_1 (pid,pname) values (4,‘parent 768‘)}, respHandler=SingleNodeHandler [node=dn4{insert into t_er_parent_1 (pid,pname) values (4,‘parent 768‘)}, packetId=0], host=localhost, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]
01/28 21:44:25.562 DEBUG [$_NIOREACTOR-1-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=15, lastTime=1453988665553, user=root, schema=range_db_4, old shema=range_db_4, borrowed=true, fromSlaveDB=false, threadId=179, charset=utf8, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=localhost, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
01/28 21:44:25.564 DEBUG [$_NIOREACTOR-0-RW] (ServerQueryHandler.java:56) -ServerConnection [id=2, schema=RANGEDB, host=0:0:0:0:0:0:0:1, user=test,txIsolation=3, autocommit=true, schema=RANGEDB]insert into t_er_child_1(cid,pid,cname) values (4,768,‘child 768‘)
01/28 21:44:25.565 DEBUG [$_NIOREACTOR-0-RW] (RouterUtil.java:650) -found partion node (using parent partion rule directly) for child table to insert dn7 sql :INSERT INTO t_er_child_1 (cid, pid, cname)
VALUES (4, 768, ‘child 768‘)
01/28 21:44:25.565 DEBUG [$_NIOREACTOR-0-RW] (NonBlockingSession.java:113) -ServerConnection [id=2, schema=RANGEDB, host=0:0:0:0:0:0:0:1, user=test,txIsolation=3, autocommit=true, schema=RANGEDB]insert into t_er_child_1(cid,pid,cname) values (4,768,‘child 768‘), route={
1 -> dn7{INSERT INTO t_er_child_1 (cid, pid, cname)
VALUES (4, 768, ‘child 768‘)}
} rrs

看下物理表的数据 range_db_4 对应的就是dn4结点。

mysql> select * from range_db_4.t_er_parent_1;

select * from range_db_4.t_er_child_1;
+-----+------------+
| pid | pname |
+-----+------------+
| 1 | parent 255 |
| 2 | parent 256 |
| 3 | parent 512 |
| 4 | parent 768 |
+-----+------------+
4 rows in set (0.00 sec)

+-----+-----+-----------+
| cid | pid | cname |
+-----+-----+-----------+
| 1 | 255 | child 255 |
+-----+-----+-----------+
1 row in set (0.00 sec)

4.2 方案b的验证

CREATE TABLE `t_er_order` (
`order_id` INT NOT NULL,
`prod_info` VARCHAR(45) NULL,
`sharding_long` VARCHAR(45) NULL,
PRIMARY KEY (`order_id`));

CREATE TABLE `t_er_detail` (
`detail_id` INT NOT NULL,
`order_id` INT NOT NULL,
`detail_info` INT NOT NULL,
PRIMARY KEY (`detail_id`));

insert into t_er_order (order_id,prod_info,sharding_long) values (1,‘prod_1‘,200);
insert into t_er_order (order_id,prod_info,sharding_long) values (2,‘prod_256‘,256);
insert into t_er_order (order_id,prod_info,sharding_long) values (3,‘prod_512‘,512);
insert into t_er_order (order_id,prod_info,sharding_long) values (4,‘prod_1024‘,1024);
insert into t_er_order (order_id,prod_info,sharding_long) values (5,‘prod_1025‘,1025);
insert into t_er_order (order_id,prod_info,sharding_long) values (6,‘prod_795‘,795);

insert into t_er_detail (detail_id,order_id,detail_info) values (1,1,200);
insert into t_er_detail (detail_id,order_id,detail_info) values (2,2,256);
insert into t_er_detail (detail_id,order_id,detail_info) values (3,3,512);
insert into t_er_detail (detail_id,order_id,detail_info) values (4,4,1024);
insert into t_er_detail (detail_id,order_id,detail_info) values (5,5,1025);
insert into t_er_detail (detail_id,order_id,detail_info) values (6,6,795);

mysql> select * from t_er_order;
+----------+-----------+---------------+
| order_id | prod_info | sharding_long |
+----------+-----------+---------------+
| 1 | prod_1 | 200 |
| 4 | prod_1024 | 1024 |
| 5 | prod_1025 | 1025 |
| 2 | prod_256 | 256 |
| 3 | prod_512 | 512 |
| 6 | prod_795 | 795 |
+----------+-----------+---------------+
6 rows in set (0.08 sec)

mysql> select * from t_er_detail;
+-----------+----------+-------------+
| detail_id | order_id | detail_info |
+-----------+----------+-------------+
| 2 | 2 | 256 |
| 1 | 1 | 200 |
| 4 | 4 | 1024 |
| 5 | 5 | 1025 |
| 3 | 3 | 512 |
| 6 | 6 | 795 |
+-----------+----------+-------------+
6 rows in set (0.01 sec)

查询物理表

mysql> select * from range_db_6.t_er_order;select * from range_db_6.t_er_detail;

+----------+-----------+---------------+
| order_id | prod_info | sharding_long |
+----------+-----------+---------------+
| 3 | prod_512 | 512 |
+----------+-----------+---------------+
1 row in set (0.00 sec)

+-----------+----------+-------------+
| detail_id | order_id | detail_info |
+-----------+----------+-------------+
| 3 | 3 | 512 |
+-----------+----------+-------------+
1 row in set (0.00 sec)

再来看下日志

新增主表数据,根据分片规则数据应该进入第2个数据结点 dn5

01/28 20:50:10.274 DEBUG [$_NIOREACTOR-0-RW] (NonBlockingSession.java:113) -ServerConnection [id=1, schema=RANGEDB, host=0:0:0:0:0:0:0:1, user=test,txIsolation=3, autocommit=true, schema=RANGEDB]insert into t_er_order (order_id,prod_info,sharding_long) values (2,‘prod_256‘,256), route={
1 -> dn5{insert into t_er_order (order_id,prod_info,sharding_long) values (2,‘prod_256‘,256)}
} rrs

份数据库中进行主键搜索确认,并进入数据缓存

01/28 20:50:10.544 DEBUG [BusinessExecutor2] (EnchachePool.java:76) -ER_SQL2PARENTID miss cache ,key:RANGEDB:select t_er_order.order_id from t_er_order where t_er_order.order_id=2
01/28 20:50:10.544 DEBUG [BusinessExecutor2] (FetchStoreNodeOfChildTableHandler.java:73) -find child node with sql:select t_er_order.order_id from t_er_order where t_er_order.order_id=2
01/28 20:50:10.545 DEBUG [BusinessExecutor2] (FetchStoreNodeOfChildTableHandler.java:81) -execute in datanode dn4
01/28 20:50:10.545 DEBUG [BusinessExecutor2] (PhysicalDBPool.java:452) -select read source hostM3306 for dataHost:localhost3306
01/28 20:50:10.545 DEBUG [$_NIOREACTOR-0-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=16, lastTime=1453985410540, user=root, schema=range_db_4, old shema=range_db_4, borrowed=true, fromSlaveDB=false, threadId=175, charset=utf8, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=localhost, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
01/28 20:50:10.745 DEBUG [BusinessExecutor2] (FetchStoreNodeOfChildTableHandler.java:81) -execute in datanode dn5
01/28 20:50:10.745 DEBUG [BusinessExecutor2] (PhysicalDBPool.java:452) -select read source hostM3306 for dataHost:localhost3306
01/28 20:50:10.746 DEBUG [$_NIOREACTOR-1-RW] (FetchStoreNodeOfChildTableHandler.java:154) -received rowResponse response,2 from MySQLConnection [id=19, lastTime=1453985410743, user=root, schema=range_db_5, old shema=range_db_5, borrowed=true, fromSlaveDB=false, threadId=176, charset=utf8, txIsolation=3, autocommit=true, attachment=dn5, respHandler=org[email protected]6be897cc, host=localhost, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
01/28 20:50:10.746 DEBUG [$_NIOREACTOR-1-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=19, lastTime=1453985410743, user=root, schema=range_db_5, old shema=range_db_5, borrowed=true, fromSlaveDB=false, threadId=176, charset=utf8, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=localhost, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]

明确插入dn5结点
01/28 20:50:10.945 DEBUG [BusinessExecutor0] (RouterUtil.java:1213) -found partion node for child table to insert dn5 sql :insert into t_er_detail (detail_id,order_id,detail_info) values (2,2,256)

本篇完。

时间: 2024-12-25 21:53:18

MyCat 学习笔记 第十篇.数据分片 之 ER分片的相关文章

MyCat 学习笔记 第十一篇.数据分片 之 分片事务处理

1 环境说明 VM 模拟3台MYSQL 5.6 服务器 VM1 192.168.31.187:3307 VM2 192.168.31.212:3307 VM3 192.168.31.150:  3307 MYCAT 1.5 服务部署在宿主机上 MYCAT 192.168.31.207 :8806[SQL执行端口] / 9066[管理端口] 2 应用场景 2.0 MYCAT配置 schema.xml <schema name="TESTDB" checkSQLschema=&quo

MyCat 学习笔记 第十一篇.数据分片 之 分片数据查询 ( select * from table_name limit 100000,100 )

1 环境说明 VM 模拟3台MYSQL 5.6 服务器 VM1 192.168.31.187:3307 VM2 192.168.31.212:3307 VM3 192.168.31.150:  3307 MYCAT 1.5 服务部署在宿主机上 MYCAT 192.168.31.207 :8806[SQL执行端口] / 9066[管理端口] 2 应用场景 2.0 MYCAT配置 schema.xml <schema name="TESTDB" checkSQLschema=&quo

MyCat 学习笔记 第十三篇.数据分片 之 通过HINT执行存储过程

1 环境说明 VM 模拟3台MYSQL 5.6 服务器 VM1 192.168.31.187:3307 VM2 192.168.31.212:3307 VM3 192.168.31.150:  3307 MYCAT 1.5 服务部署在宿主机上 MYCAT 192.168.31.207 :8806[SQL执行端口] / 9066[管理端口] 2 应用场景 2.0 MYCAT配置 schema.xml <schema name="TESTDB" checkSQLschema=&quo

《Mycat学习笔记》 第二篇. MySql 读写分离与日志分析——主从多结点

1    环境说明 接上篇环境   <Mycat学习笔记> 第一篇. MySql 读写分离与日志分析——主从单结点 http://www.cnblogs.com/kaye0110/p/5134588.html 增加一套 mysql 实例,端口为3308 ,通过Binlog方式同步主机情况 localhost : 3306 主机,    在mycat 中配置为 writehost 1 localhost : 3307 从机 a ,在mycat 中配置为 readhost localhost :

《Mycat学习笔记》 第三篇. MySql 主从同步异常后,主从切换

1)系统环境说明 MySql 5.5 主从节点 127.0.0.1:3306   主结点,为验证主从切换效果,手动停止服务 127.0.0.1: 3307    从结点 1 127.0.0.1:338     从结点 2 ,为验证主从切换效果,在主结点停止后,新增两个记录. MyCat 1.5 schema.xml 配置 具体配置说明,参考上篇: <Mycat学习笔记> 第二篇. MySql 读写分离与日志分析——主从多结点 <dataHost name="localhost1

《MyCat 学习笔记》第八篇.数据分片 之 求摸运算分片

1 应用场景 Mycat 自带了多套数据分片的机制,其实根据数值取摸应该是最简单的一种. 优点:数据离散概率较为平均,可以有效的提高应用的数据吞吐. 缺点:比较明显,后期数据运维与迁移比较困难.好在Mycat有对应的解决方案,具体后期验证或可直接参考Mycat权威指南相应章节. 2 环境说明 参考  <MyCat 学习笔记>第六篇.数据分片 之 按月数据分片  http://www.cnblogs.com/kaye0110/p/5160826.html 3 参数配置 3.1 server.xm

Android学习笔记(十二)——使用意图传递数据的几种方式

使用意图传递数据的几种方式 点此获取完整代码 我们除了要从活动返回数据,也常常要传递数据给活动.对此我们可以使用Intent对象将这些数据传递给目标活动. 1.创建一个名为PassingData的项目,在activity_main.xml文件中添加一个Button: <Button android:id="@+id/btn_SecondActivity" android:layout_width="fill_parent" android:layout_hei

SaltStack 学习笔记 - 第十二篇: SaltStack Web 界面

SaltStack 有自身的用python开发的web界面halite,好处是基于python,可以跟salt的api无缝配合,确定就比较明显,需要个性化对web界面进行定制的会比较麻烦,如果喜欢体验该界面的可以参考下面的文章  http://rfyiamcool.blog.51cto.com/1030776/1275443/ 我是运用另一个python+php来进行web开发,具体需要的工具有在我的另一篇文章里面介绍过,这里再重新进行整个开发介绍 首先介绍php 跟python通信的工具 pp

UI学习笔记---第十四天数据持久化

一.沙盒机制 每个应用程序位于文件系统的严格限制部分 每个应用程序只能在为该程序创建的文件系统中读取文件 每个应用程序在iOS系统内斗放在了统一的文件夹目录下 沙盘路径的位置 1. 通过Finder查找程序沙盘相对路径 ~/Library/Application Support/iPhone Simulator 2. 通过代码查找程序沙盘相对路径 NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory,NSSearc