转载请注明出处:http://blog.csdn.net/l1028386804/article/details/77096144
一、简单描述
今天,我们就一起来看下Mycat中的枚举分片,所谓的枚举分片,就是通过在配置文件中配置可能的枚举id,自己配置分片,本规则适用于特定的场景,比如有些业务需要按照省份或区县来做保存,而全国省份区县固定的,具体使用场景可根据自己具体的业务场景确定。
二、实现枚举分片
1、配置rule.xml
在rule.xml添加如下配置:
<!--
mapFile标识配置文件名称
type默认值为0(0表示Integer,非零表示String)
默认节点的作用:枚举分片时,如果碰到不识别的枚举值,就让它路由到默认节点
-->
<function name="hash-int-customer" class="org.opencloudb.route.function.PartitionByFileMap">
<property name="mapFile">partition-hash-int.txt</property>
<property name="defaultNode">0</property>
<property name="type">0</property>
</function>
<tableRule name="tr-hash-int-customer">
<rule>
<columns>province</columns>
<algorithm>hash-int-customer</algorithm>
</rule>
</tableRule>
2、创建partition-hash-int.txt文件
内容如下:
10000=0
10010=1
3、配置schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/" >
<schema name="lyzdb" checkSQLschema="false" sqlMaxLimit="100">
<!-- global table is auto cloned to all defined data nodes ,so can join
with any table whose sharding node is in the same data node -->
<table name="t_order" primaryKey="id" dataNode="dn1,dn2" rule="tr-hash-int-customer"/>
</schema>
<!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"
/> -->
<dataNode name="dn1" dataHost="localhost1" database="db1" />
<dataNode name="dn2" dataHost="localhost1" database="db2" />
<!--<dataNode name="dn4" dataHost="sequoiadb1" database="SAMPLE" />
<dataNode name="jdbc_dn1" dataHost="jdbchost" database="db1" />
<dataNode name="jdbc_dn2" dataHost="jdbchost" database="db2" />
<dataNode name="jdbc_dn3" dataHost="jdbchost" database="db3" /> -->
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="192.168.209.137:3306" user="root" password="root"></writeHost>
<!--<writeHost host="hostS1" url="localhost:3316" user="root"-->
<!--password="123456" />-->
<!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
</dataHost>
</mycat:schema>
至此,Mycat的配置工作就完成了,下面我们就一起来测试下Mycat的枚举分片功能。
三、测试
1、建表并且录入数据
mysql> explain CREATE TABLE t_order (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,PROVINCE INT,SN VARCHAR(64),CREATE_TIME DATETIME);
+-----------+--------------------------------------------------------------------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+--------------------------------------------------------------------------------------------------------------------+
| dn1 | CREATE TABLE t_order (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,PROVINCE INT,SN VARCHAR(64),CREATE_TIME DATETIME) |
| dn2 | CREATE TABLE t_order (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,PROVINCE INT,SN VARCHAR(64),CREATE_TIME DATETIME) |
+-----------+--------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.09 sec)
mysql>
mysql> CREATE TABLE t_order (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,PROVINCE INT,SN VARCHAR(64),CREATE_TIME DATETIME);
Query OK, 0 rows affected (0.19 sec)
mysql> explain INSERT INTO t_order (ID,PROVINCE,SN,CREATE_TIME) VALUES(1,10000,'beijing_10001_10000',NOW());
+-----------+----------------------------------------------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+----------------------------------------------------------------------------------------------+
| dn1 | INSERT INTO t_order (ID,PROVINCE,SN,CREATE_TIME) VALUES(1,10000,'beijing_10001_10000',NOW()) |
+-----------+----------------------------------------------------------------------------------------------+
1 row in set (0.07 sec)
mysql> INSERT INTO t_order (ID,PROVINCE,SN,CREATE_TIME) VALUES(1,10000,'beijing_10001_10000',NOW());
Query OK, 1 row affected (0.11 sec)
mysql> explain INSERT INTO t_order (ID,PROVINCE,SN,CREATE_TIME) VALUES(2,10010,'tianjin_10010',NOW());
+-----------+----------------------------------------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+----------------------------------------------------------------------------------------+
| dn2 | INSERT INTO t_order (ID,PROVINCE,SN,CREATE_TIME) VALUES(2,10010,'tianjin_10010',NOW()) |
+-----------+----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> INSERT INTO t_order (ID,PROVINCE,SN,CREATE_TIME) VALUES(2,10010,'tianjin_10010',NOW());
Query OK, 1 row affected (0.04 sec)
Mycat日志如下:
NCREMENT PRIMARY KEY,PROVINCE INT,SN VARCHAR(64),CREATE_TIME DATETIME), route={
1 -> dn1{CREATE TABLE t_order (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,PROVINCE INT,SN VARCHAR(64),CREATE_TIME DATETIME)}
2 -> dn2{CREATE TABLE t_order (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,PROVINCE INT,SN VARCHAR(64),CREATE_TIME DATETIME)}
} rrs
08/10 13:38:09.330 DEBUG [$_NIOREACTOR-0-RW] (NonBlockingSession.java:113) -ServerConnection [id=1, schema=lyzdb, host=192.168.209.1, user=lyz,txIsolation=3, autocommit=true, schema=lyzdb]INSERT INTO t_order (ID,PROVINCE,SN,CREATE_TIME) VALUES(1,10000,'beijing_10001_10000',NOW()), route={
1 -> dn1{INSERT INTO t_order (ID,PROVINCE,SN,CREATE_TIME) VALUES(1,10000,'beijing_10001_10000',NOW())}
} rrs
08/10 13:39:45.878 DEBUG [$_NIOREACTOR-0-RW] (NonBlockingSession.java:113) -ServerConnection [id=1, schema=lyzdb, host=192.168.209.1, user=lyz,txIsolation=3, autocommit=true, schema=lyzdb]INSERT INTO t_order (ID,PROVINCE,SN,CREATE_TIME) VALUES(2,10010,'tianjin_10010',NOW()), route={
1 -> dn2{INSERT INTO t_order (ID,PROVINCE,SN,CREATE_TIME) VALUES(2,10010,'tianjin_10010',NOW())}
} rrs
由此可见,创建表的时候,Mycat是将SQL路由到所有的数据节点;录入数据的时候,是根据分片字段将数据路由到指定的数据分片节点;
2、按照分片字段查询
mysql> explain select * from t_order where PROVINCE=10000;
+-----------+--------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+--------------------------------------------------------+
| dn1 | SELECT * FROM t_order WHERE PROVINCE = 10000 LIMIT 100 |
+-----------+--------------------------------------------------------+
1 row in set (0.40 sec)
Mycat日志如下:
08/10 13:42:02.586 DEBUG [$_NIOREACTOR-0-RW] (EnchachePool.java:76) -SQLRouteCache miss cache ,key:lyzdb select * from t_order where PROVINCE=10000
08/10 13:42:02.987 DEBUG [$_NIOREACTOR-0-RW] (EnchachePool.java:59) -SQLRouteCache add cache ,key:lyzdb select * from t_order where PROVINCE=10000 value:select * from t_order where PROVINCE=10000, route={
1 -> dn1{SELECT *
FROM t_order
WHERE PROVINCE = 10000
LIMIT 100}
}
由此可见,执行简单的查询,如果指定分片字段ID字段,则走分片查询单个分片节点
3、按照分片字段范围查询
mysql> explain select * from t_order where PROVINCE >= 10000;
+-----------+---------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+---------------------------------------------------------+
| dn1 | SELECT * FROM t_order WHERE PROVINCE >= 10000 LIMIT 100 |
| dn2 | SELECT * FROM t_order WHERE PROVINCE >= 10000 LIMIT 100 |
+-----------+---------------------------------------------------------+
2 rows in set (0.00 sec)
Mycat日志如下:
08/10 13:43:07.238 DEBUG [$_NIOREACTOR-0-RW] (EnchachePool.java:59) -SQLRouteCache add cache ,key:lyzdb select * from t_order where PROVINCE >= 10000 value:select * from t_order where PROVINCE >= 10000, route={
1 -> dn1{SELECT *
FROM t_order
WHERE PROVINCE >= 10000
LIMIT 100}
2 -> dn2{SELECT *
FROM t_order
WHERE PROVINCE >= 10000
LIMIT 100}
}
由此可见,如果分片字段范围的查询,则走所有节点去检索,哪怕只有一条数据在一个分片上,route路由也是走所有的分片进行检索查询
4、按照非分片字段查询
mysql> explain select * from t_order where sn = 'tianjin_10010';
+-----------+------------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+------------------------------------------------------------+
| dn1 | SELECT * FROM t_order WHERE sn = 'tianjin_10010' LIMIT 100 |
| dn2 | SELECT * FROM t_order WHERE sn = 'tianjin_10010' LIMIT 100 |
+-----------+------------------------------------------------------------+
2 rows in set (0.00 sec)
Mycat日志如下:
08/10 13:45:01.416 DEBUG [$_NIOREACTOR-0-RW] (EnchachePool.java:59) -SQLRouteCache add cache ,key:lyzdb select * from t_order where sn = 'tianjin_10010' value:select * from t_order where sn = 'tianjin_10010', route={
1 -> dn1{SELECT *
FROM t_order
WHERE sn = 'tianjin_10010'
LIMIT 100}
2 -> dn2{SELECT *
FROM t_order
WHERE sn = 'tianjin_10010'
LIMIT 100}
}
由此可见,如果不走分片字段的查询,即使是单个数据,也要route路由所有的分片,走所有的分片进行查询