mycat垂直分庫、垂直分片:mycat的多表跨庫的一次實戰
阿新 • • 發佈:2018-11-07
mycat垂直分庫:
userinfo庫包括,表user、表company
userorder庫包括:表order
涉及多表查詢:
配置如下:schema.xml配置如下:
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100"> <table name="order" dataNode="dn1"/> <table name="user" dataNode="dn2"/> <table name="compnay" dataNode="dn2"/> <!-- <table name="oc_call" primaryKey="ID" dataNode="dn1$0-743" rule="latest-month-calldate" /> --> </schema> <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743" /> --> <dataNode name="dn1" dataHost="localhost1" database="db1" /> <dataNode name="dn2" dataHost="localhost1" database="db2" /> <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="localhost:3306" user="root" password="root"> </writeHost> <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> --> </dataHost> </mycat:schema>
跨庫三個表以上查詢會報錯:
/*!mycat:catlet=io.mycat.catlets.ShareJoin */
select user.username, company.companyname,order.ordername
from user inner join company on user.companyid = company.companyid
inner join order on order.userid = user.userid
報錯提示:
跨庫多表查詢:
拋異常invalid route in sql, multi tables found but datanode has no intersection
最近一些查詢資料發現,mycat1.6.5支援兩個表的跨庫查詢: http://www.iocoder.cn/MyCAT/two-table-share-join/
如下跨庫sql能查出正確的結果。【注: 跨庫查詢注意註解的使用】/*!mycat:catlet=io.mycat.catlets.ShareJoin */
/*!mycat:catlet=io.mycat.catlets.ShareJoin */ select user.username,=order.ordername from user inner join order on order.userid = user.userid
主題繼續探索: 1) 可以修改 mycat原始碼進行拓展, https://github.com/MyCATApache/Mycat-Server
2)自己修改自己的業務邏輯程式碼實現,如上最有兩個表的跨庫查詢。