1 应用场景

Mycat 自带了多套数据分片的机制,其实根据数值取摸应该是最简单的一种。

优点:数据离散概率较为平均,可以有效的提高应用的数据吞吐。

缺点:比较明显,后期数据运维与迁移比较困难。好在Mycat有对应的解决方案,具体后期验证或可直接参考Mycat权威指南相应章节。

2 环境说明

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

3 参数配置

3.1 server.xml 配置

同上参考

3.2 schema.xml 配置

<!-- 配置 t_mod_long 数据表,分片规则为 mod-sharding-long ,数据结点有 4 个,注意在下面的 rule.xml 文件中其实只配置了3个, 因此dn7 不应该有数据才对 -->

<schema name="RANGEDB" checkSQLschema="false" sqlMaxLimit="100">
  <table name="t_range_date" dataNode="dn4,dn5,dn6,dn7,dn8,dn9,dn10,dn11" rule="sharding-by-date" />
  <table name="t_range_long" dataNode="dn4,dn5,dn6" rule="sharding-long" />
  <table name="t_mod_long" dataNode="dn4,dn5,dn6,dn7" rule="mod-sharding-long" />
</schema>

3.3 rule.xml 配置

<!-- 定义取摸运算规则 并设定 sharding_mod_id 为指定分片规则字段 -->

<tableRule name="mod-sharding-long">
  <rule>
    <columns>sharding_mod_id</columns>
    <algorithm>mod-long</algorithm>
  </rule>
</tableRule>

<!-- 取摸运算的具体实现类,数字节点设为3个 dn4\dn5\dn6  -->

<function name="mod-long" class="org.opencloudb.route.function.PartitionByMod">
  <!-- how many data nodes -->
  <property name="count">3</property>
</function>

4 数据验证

4.1 Mycat 建表

mysql> CREATE TABLE `t_mod_long` (
-> `id` INT NOT NULL,
-> `sharding_mod_id` VARCHAR(45) NULL,
-> `context` VARCHAR(45) NULL,
-> PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.02 sec)

4.2 数据插入与查询

insert into t_mod_long (id,sharding_mod_id,context) values (1,1,'test 1');
insert into t_mod_long (id,sharding_mod_id,context) values (2,2,'test 2');
insert into t_mod_long (id,sharding_mod_id,context) values (3,3,'test 3');
insert into t_mod_long (id,sharding_mod_id,context) values (4,4,'test 4');
insert into t_mod_long (id,sharding_mod_id,context) values (5,5,'test 5');
insert into t_mod_long (id,sharding_mod_id,context) values (6,6,'test 6');
insert into t_mod_long (id,sharding_mod_id,context) values (7,7,'test 7');
insert into t_mod_long (id,sharding_mod_id,context) values (8,8,'test 8');
insert into t_mod_long (id,sharding_mod_id,context) values (9,9,'test 9');
insert into t_mod_long (id,sharding_mod_id,context) values (10,10,'test 10');
insert into t_mod_long (id,sharding_mod_id,context) values (11,11,'test 11');
insert into t_mod_long (id,sharding_mod_id,context) values (12,12,'test 12');
insert into t_mod_long (id,sharding_mod_id,context) values (13,13,'test 13');
insert into t_mod_long (id,sharding_mod_id,context) values (14,14,'test 14');

Query OK, 1 row affected (0.01 sec)

...

mysql> select * from t_mod_long;
+----+-----------------+----------+
| id | sharding_mod_id | context |
+----+-----------------+----------+
| 3 | 3 | test 3 |
| 6 | 6 | test 6 |
| 9 | 9 | test 9 |
| 12 | 12 | test 12 |
| 1 | 1 | test 1 |
| 4 | 4 | test 4 |
| 7 | 7 | test 7 |
| 10 | 10 | test 10 |
| 13 | 13 | test 13 |
| 2 | 2 | test 2 |
| 5 | 5 | test 5 |
| 8 | 8 | test 8 |
| 11 | 11 | test 11 |
| 14 | 14 | test 14 |
+----+-----------------+----------+
14 rows in set (0.02 sec)

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

4.3 物理库查询

一共往数据库中新增记录 14 条,其实取摸后前 4 条记录进入 dn4 , dn5和dn6分别插入记录5条,dn7没有数据,验证通过。

mysql> select * from range_db_4.t_mod_long;
+----+-----------------+----------+
| id | sharding_mod_id | context |
+----+-----------------+----------+
| 3 | 3 | test 3 |
| 6 | 6 | test 6 |
| 9 | 9 | test 9 |
| 12 | 12 | test 12 |
+----+-----------------+----------+
4 rows in set (0.00 sec)

mysql> select * from range_db_5.t_mod_long;
+----+-----------------+----------+
| id | sharding_mod_id | context |
+----+-----------------+----------+
| 1 | 1 | test 1 |
| 4 | 4 | test 4 |
| 7 | 7 | test 7 |
| 10 | 10 | test 10 |
| 13 | 13 | test 13 |
+----+-----------------+----------+
5 rows in set (0.00 sec)

mysql> select * from range_db_6.t_mod_long;
+----+-----------------+----------+
| id | sharding_mod_id | context |
+----+-----------------+----------+
| 2 | 2 | test 2 |
| 5 | 5 | test 5 |
| 8 | 8 | test 8 |
| 11 | 11 | test 11 |
| 14 | 14 | test 14 |
+----+-----------------+----------+
5 rows in set (0.00 sec)

mysql> select * from range_db_7.t_mod_long;
Empty set (0.00 sec)

本篇完。

个人原创学习资料,若要转载,请加入原始地址 http://www.cnblogs.com/kaye0110/p/5162957.html

MyCat 学习笔记 第八篇.数据分片 之 求摸运算分片的更多相关文章

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

    1 应用场景 这篇来说下mycat中自带的er关系分片,所谓er关系分片即可以理解为有关联关系表之间数据分片.类似于订单主表与订单详情表间的分片存储规则. 本文所说的er分片分为两种: a. 依据主键 ...

  2. MyCat 学习笔记 第七篇.数据分片 之 按数据范围分片

    1 应用场景 Mycat 其实自带了2个数据范围分片的方案,一个是纯数据范围的分片,比如 1至 10000 号的数据放到分片1 ,10001 至 20000号数据放到分片2里. 另一个是数据常量形式的 ...

  3. MyCat 学习笔记 第六篇.数据分片 之 按月数据分片

    1 应用场景 Mycat 有很多数据分库规则,接下来几篇就相关觉得常用的规则进行试用与总结. 一般来说,按自然月份来进行数据分片的规则比较适用于商城订单查询,类似最近1周.2周.3个月内的数据.或是报 ...

  4. 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:  330 ...

  5. 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:  330 ...

  6. ES[7.6.x]学习笔记(八)数据的增删改

    在前面几节的内容中,我们学习索引.字段映射.分析器等,这些都是使用ES的基础,就像在数据库中创建表一样,基础工作做好以后,我们就要真正的使用它了,这一节我们要看看怎么向索引里写入数据.修改数据.删除数 ...

  7. Django学习笔记第八篇--实战练习四--为你的视图函数自定义装饰器

    零.背景: 对于登录后面所有视图函数,都需要验证登录信息,一般而言就是验证cookie里面的一些信息.所以你可以这么写函数: def personinfo(request): ": retu ...

  8. Vue.js学习笔记 第八篇 组件

    全局注册组件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...

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

    1 应用场景 Mycat 自带了多套数据分片的机制,其实根据数值分片也是比较简单,其实这个和数据取摸是类似的实现. 优.缺点同上一篇 2 环境说明 参考  <MyCat 学习笔记>第六篇. ...

随机推荐

  1. 【推荐】iOS集合视图的可重新排序的layout

    在实际项目中你或许会遇到在一个集合视图中移动一项到另外一个位置,那么此时我们需要对视图中的元素进行重新排序,今天推荐一个很好用的第三方类LXReorderableCollectionViewFlowL ...

  2. javascript运算符的优先级

    最基木的运算符优先级就是所谓的“先乘除,后加减”.对于优先顺序处于同一层次上的运算符,按照从左到右出现的顺序计算.下面给出javascript定义的所有运算符的优先级.运算符 优先顺序成员选择.括号. ...

  3. Import-Module ServerManager Import-Module : 未能加载指定的模块“ServerManager”,因为在任何模块目录中都没有找到有效模块文件...(通过Setup Factory调用PowerShell的脚本)

    操作系统: Windows server 2008 R2(64位) C:\Windows\System32\WindowsPowerShell\v1.0\Modules 下有ServerManager ...

  4. iis7 部署mvc4.0

    虽然用多了iis  但是很少重新去部署iis支持mvc4的环境,搞得每次需要的时候都要去百度, 所以现在自己写篇随笔来记录一下方便下次使用 建议先安装iis,再安装.net framework4.0, ...

  5. 获取datatable更新之前的数据

    string dd = ds.Tables[0].Rows[0][0, DataRowVersion.Original].ToString() ;

  6. C语言范例学习02

    第二章 指针 算是重点吧,这也是C语言的特色啊,直接访问物理存储. 重点: 指针就是一个存放它指向变量地址的变量,好绕口.   区分*在定义是与引用是的作用.   区分*.&的不同.   指针 ...

  7. Dalvik VM和JVM 的比较

    dx:dx工具用于将.class字节码(bytecode)转换为Android字节码(保存在.dex文件中)这个字节码文件 是给 Android 的 Java 虚拟机(Dalvik虚拟机)运行用的. ...

  8. Java编程思想读书笔记之内部类

    现在是够懒得了,放假的时候就想把这篇笔记写出来,一直拖到现在,最近在读<Java编程思想>,我想会做不止这一篇笔记,因为之前面试的时候总会问道一些内部类的问题,那这本书的笔记就从内部类开始 ...

  9. 经典实用jQuery soChange幻灯片实例演示

    soChange一款多很经典的幻灯片的jQuery插件. 实例预览 引入文件 <link rel="stylesheet" type="text/css" ...

  10. copy 和 strong(或retain)的区别

    http://stackoverflow.com/questions/18526909/whether-i-should-use-propertynonatomic-copy-or-propertyn ...