跨分片查询之最佳实践-亲试ok

 
 
#跨分片查询
select supplier_name,b.region_name as '省',c.region_name as '市',d.region_name as '区' from product_supplier_info a
 join region_info b on b.region_name=a.address
 join region_info c on c.region_name=a.address
 join region_info d on d.region_name=a.address;
 
ERROR 1064 (HY000): invalid route in sql, multi tables found but datanode has no intersection  sql:select supplier_name,b.region_name as '',c.region_name as '',d.region_name as '' from product_supplier_info a
join region_info b on b.region_id=a.province
join region_info c on c.region_id=a.city
join region_info d on d.region_id=a.city
MySQL [imooc_db]>
 
报错:错误的路由
 
将库order_db的region_info 导出 然后分别导入的 product_db和custer_db
 
#order_db导出 region_info
[root@s-158 ~]# mysqldump -uroot -p123456 --socket=/data/my3306/run/mysql.sock order_db region_info >>/scripts/region_info.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@s-158 ~]# ls -ltr /scripts/region_info.sql
-rw-r--r--. 1 root root 170243 9月  17 20:23 /scripts/region_info.sql
 
将导出文件拷贝至custer_db 和 product_db
[root@s-158 ~]# scp /scripts/region_info.sql 192.168.124.159:/scripts/
root@192.168.124.159's password:
region_info.sql                                                                                             100%  166KB   2.2MB/s   00:00    
[root@s-158 ~]# scp /scripts/region_info.sql 192.168.124.160:/scripts/
The authenticity of host '192.168.124.160 (192.168.124.160)' can't be established.
ECDSA key fingerprint is SHA256:jrPoh+mX9oPlztOcm9a4jELVMJOzhoLH17HTbbQTrcs.
ECDSA key fingerprint is MD5:a6:c3:0c:3d:02:41:9e:83:17:db:1b:ed:2b:38:2f:64.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.124.160' (ECDSA) to the list of known hosts.
root@192.168.124.160's password:
region_info.sql                                                                                             100%  166KB   2.9MB/s   00:00   
 
#在product_db执行导入操作
[root@s-159 network-scripts]# mysql -uroot -p123456 product_db < /scripts/region_info.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
 
#在custer_db执行导入操作
[root@s-160 ~]# mysql -uroot -p123456 custer_db < /scripts/region_info.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
 
#mycat服务器 修改mycat的配置文件 schema.xml
  <schema name="imooc_db" checkSQLschema="false" sqlMaxLimit="100" >
    <table name="order_master" primaryKey="order_id" dataNode="ord_dn"/>
    <table name="order_cart" primaryKey="cart_id" dataNode="ord_dn"/>
    <table name="order_customer_addr" primaryKey="customer_addr_id" dataNode="ord_dn"/>
    <table name="region_info" primaryKey="region_id" dataNode="ord_dn,prod_dn,cust_dn" type="global"/>
    
    
#再次确认结果
MySQL [imooc_db]> select supplier_name,b.region_name as '省',c.region_name as '市',d.region_name as '区' from product_supplier_info a
    -> join region_info b on b.region_name=a.address
    -> join region_info c on c.region_name=a.address
    -> join region_info d on d.region_name=a.address;
+---------------+-----------+-----------+-----------+
| supplier_name | 省        | 市        | 区        |
+---------------+-----------+-----------+-----------+
| 供应商-3      | 北京市    | 北京市    | 北京市    |
| 供应商-3      | 北京市    | 北京市    | 北京市    |
| 供应商-3      | 北京市    | 北京市    | 北京市    |
| 供应商-3      | 北京市    | 北京市    | 北京市    |
| 供应商-1      | 上海市    | 上海市    | 上海市    |
| 供应商-1      | 上海市    | 上海市    | 上海市    |
| 供应商-1      | 上海市    | 上海市    | 上海市    |
| 供应商-1      | 上海市    | 上海市    | 上海市    |
| 供应商-2      | 天津市    | 天津市    | 天津市    |
| 供应商-2      | 天津市    | 天津市    | 天津市    |
| 供应商-2      | 天津市    | 天津市    | 天津市    |
| 供应商-2      | 天津市    | 天津市    | 天津市    |
| 供应商-3      | 北京市    | 北京市    | 北京市    |
| 供应商-3      | 北京市    | 北京市    | 北京市    |
| 供应商-3      | 北京市    | 北京市    | 北京市    |
| 供应商-3      | 北京市    | 北京市    | 北京市    |
| 供应商-1      | 上海市    | 上海市    | 上海市    |
| 供应商-1      | 上海市    | 上海市    | 上海市    |
| 供应商-1      | 上海市    | 上海市    | 上海市    |
| 供应商-1      | 上海市    | 上海市    | 上海市    |
| 供应商-2      | 天津市    | 天津市    | 天津市    |
| 供应商-2      | 天津市    | 天津市    | 天津市    |
| 供应商-2      | 天津市    | 天津市    | 天津市    |
| 供应商-2      | 天津市    | 天津市    | 天津市    |
+---------------+-----------+-----------+-----------+
 
总结:需要那个公共表分散到各个数据库节点中。哈哈 
创建时间:2021-09-26 17:22
浏览量:0