1,自己定义数字范围分片

自己定义数字范围分片,提前规划好分片字段某个范围属于哪个分片,比方说将第一个500W的数据分片在第一个节点上面。第二个500W的数据分片在第二个节点上,依次类推


2,加入配置文件

在function.xml里面进行配置:

    <!-- 对自己定义数字分片规则rang-long-tr04的定义 -->
<function name="rang-long-04" class="org.opencloudb.route.function.AutoPartitionByLong">
<property name="mapFile">autopartition-long04.txt</property>
<property name="defaultNode">0</property>
<property name="type">0</property>
</function> <!-- 对表进行分片的描写叙述。字段以及分片规则 -->
<tableRule name="rang-long-tr04">
<rule>
<columns>ID</columns>
<algorithm>rang-long-04</algorithm>
</rule>
</tableRule>

在schema.xml里面进行配置:

    <table name="ORDER0401" primaryKey="ID" dataNode="dn21,dn22" rule="rang-long-tr04"/>

autopartition-long04.txt文件的配置:

    [root@crm_idc_squid_1_11 conf]# more autopartition-long04.txt
0-500M=0
500M-1000M=1
1000M-1500M=2
You have mail in /var/spool/mail/root
[root@crm_idc_squid_1_11 conf]#

注意: 此配置很easy。即预先制定可能的id范围到某个分片。全部的节点配置都是从0開始。及0代表节点1


3,建表而且录入数据

    CREATE TABLE ORDER0401(ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,PROVINCE VARCHAR(16),SN VARCHAR(64),CREATE_TIME DATETIME);
mysql> CREATE TABLE ORDER0401(ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,PROVINCE VARCHAR(16),SN VARCHAR(64),CREATE_TIME DATETIME);
Query OK, 0 rows affected (0.05 sec) mysql>
INSERT INTO ORDER0401(ID,PROVINCE,SN,CREATE_TIME) VALUES(10001,'shanghai','shanghai10006_10000',NOW());
INSERT INTO ORDER0401(ID,PROVINCE,SN,CREATE_TIME) VALUES(5000001,'shanghai','shanghai10006_5000001',NOW());
mysql> explain INSERT INTO ORDER0401(ID,PROVINCE,SN,CREATE_TIME) VALUES(10001,'shanghai','shanghai10006_10000',NOW());
+-----------+--------------------------------------------------------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+--------------------------------------------------------------------------------------------------------+
| dn21 | INSERT INTO ORDER0401(ID,PROVINCE,SN,CREATE_TIME) VALUES(10001,'shanghai','shanghai10006_10000',NOW()) |
+-----------+--------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec) mysql> INSERT INTO ORDER0401(ID,PROVINCE,SN,CREATE_TIME) VALUES(10001,'shanghai','shanghai10006_10000',NOW());
Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO ORDER0401(ID,PROVINCE,SN,CREATE_TIME) VALUES(5000001,'shanghai','shanghai10006_5000001',NOW());
Query OK, 1 row affected (0.00 sec) mysql>

4,对insert操作流程进行分析。參考mycat.log对路由过程做完整的分析

查看后台mycat.log分析例如以下:

開始获取连接:
02/12 21:17:13.614 DEBUG [$_NIOREACTOR-1-RW] (ServerQueryHandler.java:56) -ServerConnection [id=1, schema=TESTDB, host=127.0.0.1, user=test,txIsolation=3, autocommit=true, schema=TESTDB]INSERT INTO ORDER0401(ID,PROVINCE,SN,CREATE_TIME) VALUES(10001,'shanghai','shanghai10006_10000',NOW()) 開始建立缓存:
02/12 21:17:13.615 DEBUG [$_NIOREACTOR-1-RW] (RouterUtil.java:951) -try to find cache by primary key
02/12 21:17:13.616 INFO [$_NIOREACTOR-1-RW] (DefaultLayedCachePool.java:80) -create child Cache: TESTDB_ORDER0401 for layered cache TableID2DataNodeCache, size 10000, expire seconds 18000
02/12 21:17:13.616 DEBUG [$_NIOREACTOR-1-RW] (CacheManager.java:794) -Attempting to create an existing singleton. Existing singleton returned.
02/12 21:17:13.616 DEBUG [$_NIOREACTOR-1-RW] (Cache.java:955) -No BootstrapCacheLoaderFactory class specified. Skipping...
02/12 21:17:13.617 DEBUG [$_NIOREACTOR-1-RW] (Cache.java:929) -CacheWriter factory not configured. Skipping...
02/12 21:17:13.618 DEBUG [$_NIOREACTOR-1-RW] (MemoryStore.java:153) -Initialized net.sf.ehcache.store.NotifyingMemoryStore for TableID2DataNodeCache.TESTDB_ORDER0401
02/12 21:17:13.619 DEBUG [$_NIOREACTOR-1-RW] (Cache.java:1165) -Initialised cache: TableID2DataNodeCache.TESTDB_ORDER0401 開始路由匹配:
02/12 21:17:13.621 DEBUG [$_NIOREACTOR-1-RW] (NonBlockingSession.java:113) -ServerConnection [id=1, schema=TESTDB, host=127.0.0.1, user=test,txIsolation=3, autocommit=true, schema=TESTDB]INSERT INTO ORDER0401(ID,PROVINCE,SN,CREATE_TIME) VALUES(10001,'shanghai','shanghai10006_10000',NOW()), route={
1 -> dn21{INSERT INTO ORDER0401(ID,PROVINCE,SN,CREATE_TIME) VALUES(10001,'shanghai','shanghai10006_10000',NOW())}
} rrs 匹配好路由后。直接往相应的dn21节点開始运行insert数据录入操作:
02/12 21:17:13.624 DEBUG [$_NIOREACTOR-0-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=8, lastTime=1455283033608, user=root, schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false, threadId=52, charset=latin1, txIsolation=3, autocommit=true, attachment=dn21{INSERT INTO ORDER0401(ID,PROVINCE,SN,CREATE_TIME) VALUES(10001,'shanghai','shanghai10006_10000',NOW())}, respHandler=SingleNodeHandler [node=dn21{INSERT INTO ORDER0401(ID,PROVINCE,SN,CREATE_TIME) VALUES(10001,'shanghai','shanghai10006_10000',NOW())}, packetId=0], host=13.217.1.11, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=true] 连接释放:
02/12 21:17:13.624 DEBUG [$_NIOREACTOR-0-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=8, lastTime=1455283033608, user=root, schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false, threadId=52, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=13.217.1.11, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]

5,运行简单的查询。假设指定分片字段ID字段,则走分片查询单个分片节点

mycat命令台显示结果:

    mysql> explain select * from ORDER0401 t1 where t1.ID=5000001;
+-----------+------------------------------------------------+
| DATA_NODE | SQL |
+-----------+------------------------------------------------+
| dn22 | select * from ORDER0401 t1 where t1.ID=5000001 |
+-----------+------------------------------------------------+
1 row in set (0.08 sec) mysql> select * from ORDER0401 t1 where t1.ID=5000001;
+---------+----------+-----------------------+---------------------+
| ID | PROVINCE | SN | CREATE_TIME |
+---------+----------+-----------------------+---------------------+
| 5000001 | shanghai | shanghai10006_5000001 | 2016-02-12 21:18:17 |
+---------+----------+-----------------------+---------------------+
1 row in set (0.00 sec) mysql>

查看后台mycat日志信息:

    02/12 21:44:01.116  DEBUG [$_NIOREACTOR-1-RW] (ServerQueryHandler.java:56) -ServerConnection [id=1, schema=TESTDB, host=127.0.0.1, user=test,txIsolation=3, autocommit=true, schema=TESTDB]select * from ORDER0401 t1 where t1.ID=5000001
02/12 21:44:01.117 DEBUG [$_NIOREACTOR-1-RW] (EnchachePool.java:76) -SQLRouteCache miss cache ,key:TESTDBselect * from ORDER0401 t1 where t1.ID=5000001
02/12 21:44:01.118 DEBUG [$_NIOREACTOR-1-RW] (RouterUtil.java:951) -try to find cache by primary key
02/12 21:44:01.118 DEBUG [$_NIOREACTOR-1-RW] (EnchachePool.java:76) -TableID2DataNodeCache.TESTDB_ORDER0401 miss cache ,key:5000001
02/12 21:44:01.118 DEBUG [$_NIOREACTOR-1-RW] (NonBlockingSession.java:113) -ServerConnection [id=1, schema=TESTDB, host=127.0.0.1, user=test,txIsolation=3, autocommit=true, schema=TESTDB]select * from ORDER0401 t1 where t1.ID=5000001, route={
1 -> dn22{select * from ORDER0401 t1 where t1.ID=5000001}
} rrs
02/12 21:44:01.119 DEBUG [$_NIOREACTOR-1-RW] (PhysicalDBPool.java:452) -select read source hostM2 for dataHost:m2
02/12 21:44:01.120 DEBUG [$_NIOREACTOR-1-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=17, lastTime=1455284641106, user=root, schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false, threadId=59, charset=latin1, txIsolation=3, autocommit=true, attachment=dn22{select * from ORDER0401 t1 where t1.ID=5000001}, respHandler=SingleNodeHandler [node=dn22{select * from ORDER0401 t1 where t1.ID=5000001}, packetId=7], host=13.217.1.11, port=3327, statusSync=org.opencloudb.mysql.nio.MySQLConnection$StatusSync@6d1ef03, writeQueue=0, modifiedSQLExecuted=false]
02/12 21:44:01.121 DEBUG [$_NIOREACTOR-1-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=17, lastTime=1455284641106, user=root, schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false, threadId=59, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=13.217.1.11, port=3327, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]

6。假设分片字段范围的查询,则走全部节点去检索,哪怕仅仅有一条数据在一个分片上。route路由也是走全部的分片进行检索查询

    mysql> explain select * from ORDER0401 t1 where t1.ID<50000;
+-----------+----------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+----------------------------------------------------------+
| dn21 | SELECT * FROM ORDER0401 t1 WHERE t1.ID < 50000 LIMIT 100 |
| dn22 | SELECT * FROM ORDER0401 t1 WHERE t1.ID < 50000 LIMIT 100 |
+-----------+----------------------------------------------------------+
2 rows in set (0.01 sec) mysql>

7,假设不走分片字段的查询。即使是单个数据,也要route路由全部的分片,走全部的分片进行查询

    mysql> explain select * from ORDER0401 t1 where t1.SN='shanghai10006_10000';
+-----------+--------------------------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+--------------------------------------------------------------------------+
| dn21 | SELECT * FROM ORDER0401 t1 WHERE t1.SN = 'shanghai10006_10000' LIMIT 100 |
| dn22 | SELECT * FROM ORDER0401 t1 WHERE t1.SN = 'shanghai10006_10000' LIMIT 100 |
+-----------+--------------------------------------------------------------------------+
2 rows in set (0.00 sec) mysql>

mycat 连续分片 -&gt; 自己定义数字范围分片的更多相关文章

  1. mycat 连续分片 -&gt; 按日期(天)分片

    1,按日期(天)分片 按日期(天)分片:从開始日期算起,依照天数来分片 比如,从2016-01-01.每10天一个分片 注意事项:须要提前将分片规划好,建好.否则有可能日期超出实际配置分片数 2,加入 ...

  2. Mycat 分片规则详解--自然月分片

    实现方式:按照月份列分片,每个自然月一个分片 优点:使数据按照每月来进行分时存储 缺点:由于数据是连续的,所以该方案不能有效的利用资源 配置示例: <tableRule name="s ...

  3. Mycat 分片规则详解--应用指定分片

    实现方式:根据字符串的子串(必须是数字)计算分区号(由调用方传递参数,显示指定分区号),例如,id=05-12232323,其中 id 是从 startIndex = 0,size=2,即截取的子串是 ...

  4. Mycat 分片规则详解--固定 hash 分片

    实现方式:该算法类似于十进制的求模运算,但是为二进制的操作,例如,取 id 的二进制低 10 位 与 1111111111 进行 & 运算 优点:这种策略比较灵活,可以均匀分配也可以非均匀分配 ...

  5. 面试题解:输入一个数A,找到大于A的一个最小数B,且B中不存在连续相等的两个数字

    玄魂工作室秘书 [玄魂工作室]      昨天发的算法有一处情况没考虑到,比如加一后有进位,导致又出现重复数字的情况,修正后今天重新发一次.     比如输入99,那B应该是101 因为100有两个连 ...

  6. 输入一个正整数n,计算出[0,n]这些整数中的二进制数没有连续3个1的数字有多少

    输入一个正整数n,计算出[0,n]这些整数中的二进制数没有连续3个1的数字有多少? 例子:输入数字9,则输出结果位9.因为[0-9]中,只有数字7有连续的三个‘1’出现,别的都没有,所以一共有9个数字 ...

  7. mongodb分片介绍—— 基于范围(数值型)的分片 或者 基于哈希的分片

    数据分区 MongoDB中数据的分片是以集合为基本单位的,集合中的数据通过 片键 被分成多部分. 片键 对集合进行分片时,你需要选择一个 片键 , shard key 是每条记录都必须包含的,且建立了 ...

  8. Mongodb 笔记07 分片、配置分片、选择片键、分片管理

    分片 1. 分片(sharding)是指将数据拆分,将其分散存放在不同的机器上的过程.有时也用分区(partitioning)来表示这个概念.将数据分散到不同的机器上,不需要功能强大的大型计算机就可以 ...

  9. mongo 3.4分片集群系列之八:分片管理

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

随机推荐

  1. DecimalFormat格式化输出带小数的数字类型

    刚開始 double d = 333333333.333333333; System.out.println(d); 输出结果为3.333333333333333E8 网上找到了DecimalForm ...

  2. oc4--方法

    // main.m // 第一个OC类-方法2 #import <Foundation/Foundation.h> // 1.编写类的声明 @interface Iphone : NSOb ...

  3. Shell case esac语句【转】

    本文转载自:http://c.biancheng.net/cpp/view/7006.html C语言中文网推出辅导班啦,包括「C语言辅导班.C++辅导班.算法/数据结构辅导班」,全部都是一对一教学: ...

  4. poj--1664--放苹果(递归好体)

    放苹果 Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u Submit Status De ...

  5. hdoj--3072--Intelligence System(scc+缩点+数据去重)

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  6. SimpleMappingExceptionResolver异常映射

    转自:https://blog.csdn.net/qqqqqq654/article/details/65767701 SimpleMappingException异常映射 当异常发生时,我们可以将它 ...

  7. JS实时获取浏览器窗口尺寸 .

    给div实时设置宽度 <div id="detail" style="width: 100%; overflow: scroll;"> </d ...

  8. Goldengate升级之目标端(replicat端)升级

    转自红黑联盟Goldengate升级之目标端(replicat端升级 要升级replicat端的原因为:目标端OGG软件版本与源端OGG软件版本不同,在实际生产应用中,经常发现replicat端事务丢 ...

  9. CaptCha的现状与未来

    2011年的老文.................转自于伯乐在线:http://blog.jobbole.com/4655/       有一个机会,朋友推荐一个创业的哥们给我认识,方向就是验证码识别 ...

  10. 如何防范自己的IP泄漏

    在正式进行各种“黑客行为”之前,黑客会采取各种手段,探测(也可以说“侦察”)对方的主机信息,以便决定使用何种最有效的方法达到自己的目的.来看看黑客是如何获知最基本的网络信息——对方的IP地址:以及用户 ...