MyCat 学习笔记 第九篇.数据分片 之 数值分布

1 应用场景

Mycat 自带了多套数据分片的机制,其实根据数值分片也是比较简单,其实这个和数据取摸是类似的实现。

优、缺点同上一篇

2 环境说明

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

3 参数配置

3.1 server.xml 配置

同上参考

3.2 schema.xml 配置

<!-- 配置 t_sharding_long 数据表,分片规则为 sharding-by-long ,数据结点有 4 个 -->

<schema name="RANGEDB" checkSQLschema="false" sqlMaxLimit="100">

  <table name="t_sharding_long" dataNode="dn4,dn5,dn6,dn7" rule="sharding-by-long" />
</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 Mycat 建表

mysql> CREATE TABLE t_sharding_long (
-> `id` INT NOT NULL,
-> `sharding_long` VARCHAR(45) NULL,
-> `context` VARCHAR(45) NULL,
-> PRIMARY KEY (`id`));
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 1
Current database: RANGEDB

4.2 数据插入与查询

insert into t_sharding_long (id,sharding_long,context) values (1,255,'test 255');
insert into t_sharding_long (id,sharding_long,context) values (2,256,'test 256');
insert into t_sharding_long (id,sharding_long,context) values (3,257,'test 257');
insert into t_sharding_long (id,sharding_long,context) values (4,511,'test 511');
insert into t_sharding_long (id,sharding_long,context) values (5,512,'test 512');
insert into t_sharding_long (id,sharding_long,context) values (6,513,'test 513');
insert into t_sharding_long (id,sharding_long,context) values (7,767,'test 767');
insert into t_sharding_long (id,sharding_long,context) values (8,768,'test 768');
insert into t_sharding_long (id,sharding_long,context) values (9,769,'test 769');
insert into t_sharding_long (id,sharding_long,context) values (10,1023,'test 1023');
insert into t_sharding_long (id,sharding_long,context) values (11,1024,'test 1024');
insert into t_sharding_long (id,sharding_long,context) values (12,1025,'test 1025');

Query OK, 1 row affected (0.01 sec)

...

mysql> select * from t_sharding_long;
+----+---------------+------------+
| id | sharding_long | context |
+----+---------------+------------+
| 2 | 256 | test 256 |
| 3 | 257 | test 257 |
| 4 | 511 | test 511 |
| 1 | 255 | test 255 |
| 11 | 1024 | test 1024 |
| 12 | 1025 | test 1025 |
| 5 | 512 | test 512 |
| 6 | 513 | test 513 |
| 7 | 767 | test 767 |
| 8 | 768 | test 768 |
| 9 | 769 | test 769 |
| 10 | 1023 | test 1023 |
+----+---------------+------------+
12 rows in set (0.06 sec)

其实从数据反馈的角度已经可以很明确的发现,当前记录是进入了分片规则。

4.3 物理库查询

一共往数据库中新增记录 10 条,sharding_long字段除256后进行不同的分片,验证通过。

mysql> select * from range_db_4.t_sharding_long;
+----+---------------+------------+
| id | sharding_long | context |
+----+---------------+------------+
| 1 | 255 | test 255 |
| 11 | 1024 | test 1024 |
| 12 | 1025 | test 1025 |
+----+---------------+------------+
3 rows in set (0.00 sec)

mysql> select * from range_db_5.t_sharding_long;
+----+---------------+-----------+
| id | sharding_long | context |
+----+---------------+-----------+
| 2 | 256 | test 256 |
| 3 | 257 | test 257 |
| 4 | 511 | test 511 |
+----+---------------+-----------+
3 rows in set (0.00 sec)

mysql> select * from range_db_6.t_sharding_long;
+----+---------------+-----------+
| id | sharding_long | context |
+----+---------------+-----------+
| 5 | 512 | test 512 |
| 6 | 513 | test 513 |
| 7 | 767 | test 767 |
+----+---------------+-----------+
3 rows in set (0.00 sec)

mysql> select * from range_db_7.t_sharding_long;
+----+---------------+------------+
| id | sharding_long | context |
+----+---------------+------------+
| 8 | 768 | test 768 |
| 9 | 769 | test 769 |
| 10 | 1023 | test 1023 |
+----+---------------+------------+
3 rows in set (0.00 sec)

本篇完。

上一篇:Python学习第十三篇——函数的深层次运用


下一篇:HDU2859 Phalanx 简单DP