mycat 離散分片 -> 程序指定分區的分片
阿新 • • 發佈:2017-07-15
spa ssi ica arc direct ted def cut roo
1。程序指定分區的分片
此規則是在運行階段有應用自主決定路由到那個分片。
此方法為直接依據字符子串(必須是數字)計算分區號(由應用傳遞參數。顯式指定分區號)。
2,加入配置文件
在function.xml裏面進行配置:
<function name="sharding-by-substring-040302" class="org.opencloudb.route.function.PartitionDirectBySubString">
<property name="startIndex" >0</property>
<property name="size">1</property>
<property name="partitionCount">8</property>
<property name="defaultPartition">0</property>
</function>
比如id=0-100000020在此配置中代表依據id中從startIndex=0,開始,截取siz=1位數字即0,0就是獲取的分區,假設沒傳默認分配到defaultPartition
<tableRule name="tr-sharding-substr-mc040302">
<rule>
<columns>CODE</columns>
<algorithm>sharding-by-substring-040302</algorithm>
</rule>
</tableRule>
在schema.xml裏面進行配置:
<table name="ORDER040302" primaryKey="ID" dataNode="dn21,dn22" rule="tr-sharding-substr-mc040302"/>
3,建表而且錄入數據
CREATE TABLE ORDER040302(ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,CODE VARCHAR(64),SN VARCHAR(64),CREATE_TIME DATETIME);
mysql> CREATE TABLE ORDER040302(ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,CODE VARCHAR(64),SN VARCHAR(64),CREATE_TIME DATETIME);
Query OK, 0 rows affected (0.05 sec)
mysql>
INSERT INTO ORDER040302(ID,CODE,SN,CREATE_TIME) VALUES(1,‘0-10000020‘,‘beijing_03-10000020‘,NOW());
INSERT INTO ORDER040302(ID,CODE,SN,CREATE_TIME) VALUES(2,‘1-10000020‘,‘shanghai_10-10000020‘,NOW());
mysql> explain INSERT INTO ORDER040302(ID,CODE,SN,CREATE_TIME) VALUES(1,‘0-10000020‘,‘beijing_03-10000020‘,NOW());
+-----------+----------------------------------------------------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+----------------------------------------------------------------------------------------------------+
| dn21 | INSERT INTO ORDER040302(ID,CODE,SN,CREATE_TIME) VALUES(1,‘0-10000020‘,‘beijing_03-10000020‘,NOW()) |
+-----------+----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
mysql> INSERT INTO ORDER040302(ID,CODE,SN,CREATE_TIME) VALUES(1,‘0-10000020‘,‘beijing_03-10000020‘,NOW());
Query OK, 1 row affected (0.01 sec)
mysql>
mysql> explain INSERT INTO ORDER040302(ID,CODE,SN,CREATE_TIME) VALUES(2,‘1-10000020‘,‘shanghai_10-10000020‘,NOW());
+-----------+-----------------------------------------------------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+-----------------------------------------------------------------------------------------------------+
| dn22 | INSERT INTO ORDER040302(ID,CODE,SN,CREATE_TIME) VALUES(2,‘1-10000020‘,‘shanghai_10-10000020‘,NOW()) |
+-----------+-----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> INSERT INTO ORDER040302(ID,CODE,SN,CREATE_TIME) VALUES(2,‘1-10000020‘,‘shanghai_10-10000020‘,NOW());
Query OK, 1 row affected (0.00 sec)
mysql>
4,對insert操作流程進行分析,參考mycat.log對路由過程做完整的分析
查看後臺mycat.log分析例如以下:
開始獲取連接:
02/13 00:40:18.173 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 ORDER040302(ID,CODE,SN,CREATE_TIME) VALUES(2,‘1-10000020‘,‘shanghai_10-10000020‘,NOW())
開始路由匹配:
02/13 00:40:18.175 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 ORDER040302(ID,CODE,SN,CREATE_TIME) VALUES(2,‘1-10000020‘,‘shanghai_10-10000020‘,NOW()), route={
1 -> dn22{INSERT INTO ORDER040302(ID,CODE,SN,CREATE_TIME) VALUES(2,‘1-10000020‘,‘shanghai_10-10000020‘,NOW())}
匹配好路由後,直接往相應的dn21節點開始運行insert數據錄入操作:
02/13 00:40:18.180 DEBUG [$_NIOREACTOR-1-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=17, lastTime=1455295218162, user=root, schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false, threadId=199, charset=latin1, txIsolation=3, autocommit=true, attachment=dn22{INSERT INTO ORDER040302(ID,CODE,SN,CREATE_TIME) VALUES(2,‘1-10000020‘,‘shanghai_10-10000020‘,NOW())}, respHandler=SingleNodeHandler [node=dn22{INSERT INTO ORDER040302(ID,CODE,SN,CREATE_TIME) VALUES(2,‘1-10000020‘,‘shanghai_10-10000020‘,NOW())}, packetId=0], host=10.254.1.11, port=3327, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]
連接釋放:
02/13 00:40:18.180 DEBUG [$_NIOREACTOR-1-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=17, lastTime=1455295218162, user=root, schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false, threadId=199, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=10.254.1.11, port=3327, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
02/13 00:40:24.390 DEBUG [Timer1] (SQLJob.java:85) -con query s
5,運行簡單的查詢,假設指定分片字段ID字段,則走分片查詢單個分片節點
mycat命令臺顯示結果:
mysql> explain select * from ORDER040302 t1 where CODE= ‘0-10000020‘;
+-----------+------------------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+------------------------------------------------------------------+
| dn21 | SELECT * FROM ORDER040302 t1 WHERE CODE = ‘0-10000020‘ LIMIT 100 |
+-----------+------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from ORDER040302 t1 where CODE= ‘0-10000020‘;
+----+------------+---------------------+---------------------+
| ID | CODE | SN | CREATE_TIME |
+----+------------+---------------------+---------------------+
| 1 | 0-10000020 | beijing_03-10000020 | 2016-02-13 00:40:27 |
+----+------------+---------------------+---------------------+
1 row in set (0.01 sec)
mysql>
查看後臺mycat日誌信息:
02/13 00:44:46.717 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]explain select * from ORDER040302 t1 where CODE= ‘0-10000020‘
02/13 00:44:46.718 DEBUG [$_NIOREACTOR-1-RW] (EnchachePool.java:76) -SQLRouteCache miss cache ,key:TESTDB select * from ORDER040302 t1 where CODE= ‘0-10000020‘
02/13 00:44:46.719 DEBUG [$_NIOREACTOR-1-RW] (EnchachePool.java:59) -SQLRouteCache add cache ,key:TESTDB select * from ORDER040302 t1 where CODE= ‘0-10000020‘ value:select * from ORDER040302 t1 where CODE= ‘0-10000020‘, route={
1 -> dn21{SELECT *
FROM ORDER040302 t1
WHERE CODE = ‘0-10000020‘
LIMIT 100}
}
6。假設分片字段範圍的查詢,則走全部節點去檢索。哪怕僅僅有一條數據在一個分片上。route路由也是走全部的分片進行檢索查詢
mysql> explain select * from ORDER040302 t1 where CODE likt ‘0-1%‘;
+-----------+---------------------------------------------------+
| DATA_NODE | SQL |
+-----------+---------------------------------------------------+
| dn21 | SELECT * FROM ORDER040302 t1 WHERE CODE LIMIT 100 |
| dn22 | SELECT * FROM ORDER040302 t1 WHERE CODE LIMIT 100 |
+-----------+---------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
7,假設不走分片字段的查詢。即使是單個數據。也要route路由全部的分片,走全部的分片進行查詢
mysql> explain select * from ORDER040302 t1 where SN=‘beijing_03-10000020‘;
+-----------+-------------------------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+-------------------------------------------------------------------------+
| dn21 | SELECT * FROM ORDER040302 t1 WHERE SN = ‘beijing_03-10000020‘ LIMIT 100 |
| dn22 | SELECT * FROM ORDER040302 t1 WHERE SN = ‘beijing_03-10000020‘ LIMIT 100 |
+-----------+-------------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
mycat 離散分片 -> 程序指定分區的分片