博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mycat 连续分片 -> 按日期(天)分片
阅读量:5944 次
发布时间:2019-06-19

本文共 8639 字,大约阅读时间需要 28 分钟。

1,按日期(天)分片

按日期(天)分片:从開始日期算起,依照天数来分片

比如,从2016-01-01。每10天一个分片
注意事项:须要提前将分片规划好,建好。否则有可能日期超出实际配置分片数


2,加入配置文件

在function.xml里面进行配置:

yyyy-MM-dd
2016-01-01
10
CREATE_TIME
sharding-by-date

在schema.xml里面进行配置:


3。建表而且录入数据

CREATE TABLE ORDER0402(ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,PROVINCE VARCHAR(16),SN VARCHAR(64),CREATE_TIME DATETIME);    mysql> CREATE TABLE ORDER0402(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 ORDER0402(ID,PROVINCE,SN,CREATE_TIME) VALUES(10001,'shanghai','shanghai10006_2016-01-03','2016-01-03');    INSERT INTO ORDER0402(ID,PROVINCE,SN,CREATE_TIME) VALUES(5000001,'shanghai','shanghai10006_2016-01-13','2016-01-13');    mysql> CREATE TABLE ORDER0402(ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,PROVINCE VARCHAR(16),SN VARCHAR(64),CREATE_TIME DATETIME);    Query OK, 0 rows affected (0.06 sec)    mysql> explain INSERT INTO ORDER0402(ID,PROVINCE,SN,CREATE_TIME) VALUES(10001,'shanghai','shanghai10006_2016-01-03','2016-01-03');    +-----------+--------------------------------------------------------------------------------------------------------------------+    | DATA_NODE | SQL                                                                                                                |    +-----------+--------------------------------------------------------------------------------------------------------------------+    | dn21      | INSERT INTO ORDER0402(ID,PROVINCE,SN,CREATE_TIME) VALUES(10001,'shanghai','shanghai10006_2016-01-03','2016-01-03') |    +-----------+--------------------------------------------------------------------------------------------------------------------+    1 row in set (0.11 sec)    mysql> INSERT INTO ORDER0402(ID,PROVINCE,SN,CREATE_TIME) VALUES(10001,'shanghai','shanghai10006_2016-01-03','2016-01-03');    Query OK, 1 row affected (0.00 sec)    mysql>     mysql> explain INSERT INTO ORDER0402(ID,PROVINCE,SN,CREATE_TIME) VALUES(5000001,'shanghai','shanghai10006_2016-01-13','2016-01-13');    +-----------+----------------------------------------------------------------------------------------------------------------------+    | DATA_NODE | SQL                                                                                                                  |    +-----------+----------------------------------------------------------------------------------------------------------------------+    | dn22      | INSERT INTO ORDER0402(ID,PROVINCE,SN,CREATE_TIME) VALUES(5000001,'shanghai','shanghai10006_2016-01-13','2016-01-13') |    +-----------+----------------------------------------------------------------------------------------------------------------------+    1 row in set (0.01 sec)    mysql> INSERT INTO ORDER0402(ID,PROVINCE,SN,CREATE_TIME) VALUES(5000001,'shanghai','shanghai10006_2016-01-13','2016-01-13');    Query OK, 1 row affected (0.01 sec)    mysql>

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

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

開始获取连接:    02/12 22:23:26.386  DEBUG [$_NIOREACTOR-2-RW] (ServerQueryHandler.java:56) -ServerConnection [id=1, schema=TESTDB, host=127.0.0.1, user=test,txIsolation=3, autocommit=true, schema=TESTDB]INSERT INTO ORDER0402(ID,PROVINCE,SN,CREATE_TIME) VALUES(5000001,'shanghai','shanghai10006_2016-01-13','2016-01-13')開始路由匹配:    02/12 22:23:26.388  DEBUG [$_NIOREACTOR-2-RW] (NonBlockingSession.java:113) -ServerConnection [id=1, schema=TESTDB, host=127.0.0.1, user=test,txIsolation=3, autocommit=true, schema=TESTDB]INSERT INTO ORDER0402(ID,PROVINCE,SN,CREATE_TIME) VALUES(5000001,'shanghai','shanghai10006_2016-01-13','2016-01-13'), route={       1 -> dn22{
INSERT INTO ORDER0402(ID,PROVINCE,SN,CREATE_TIME) VALUES(5000001,'shanghai','shanghai10006_2016-01-13','2016-01-13')}匹配好路由后,直接往相应的dn22节点開始运行insert数据录入操作: 02/12 22:23:26.403 DEBUG [$_NIOREACTOR-1-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=17, lastTime=1455287006372, user=root, schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false, threadId=90, charset=latin1, txIsolation=3, autocommit=true, attachment=dn22{
INSERT INTO ORDER0402(ID,PROVINCE,SN,CREATE_TIME) VALUES(5000001,'shanghai','shanghai10006_2016-01-13','2016-01-13')}, respHandler=SingleNodeHandler [node=dn22{
INSERT INTO ORDER0402(ID,PROVINCE,SN,CREATE_TIME) VALUES(5000001,'shanghai','shanghai10006_2016-01-13','2016-01-13')}, packetId=0], host=13.23.1.11, port=3327, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]操作完毕,開始释放连接: 02/12 22:23:26.404 DEBUG [$_NIOREACTOR-1-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=17, lastTime=1455287006372, user=root, schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false, threadId=90, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=13.23.1.11, port=3327, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]

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

mycat命令台显示结果:

mysql>  explain select * from ORDER0402 t1 where t1.CREATE_TIME='2016-01-03';    +-----------+--------------------------------------------------------------------------+    | DATA_NODE | SQL                                                                      |    +-----------+--------------------------------------------------------------------------+    | dn21      | SELECT * FROM ORDER0402 t1 WHERE t1.CREATE_TIME = '2016-01-03' LIMIT 100 |    +-----------+--------------------------------------------------------------------------+    1 row in set (0.04 sec)    mysql> select * from ORDER0402 t1 where t1.CREATE_TIME='2016-01-03';    +-------+----------+--------------------------+---------------------+    | ID    | PROVINCE | SN                       | CREATE_TIME         |    +-------+----------+--------------------------+---------------------+    | 10001 | shanghai | shanghai10006_2016-01-03 | 2016-01-03 00:00:00 |    +-------+----------+--------------------------+---------------------+    1 row in set (0.00 sec)    mysql>

查看后台mycat日志信息:

02/12 22:44:13.773  DEBUG [$_NIOREACTOR-2-RW] (ServerQueryHandler.java:56) -ServerConnection [id=1, schema=TESTDB, host=127.0.0.1, user=test,txIsolation=3, autocommit=true, schema=TESTDB]explain select * from ORDER0402 t1 where t1.CREATE_TIME='2016-01-03'    02/12 22:44:13.774  DEBUG [$_NIOREACTOR-2-RW] (EnchachePool.java:76) -SQLRouteCache  miss cache ,key:TESTDB select * from ORDER0402 t1 where t1.CREATE_TIME='2016-01-03'    02/12 22:44:13.812  DEBUG [$_NIOREACTOR-2-RW] (EnchachePool.java:59) -SQLRouteCache add cache ,key:TESTDB select * from ORDER0402 t1 where t1.CREATE_TIME='2016-01-03' value:select * from ORDER0402 t1 where t1.CREATE_TIME='2016-01-03', route={       1 -> dn21{
SELECT * FROM ORDER0402 t1 WHERE t1.CREATE_TIME = '2016-01-03' LIMIT 100} }

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

mysql> explain select * from ORDER0402 t1 where t1.CREATE_TIME<'2016-01-04';    +-----------+--------------------------------------------------------------------------+    | DATA_NODE | SQL                                                                      |    +-----------+--------------------------------------------------------------------------+    | dn21      | SELECT * FROM ORDER0402 t1 WHERE t1.CREATE_TIME < '2016-01-04' LIMIT 100 |    | dn22      | SELECT * FROM ORDER0402 t1 WHERE t1.CREATE_TIME < '2016-01-04' LIMIT 100 |    +-----------+--------------------------------------------------------------------------+    2 rows in set (0.01 sec)    mysql>

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

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

转载地址:http://lcwxx.baihongyu.com/

你可能感兴趣的文章
SpringBoot 搭建
查看>>
中信国健临床通讯2011年7月期目录
查看>>
iOS开发之设计模式篇
查看>>
电信运营商 IT 系统介绍
查看>>
简单粗暴的“Debug模式”
查看>>
汇编实验一
查看>>
Python 面试总结
查看>>
ansible register when: result | succeeded when: item.rc != 0
查看>>
有赞MySQL自动化运维之路—ZanDB
查看>>
ssm中shiro的使用
查看>>
10个操作数的随机四则运算(二)
查看>>
django中聚合aggregate和annotate GROUP BY的使用方法
查看>>
TFS简介
查看>>
docker管理平台 shipyard安装
查看>>
安装django
查看>>
Bootstrap3 栅格系统-简介
查看>>
ADODB类库操作查询数据表
查看>>
第五章(5)Libgdx应用框架之线程
查看>>
【java】File的使用:将字符串写出到本地文件,大小0kb的原因
查看>>
安卓音乐播放器开发实例
查看>>