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)

本篇完。

MyCat 学习笔记 第九篇.数据分片 之 数值分布的更多相关文章

  1. Django学习笔记第九篇--实战练习五--关于数据的改、删操作、数据库字段属性的设置和类视图

    一.首先上代码.关于类视图: class register(View): #template_name = "templates/register.html" def get(se ...

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

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

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

    1 应用场景 Mycat 自带了多套数据分片的机制,其实根据数值取摸应该是最简单的一种. 优点:数据离散概率较为平均,可以有效的提高应用的数据吞吐. 缺点:比较明显,后期数据运维与迁移比较困难.好在M ...

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

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

  5. PHP学习笔记 - 进阶篇(11)

    PHP学习笔记 - 进阶篇(11) 数据库操作 PHP支持哪些数据库 PHP通过安装相应的扩展来实现数据库操作,现代应用程序的设计离不开数据库的应用,当前主流的数据库有MsSQL,MySQL,Syba ...

  6. PHP学习笔记 - 进阶篇(7)

    PHP学习笔记 - 进阶篇(7) 文件操作 读取文件内容 PHP具有丰富的文件操作函数,最简单的读取文件的函数为file_get_contents,可以将整个文件全部读取到一个字符串中. $conte ...

  7. PHP学习笔记 - 进阶篇(6)

    PHP学习笔记- 进阶篇(6) 会话控制(session与cookie) 当前的Cookie为: cookie简介 Cookie是存储在客户端浏览器中的数据,我们通过Cookie来跟踪与存储用户数据. ...

  8. PHP学习笔记 - 进阶篇(5)

    PHP学习笔记 - 进阶篇(5) 正则表达式 什么叫正则表达式 正则表达式是对字符串进行操作的一种逻辑公式,就是用一些特定的字符组合成一个规则字符串,称之为正则匹配模式. $p = '/apple/' ...

  9. PHP学习笔记 - 进阶篇(3)

    PHP学习笔记 - 进阶篇(3) 类与面向对象 1.类和对象 类是面向对象程序设计的基本概念,通俗的理解类就是对现实中某一个种类的东西的抽象, 比如汽车可以抽象为一个类,汽车拥有名字.轮胎.速度.重量 ...

随机推荐

  1. 新手学习Cocoapods教程

    CocoaPods简介 当你开发iOS应用时,会经常使用到很多第三方开源类库,比如AFNetWorking等等.手动去下载所需类库十分麻烦.另外一种常见情况是,你项目中用到的类库有更新,你必须得重新下 ...

  2. [Architect] Abp 框架原理解析(3) DynamicFilters

    本节目录 介绍 定义Filter 设置Filter 这是Abp中多租户.软删除.激活\禁用等如此方便的原因 Install-Package EntityFramework.DynamicFilters ...

  3. LeetCode - 38. Count and Say

    38. Count and Say Problem's Link ------------------------------------------------------------------- ...

  4. 组合数学 + STL --- 利用STL生成全排列

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  5. Matlab绘图(一二三维)

    Matlab绘图 强大的绘图功能是Matlab的特点之一,Matlab提供了一系列的绘图函数,用户不需要过多的考虑绘图的细节,只需要给出一些基本参数就能得到所需图形,这类函数称为高层绘图函数.此外,M ...

  6. Aspose.Words.Tables.Row类操作word表格行

    http://www.aspose.com/docs/display/wordsnet/Aspose.Words.Tables.Row+Class Retrieves the index of a r ...

  7. 【Unity】13.2 通过Lighting Window设置相关参数

    分类:Unity.C#.VS2015 创建日期:2016-05-19 一.简介 Unity 5.3.4的Lighting Window有3个选项卡:Object.Scene.Lightmaps. 二. ...

  8. js获取url传递的参数

    获取URL带参数的JAVASCRIPT客户端解决方案 一.正则分析法.(我较喜欢使用正则)function GetQueryString(name) {var reg = new RegExp(“(^ ...

  9. 004_URL 路由 - URL 路由

    在Web Form 情况下,每一个 ASPX页面既是一个文件,又是一个队请求自包含的响应.而在 MVC 情况下,请求是由控制器类中的动作方法处理的,而且与硬盘上的文件没有一对一的相互关系. ASP.N ...

  10. [转]PDO防注入原理分析以及使用PDO的注意事项

    原文:http://zhangxugg-163-com.iteye.com/blog/1835721 好文章不得不转. 我们都知道,只要合理正确使用PDO,可以基本上防止SQL注入的产生,本文主要回答 ...