标签:
一个真实的业务系统中,往往存在大量的类似字典表的数据表,它们与业务表之间可能有关系,这种关系,可以理解为“标签”,而不应理解为通常的
“主从关系”,这些表基本上很少变动,可以根据主键 ID进行缓存,下面这张图说明了一个典型的“标签关系”图:
在分片的情况下,当业务表因为规模而进行分片以后,业务表与这些附属的字典表之间的关联,就成了比较棘手的问题,考虑到字典表具有以下几个特性:
? 变动不频繁
? 数据量总体变化不大
? 数据规模不大,很少有超过数十万条记录。
鉴于此,MyCAT 定义了一种特殊的表,称之为“全局表”,全局表具有以下特性:
? 全局表的插入、更新操作会实时在所有节点上执行,保持各个分片的数据一致性
? 全局表的查询操作,只从一个节点获取
? 全局表可以跟任何一个表进行 JOIN 操作
MyCAT 可以满足 80%以上的企业应用开发。全局表有点类似于RAID 1,写的时候多份写入,读的时候从任意节点读取即可。
全局表配置比较简单,不用写 Rule 规则,修改schema.xml,如下配置即可:
<table name="company" primaryKey="id" type="global" dataNode="dn1,dn2" />
需要注意的是,全局表每个分片节点上都会运行创建表的 DDL 语句。
--修改配置后重新装载一下配置文件
mysql:9600> reload @@config;
Query OK, 1 row affected (0.03 sec)
Reload config success
--查看分片节点信息
mysql:9600>show @@datanode;
+------+-------------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
| NAME | DATHOST | INDEX | TYPE | ACTIVE | IDLE | SIZE | EXECUTE | TOTAL_TIME | MAX_TIME | MAX_SQL | RECOVERY_TIME |
+------+-------------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
| dn1 | 192.168.1.204/db1 | 0 | mysql | 0 | 10 | 1000 | 1112 | 0 | 0 | 0 | -1 |
| dn2 | 192.168.1.143/db2 | 0 | mysql | 0 | 8 | 1000 | 458 | 0 | 0 | 0 | -1 |
+------+-------------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
mysql:8066> use testdb;
Database changed
mysql> show tables like ‘%compa%‘;
+------------------+
| Tables in testdb |
+------------------+
| company |
+------------------+
mysql:8066>desc company;
ERROR 1146 (42S02): Table ‘db2.company‘ doesn‘t exist
mysql:8066>create table company(id int primary key,ename varchar(30),loc varchar(30));
Query OK, 0 rows affected (0.07 sec)
mysql:8066>desc company;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| ename | varchar(30) | YES | | NULL | |
| loc | varchar(30) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql:8066>insert into company values(1,‘IBM‘,‘ShenZhen‘);
Query OK, 1 row affected (0.01 sec)
-- 查看执行计划,可以看到当前的执行计划中的查询来自分片节点dn1
mysql:8066>explain select * from company;
+-----------+---------------------------------+
| DATA_NODE | SQL |
+-----------+---------------------------------+
| dn1 | SELECT * FROM company LIMIT 100 |
+-----------+---------------------------------+
mysql:8066>select * from company;
+----+-------+----------+
| id | ename | loc |
+----+-------+----------+
| 1 | IBM | ShenZhen |
+----+-------+----------+
-- 尝试从一台Linux主机来访问company全局表,可以看到此时是从第二个分片节点访问
[root@java_2 ~]# mysql -utest -ptest -h192.168.21.157 -P8066 -Dtestdb
(test@192.168.21.157) [testdb]> explain select * from company;
+-----------+---------------------------------+
| DATA_NODE | SQL |
+-----------+---------------------------------+
| dn2 | SELECT * FROM company LIMIT 100 |
+-----------+---------------------------------+
mysql> select ‘Leshami‘ as Author, ‘http://blog.csdn.net/leshami‘ as Blog;
+---------+------------------------------+
| Author | Blog |
+---------+------------------------------+
| Leshami | http://blog.csdn.net/leshami |
+---------+------------------------------+
-- 下面分别从各个分片节点直接访问,可以看到每一个分片节点都有相对应的数据行
(root@192.168.1.143:3307) [(none)]> select * from db2.company;
+----+-------+----------+
| id | ename | loc |
+----+-------+----------+
| 1 | IBM | ShenZhen |
+----+-------+----------+
(root@192.168.1.204:3306) [(none)]> select * from db1.company;
+----+-------+----------+
| id | ename | loc |
+----+-------+----------+
| 1 | IBM | ShenZhen |
+----+-------+----------+
1 row in set (0.00 sec)
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/leshami/article/details/49019701