MyCat 学习笔记 第十三篇.数据分片 之 通过HINT执行存储过程
1 环境说明
VM 模拟3台MYSQL 5.6 服务器
VM1 192.168.31.187:3307
VM2 192.168.31.212:3307
VM3 192.168.31.150: 3307
MYCAT 1.5 服务部署在宿主机上
MYCAT 192.168.31.207 :8806【SQL执行端口】 / 9066【管理端口】
2 应用场景
2.0 MYCAT配置
schema.xml
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="t_demo_travel_record" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" />
<table name="t_demo_travel_record_child" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" />
</schema>
<dataNode name="dn1" dataHost="vm1" database="test" />
<dataNode name="dn2" dataHost="vm2" database="test" />
<dataNode name="dn3" dataHost="vm3" database="test" />
<dataHost name="vm1" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="vm1M1" url="192.168.31.187:3307" user="root" password="root123"></writeHost>
</dataHost>
<dataHost name="vm2" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="vm2M1" url="192.168.31.212:3307" user="root" password="root123"></writeHost>
</dataHost>
<dataHost name="vm3" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="vm3M1" url="192.168.31.150:3307" user="root" password="root123"></writeHost>
</dataHost>
rule.xml
<tableRule name="auto-sharding-long">
<rule>
<columns>id</columns>
<algorithm>rang-long</algorithm>
</rule>
</tableRule>
<function name="rang-long"
class="org.opencloudb.route.function.AutoPartitionByLong">
<property name="mapFile">autopartition-long.txt</property>
<property name="defaultNode">0</property>
</function>
autopartition-long.txt
# range start-end ,data node index
# K=1000,M=10000.
0-500M=0
500M-1000M=1
1000M-1500M=2
2.1 hint
第一次知道并使用 hint 是在用oracle数据库调优的时候。
08年的时候,看到生产环境程序的SQL语句有 /* xxxx */ select xxx from table ,当初不知道何为hint,就一把删了。结果被 TPM 批了一顿~~~
MYCAT 中也可以hint ,这里主要讲一下如何通过 hint 指定物理数据库执行存储过程。
2.2 Mysql 存储过程 ,传参形式没有验证成功,不知道是否是mycat这个版本目前还不支持
DELIMITER ;
DROP PROCEDURE `test`.`p_travel_recrod_sum`;
DELIMITER //
CREATE PROCEDURE `p_travel_recrod_sum` ()
BEGIN
declare m_id_sum varchar(45);
select sum(id) into m_id_sum from t_demo_travel_record ;
update t_demo_travel_record t set t.context = t.context + m_id_sum ;
END //
DELIMITER ;
grant execute on procedure p_travel_recrod_sum to 'root'@'%';
flush privileges;
2.3 MYCAT 通过 hint 执行存储过程,根据分片规则ID=1的数据应该是在DN1结点上执行 p_travel_record_sum()
mysql> DELIMITER //
mysql> /*!mycat:sql=select * from t_demo_travel_record where id = 1;*/ call p_travel_recrod_sum() //
Query OK, 748003 rows affected, 64 warnings (9.57 sec)
查看下物理库的情况 DN1结点
mysql> select * from t_demo_travel_record limit 100,10;
+-------+---------------+
| id | context |
+-------+---------------+
| 10099 | 1870567757749 |
| 10100 | 1870567757749 |
| 10101 | 1870567757749 |
| 10102 | 1870567757749 |
| 10103 | 1870567757749 |
| 10104 | 1870567757749 |
| 10105 | 1870567757749 |
| 10106 | 1870567757749 |
| 10107 | 1870567757749 |
| 10108 | 1870567757749 |
+-------+---------------+
10 rows in set (0.00 sec)
DN2结点
mysql> select * from t_demo_travel_record limit 100,10;
+---------+-----------------+
| id | context |
+---------+-----------------+
| 5000101 | context_5000101 |
| 5000102 | context_5000102 |
| 5000103 | context_5000103 |
| 5000104 | context_5000104 |
| 5000105 | context_5000105 |
| 5000106 | context_5000106 |
| 5000107 | context_5000107 |
| 5000108 | context_5000108 |
| 5000109 | context_5000109 |
| 5000110 | context_5000110 |
+---------+-----------------+
10 rows in set (0.00 sec)
到此为止,通过 hint 执行 指定数据库 procedure 验证完成。