Mycat catlet跨库JOIN与全局JOIN Mycat catlet跨库JOIN与全局JOIN

Mycat catlet跨库JOIN与全局JOIN

基于catlet的分库JOIN为数据库表按类型分库提供了很好的支持,而全局表在每个节点都有DDL实现也可以实现直接JOIN操作。当表设置为全局表后可以与任意一个表进行JOIN操作。

Mysql分库

这里我们将基础和业务分别放在不同的数据库分片上,创建m和n数据库实例名。

m基础配置数据库:包含t_user

n业务数据数据库:包含t_service

m&n数据库建表语句:

t_user

  1. /*  
  2. Navicat MySQL Data Transfer  
  3.   
  4. Source Server         : 10.10.13.251-dev  
  5. Source Server Version : 50719  
  6. Source Host           : 10.10.13.251:3306  
  7. Source Database       : m  
  8.   
  9. Target Server Type    : MYSQL  
  10. Target Server Version : 50719  
  11. File Encoding         : 65001  
  12.   
  13. Date: 2017-06-14 13:08:49  
  14. */  
  15.   
  16. SET FOREIGN_KEY_CHECKS=0;  
  17.   
  18. -- ----------------------------  
  19. -- Table structure for `t_user`  
  20. -- ----------------------------  
  21. DROP TABLE IF EXISTS `t_user`;  
  22. CREATE TABLE `t_user` (  
  23.   `u_id` int(11) NOT NULL AUTO_INCREMENT,  
  24.   `u_name` varchar(50) DEFAULT NULL,  
  25.   `u_email` varchar(100) DEFAULT NULL,  
  26.   PRIMARY KEY (`u_id`)  
  27. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;  
t_service:
  1. /*  
  2. Navicat MySQL Data Transfer  
  3.   
  4. Source Server         : 10.10.13.251-dev  
  5. Source Server Version : 50719  
  6. Source Host           : 10.10.13.251:3306  
  7. Source Database       : n  
  8.   
  9. Target Server Type    : MYSQL  
  10. Target Server Version : 50719  
  11. File Encoding         : 65001  
  12.   
  13. Date: 2017-06-14 13:08:59  
  14. */  
  15.   
  16. SET FOREIGN_KEY_CHECKS=0;  
  17.   
  18. -- ----------------------------  
  19. -- Table structure for `t_service`  
  20. -- ----------------------------  
  21. DROP TABLE IF EXISTS `t_service`;  
  22. CREATE TABLE `t_service` (  
  23.   `s_id` int(11) NOT NULL AUTO_INCREMENT,  
  24.   `s_name` varchar(50) DEFAULT NULL,  
  25.   `s_uid` int(11) DEFAULT NULL,  
  26.   PRIMARY KEY (`s_id`)  
  27. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;  

rule.xml配置:

[html] view plain copy
  1. <tableRule name="mod-long">  
  2.         <rule>  
  3.             <columns>id</columns>  
  4.             <algorithm>mod-long</algorithm>  
  5.         </rule>  
  6.     </tableRule>  
[html] view plain copy
  1. <function name="mod-long" class="io.mycat.route.function.PartitionByMod">  
  2.    <!-- how many data nodes -->  
  3.    <property name="count">1</property>  
  4. </function>  

Mycat 跨库JOIN

只需要修改${MYCAT_HOME}/conf/schema.xml
[html] view plain copy
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE mycat:schema SYSTEM "schema.dtd">  
  3. <mycat:schema xmlns:mycat="http://io.mycat/">  
  4.   
  5.         <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">  
  6.                 <!-- auto sharding by id (long) -->  
  7.                 <table name="t_user"   primaryKey="u_id" autoIncrement="true"   dataNode="dn1" rule="mod-long" >  
  8.                    <!--  <childTable name="t_service" primaryKey="s_id" joinKey="s_uid" parentKey="u_id"/> -->  
  9.                 </table>  
  10.   
  11.                 <table name="t_service" primaryKey="s_id" autoIncrement="true"   dataNode="dn2"  rule="mod-long" />  
  12.                 <!-- random sharding using mod sharind rule -->  
  13.   
  14.                 <!-- <table name="dual" primaryKey="ID" dataNode="dnx,dnoracle2" type="global"  
  15.                         needAddLimit="false"/> <table name="worker" primaryKey="ID" dataNode="jdbc_dn1,jdbc_dn2,jdbc_dn3"  
  16.                         rule="mod-long" /> -->  
  17.                  
  18.         </schema>   
  19.         <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"  
  20.                 /> -->  
  21.         <dataNode name="dn1" dataHost="localhost1" database="m" />  
  22.         <dataNode name="dn2" dataHost="localhost1" database="n" />  
  23.           
  24.         <!--<dataNode name="dn4" dataHost="sequoiadb1" database="SAMPLE" />  
  25.          <dataNode name="jdbc_dn1" dataHost="jdbchost" database="db1" />  
  26.         <dataNode       name="jdbc_dn2" dataHost="jdbchost" database="db2" />  
  27.         <dataNode name="jdbc_dn3"       dataHost="jdbchost" database="db3" /> -->  
  28.         <dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"  
  29.                           writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">  
  30.                 <heartbeat>select user()</heartbeat>  
  31.                 <!-- can have multi write hosts -->  
  32.                 <writeHost host="hostM1" url="192.168.178.128:3306" user="root"   password="123456">  
  33.                         <!-- can have multi read hosts -->  
  34.                         <readHost host="hostS2" url="192.168.178.128:3306" user="root" password="123456" />  
  35.                 </writeHost>  
  36.                 <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->  
  37.         </dataHost>  
  38.          
  39. </mycat:schema>  
  40.                                                          

利用catlets人工智能解析工具JOIN:

[plain] view plain copy
  1. /*!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
  1. <table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />  

需要注意的是,全局表每个分片节点上都要有运行创建表的 DDL 语句。


人工智能JOIN测试数据

非limit查询

[plain] view plain copy
  1. boonya@ubuntu:~$ mysql -h192.168.178.128  -uroot -p123456  -P8666  
  2. Warning: Using a password on the command line interface can be insecure.  
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  4. Your MySQL connection id is 5  
  5. Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)  
  6.   
  7. Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.  
  8.   
  9. Oracle is a registered trademark of Oracle Corporation and/or its  
  10. affiliates. Other names may be trademarks of their respective  
  11. owners.  
  12.   
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  14.   
  15. mysql> /*!mycat:catlet=demo.catlets.ShareJoin */SELECT * from t_service s,t_user u ON u.u_id=s.s_uid;  
  16. ERROR 1064 (HY000): java.lang.ClassNotFoundException: demo.catlets.ShareJoin  
  17. mysql> /*!mycat:catlet=io.mycat.catlets.ShareJoin */SELECT * from t_service s,t_user u ON u.u_id=s.s_uid;  
  18. Empty set (0.15 sec)  
  19.   
  20. mysql> show databases;  
  21. +----------+  
  22. | DATABASE |  
  23. +----------+  
  24. | TESTDB   |  
  25. +----------+  
  26. 1 row in set (0.00 sec)  
  27.   
  28. mysql> /*!mycat:catlet=io.mycat.catlets.ShareJoin */SELECT * from t_service s,t_user u ON u.u_id=s.s_uid;  
  29. +------+--------------+-------+-------+--------+----------------+  
  30. | s_id | s_name       | s_uid | s_uid | u_name | u_email        |  
  31. +------+--------------+-------+-------+--------+----------------+  
  32. |    1 | MYCATSERVICE |     1 |     1 | boonya | [email protected] |  
  33. +------+--------------+-------+-------+--------+----------------+  
  34. 1 row in set (0.02 sec)  
  35.   
  36. mysql> /*!mycat:catlet=io.mycat.catlets.ShareJoin */SELECT * from t_service s,t_user u ON u.u_id=s.s_uid;  
  37. ERROR 2006 (HY000): MySQL server has gone away  
  38. No connection. Trying to reconnect...  
  39. Connection id:    15  
  40. Current database: *** NONE ***  
  41.   
  42. +------+--------------+-------+-------+--------+-----------------+  
  43. | s_id | s_name       | s_uid | s_uid | u_name | u_email         |  
  44. +------+--------------+-------+-------+--------+-----------------+  
  45. |    1 | MYCATSERVICE |     1 |     1 | boonya | [email protected]  |  
  46. |    2 | SHOPPING     |     2 |     2 | niuniu | [email protected] |  
  47. +------+--------------+-------+-------+--------+-----------------+  
  48. 2 rows in set (0.02 sec)  
  49.   
  50. mysql>   

JOIN查询有效。

limit查询

[plain] view plain copy
  1. +------+--------------+-------+-------+--------+-----------------+  
  2. | s_id | s_name       | s_uid | s_uid | u_name | u_email         |  
  3. +------+--------------+-------+-------+--------+-----------------+  
  4. |    1 | MYCATSERVICE |     1 |     1 | boonya | [email protected]  |  
  5. |    2 | SHOPPING     |     2 |     2 | niuniu | [email protected] |  
  6. +------+--------------+-------+-------+--------+-----------------+  
  7. 2 rows in set (0.37 sec)  
  8.   
  9. mysql> /*!mycat:catlet=io.mycat.catlets.ShareJoin */SELECT * from t_service s,t_user u ON u.u_id=s.s_uid limit 2;  
  10. +------+--------------+-------+-------+--------+-----------------+  
  11. | s_id | s_name       | s_uid | s_uid | u_name | u_email         |  
  12. +------+--------------+-------+-------+--------+-----------------+  
  13. |    1 | MYCATSERVICE |     1 |     1 | boonya | [email protected]  |  
  14. |    2 | SHOPPING     |     2 |     2 | niuniu | [email protected] |  
  15. +------+--------------+-------+-------+--------+-----------------+  
  16. 2 rows in set (0.06 sec)  
  17.   
  18. mysql> /*!mycat:catlet=io.mycat.catlets.ShareJoin */SELECT * from t_service s,t_user u ON u.u_id=s.s_uid limit 1;  
  19. +------+--------------+-------+-------+--------+----------------+  
  20. | s_id | s_name       | s_uid | s_uid | u_name | u_email        |  
  21. +------+--------------+-------+-------+--------+----------------+  
  22. |    1 | MYCATSERVICE |     1 |     1 | boonya | [email protected] |  
  23. +------+--------------+-------+-------+--------+----------------+  
  24. 1 row in set (0.02 sec)  

最终看得到数据表对应关系如下:


版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/boonya/article/details/73200994

猜你喜欢

转载自blog.csdn.net/u010719917/article/details/80344881