Mycat catlet跨庫JOIN與全域性JOIN Mycat catlet跨庫JOIN與全域性JOIN
阿新 • • 發佈:2019-01-09
Mycat catlet跨庫JOIN與全域性JOIN
2017年06月14日 10:38:00 閱讀數:3067基於catlet的分庫JOIN為資料庫表按型別分庫提供了很好的支援,而全域性表在每個節點都有DDL實現也可以實現直接JOIN操作。當表設定為全域性表後可以與任意一個表進行JOIN操作。
Mysql分庫
這裡我們將基礎和業務分別放在不同的資料庫分片上,建立m和n資料庫例項名。
m基礎配置資料庫:包含t_user
n業務資料資料庫:包含t_service
m&n資料庫建表語句:
t_user
- /*
- Navicat MySQL Data Transfer
- Source Server : 10.10.13.251-dev
- Source Server Version : 50719
- Source Host : 10.10.13.251:3306
- Source Database : m
- Target Server Type : MYSQL
- Target Server Version : 50719
- File Encoding : 65001
- Date: 2017-06-14 13:08:49
- */
- SET FOREIGN_KEY_CHECKS=0;
- -- ----------------------------
- -- Table structure for `t_user`
- -- ----------------------------
- DROP TABLE IF EXISTS `t_user`;
- CREATE TABLE `t_user` (
- `u_id` int(11) NOT NULL AUTO_INCREMENT,
- `u_name` varchar(50) DEFAULT NULL,
- `u_email` varchar(100) DEFAULT NULL,
- PRIMARY KEY (`u_id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
- /*
- Navicat MySQL Data Transfer
- Source Server : 10.10.13.251-dev
- Source Server Version : 50719
- Source Host : 10.10.13.251:3306
- Source Database : n
- Target Server Type : MYSQL
- Target Server Version : 50719
- File Encoding : 65001
- Date: 2017-06-14 13:08:59
- */
- SET FOREIGN_KEY_CHECKS=0;
- -- ----------------------------
- -- Table structure for `t_service`
- -- ----------------------------
- DROP TABLE IF EXISTS `t_service`;
- CREATE TABLE `t_service` (
- `s_id` int(11) NOT NULL AUTO_INCREMENT,
- `s_name` varchar(50) DEFAULT NULL,
- `s_uid` int(11) DEFAULT NULL,
- PRIMARY KEY (`s_id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
rule.xml配置:
[html] view plain copy- <tableRule name="mod-long">
- <rule>
- <columns>id</columns>
- <algorithm>mod-long</algorithm>
- </rule>
- </tableRule>
- <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
- <!-- how many data nodes -->
- <property name="count">1</property>
- </function>
Mycat 跨庫JOIN
只需要修改${MYCAT_HOME}/conf/schema.xml [html] view plain copy- <?xml version="1.0"?>
- <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
- <mycat:schema xmlns:mycat="http://io.mycat/">
- <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
- <!-- auto sharding by id (long) -->
- <table name="t_user" primaryKey="u_id" autoIncrement="true" dataNode="dn1" rule="mod-long" >
- <!-- <childTable name="t_service" primaryKey="s_id" joinKey="s_uid" parentKey="u_id"/> -->
- </table>
- <table name="t_service" primaryKey="s_id" autoIncrement="true" dataNode="dn2" rule="mod-long" />
- <!-- random sharding using mod sharind rule -->
- <!-- <table name="dual" primaryKey="ID" dataNode="dnx,dnoracle2" type="global"
- needAddLimit="false"/> <table name="worker" primaryKey="ID" dataNode="jdbc_dn1,jdbc_dn2,jdbc_dn3"
- rule="mod-long" /> -->
- </schema>
- <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"
- /> -->
- <dataNode name="dn1" dataHost="localhost1" database="m" />
- <dataNode name="dn2" dataHost="localhost1" database="n" />
- <!--<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.178.128:3306" user="root" password="123456">
- <!-- can have multi read hosts -->
- <readHost host="hostS2" url="192.168.178.128:3306" user="root" password="123456" />
- </writeHost>
- <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
- </dataHost>
- </mycat:schema>
利用catlets人工智慧解析工具JOIN:
- /*!mycat:catlet=io.mycat.catlets.ShareJoin */SELECT * from t_service s,t_user u ON u.u_id=s.s_uid;
Mycat 全域性JOIN
當表設定為全域性表後可以與任意一個表進行JOIN操作。
分類全域性表
一個真實的業務系統中,往往存在大量的類似字典表的表格,它們與業務表之間可能有關係,這種關係,可以理解為“標籤”,而不應理解為通常的“主從關係”,這些表基本上很少變動,可以根據主鍵ID進行快取,下面這張圖說明了一個典型的“標籤關係”圖:
在分片的情況下,當業務表因為規模而進行分片以後,業務表與這些附屬的字典表之間的關聯,就成了比較棘手的問題,考慮到字典表具有以下幾個特性:
- 變動不頻繁
- 資料量總體變化不大
- 資料規模不大,很少有超過數十萬條記錄。
鑑於此,MyCAT定義了一種特殊的表,稱之為“全域性表”,全域性表具有以下特性:
- 全域性表的插入、更新操作會實時在所有節點上執行,保持各個分片的資料一致性
- 全域性表的查詢操作,只從一個節點獲取
- 全域性表可以跟任何一個表進行JOIN操作
將字典表或者符合字典表特性的一些表定義為全域性表,則從另外一個方面,很好的解決了資料JOIN的難題。通過全域性表+基於E-R關係的分片策略,MyCAT可以滿足80%以上的企業應用開發。
全域性表配置
全域性表配置比較簡單,不用寫 Rule 規則,如下配置即可:
[plain] view plain copy
- <table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />
需要注意的是,全域性表每個分片節點上都要有執行建立表的 DDL 語句。
人工智慧JOIN測試資料
非limit查詢
[plain]
view plain
copy
- [email protected]:~$ mysql -h192.168.178.128 -uroot -p123456 -P8666
- Warning: Using a password on the command line interface can be insecure.
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 5
- Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)
- Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- mysql> /*!mycat:catlet=demo.catlets.ShareJoin */SELECT * from t_service s,t_user u ON u.u_id=s.s_uid;
- ERROR 1064 (HY000): java.lang.ClassNotFoundException: demo.catlets.ShareJoin
- mysql> /*!mycat:catlet=io.mycat.catlets.ShareJoin */SELECT * from t_service s,t_user u ON u.u_id=s.s_uid;
- Empty set (0.15 sec)
- mysql> show databases;
- +----------+
- | DATABASE |
- +----------+
- | TESTDB |
- +----------+
- 1 row in set (0.00 sec)
- mysql> /*!mycat:catlet=io.mycat.catlets.ShareJoin */SELECT * from t_service s,t_user u ON u.u_id=s.s_uid;
- +------+--------------+-------+-------+--------+----------------+
- | s_id | s_name | s_uid | s_uid | u_name | u_email |
- +------+--------------+-------+-------+--------+----------------+
- | 1 | MYCATSERVICE | 1 | 1 | boonya | [email protected] |
- +------+--------------+-------+-------+--------+----------------+
- 1 row in set (0.02 sec)
- mysql> /*!mycat:catlet=io.mycat.catlets.ShareJoin */SELECT * from t_service s,t_user u ON u.u_id=s.s_uid;
- ERROR 2006 (HY000): MySQL server has gone away
- No connection. Trying to reconnect...
- Connection id: 15
- Current database: *** NONE ***
- +------+--------------+-------+-------+--------+-----------------+
- | s_id | s_name | s_uid | s_uid | u_name | u_email |
- +------+--------------+-------+-------+--------+-----------------+
- | 1 | MYCATSERVICE | 1 | 1 | boonya | [email protected] |
- | 2 | SHOPPING | 2 | 2 | niuniu | [email protected] |
- +------+--------------+-------+-------+--------+-----------------+
- 2 rows in set (0.02 sec)
- mysql>
JOIN查詢有效。
limit查詢
[plain] view plain copy- +------+--------------+-------+-------+--------+-----------------+
- | s_id | s_name | s_uid | s_uid | u_name | u_email |
- +------+--------------+-------+-------+--------+-----------------+
- | 1 | MYCATSERVICE | 1 | 1 | boonya | [email protected] |
- | 2 | SHOPPING | 2 | 2 | niuniu | [email protected] |
- +------+--------------+-------+-------+--------+-----------------+
- 2 rows in set (0.37 sec)
- mysql> /*!mycat:catlet=io.mycat.catlets.ShareJoin */SELECT * from t_service s,t_user u ON u.u_id=s.s_uid limit 2;
- +------+--------------+-------+-------+--------+-----------------+
- | s_id | s_name | s_uid | s_uid | u_name | u_email |
- +------+--------------+-------+-------+--------+-----------------+
- | 1 | MYCATSERVICE | 1 | 1 | boonya | [email protected] |
- | 2 | SHOPPING | 2 | 2 | niuniu | [email protected] |
- +------+--------------+-------+-------+--------+-----------------+
- 2 rows in set (0.06 sec)
- mysql> /*!mycat:catlet=io.mycat.catlets.ShareJoin */SELECT * from t_service s,t_user u ON u.u_id=s.s_uid limit 1;
- +------+--------------+-------+-------+--------+----------------+
- | s_id | s_name | s_uid | s_uid | u_name | u_email |
- +------+--------------+-------+-------+--------+----------------+
- | 1 | MYCATSERVICE | 1 | 1 | boonya | [email protected] |
- +------+--------------+-------+-------+--------+----------------+
- 1 row in set (0.02 sec)
最終看得到資料表對應關係如下: