注释透传

最近更新时间: 2024-10-17 17:10:00

常用语法

注释透传指支持透传 SQL语句到对应的一个或者多个物理分片(Set),并透传到分表键(Shardkey)对应的分片(Set)中的操作方式。

具体语法如下:

/*sets:set_1*/
/*sets:set_1,set_2*/ (set名字可以通过/*proxy*/show status查询)
/*sets:allsets */

注意:

对于分布式实例,Proxy 会对 SQL进行语法解析,但有比较严格的限制,如果用户想在某个物理分片(set)中执行SQL语句,可以使用该功能

示例:

MySQL [test]> DROP TABLE IF EXISTS test1;
Query OK, 0 rows affected (0.08 sec)

MySQL [test]> create table test1 (a int key, b int, c char(20)) shardkey=a;
Query OK, 0 rows affected (1.71 sec)

--加载300行数据到test1表之后:
MySQL [test]> select count(*) from test1;
+----------+
| count(*) |
+----------+
|      300 |
+----------+
1 row in set (0.12 sec)

MySQL [test]> select count(*) from test1;
+----------+
| count(*) |
+----------+
|      300 |
+----------+
1 row in set (0.11 sec)

MySQL [test]> /*sets:allsets */ select count(*) from test1;
+----------+------------------+
| count(*) | info             |
+----------+------------------+
|      150 | set_1619374020_1 |
|      150 | set_1619508344_3 |
+----------+------------------+
2 rows in set (0.02 sec)

MySQL [(none)]> /*proxy*/ show status;
+-----------------------------+-------------------------------------------+
| status_name                 | value                                     |
+-----------------------------+-------------------------------------------+
| cluster                     | group_1619373877_13                       |
| set_1619374020_1:ip         | 10.0.0.17:4007;s1@10.0.0.16:4007@1@IDC1@0 |
| set_1619374020_1:alias      | s1                                        |
| set_1619374020_1:hash_range | 0---31                                    |
| set_1619508344_3:ip         | 10.0.0.17:4008;s1@10.0.0.16:4008@1@IDC1@0 |
| set_1619508344_3:alias      | s2                                        |
| set_1619508344_3:hash_range | 32---62                                   |
| set                         | set_1619374020_1,set_1619508344_3         |
+-----------------------------+-------------------------------------------+
8 rows in set (0.00 sec)

MySQL [test]> /*sets:set_1619374020_1*/ select count(*) from test1;
+----------+------------------+
| count(*) | info             |
+----------+------------------+
|      150 | set_1619374020_1 |
+----------+------------------+
1 row in set (0.04 sec)

MySQL [test]> /*set_1619508344_3*/ select count(*) from test1;
+----------+
| count(*) |
+----------+
|      150 |
+----------+
1 row in set (0.11 sec)


MySQL [test]> delete from test1;
ERROR 913 (HY000): Proxy ERROR:Join internal error: delete query has no where clause

MySQL [test]> /*sets:allsets*/delete from test1;
Query OK, 300 rows affected (0.04 sec)

展示一致性读是否开启

注意:

  • $port等参数根据实例信息进行配置
以下返回结果成功查到consistent_read参数的值:
mysql -h{proxy_ip} -u{user} -p{password} -P{proxy_port} -c
/*proxy*/ show config;

统计数据显示

以下返回结果成功查到统计信息:
mysql -h{proxy_ip} -u{user} -p{password} -P{proxy_port} -c
/*proxy*/ show statistics;

读写分离新增

强制访问主库

赤兔上创建普通用户后,连接网关执行sql语句:
mysql -h{proxy_ip} -u{user} -p{password} -P{proxy_port} -c
/*master*/ select 1;

同机房就近访问

语法解释

/slave:localslave/ 优先访问同机房从库(随机访问同机房的从库),次之异机房从库(随机访问异机房的从库),否则访问主库
/slave:localnode/ 优先随机访问同机房主库&从库(随机访问本地所有集合),次之异机房主库& 从库(随机访问异地所有的集合)
/slave:localslave,slaveonly/ 优先访问同机房从库(随机访问同机房的从库),次之异机房从库(随机访问异机房的从库)否则失败
/slave:localnode,slaveonly/ 优先访问同机房从库(随机访问同机房的从库),次之异机房从库(随机访问异机房的从库)否则失败
/slave:localslave,20/ 优先访问同机房从库(随机选择小于20的延迟从库),次之异机房从库(随机选择小于20的延迟的从库),否则访问主库。
/slave:localnode,20/ 优先随机访问同机房主库&从库(将符合20延迟的从库和主库放一起,随机访问),次之异机房主库&从库(将符合20延迟的从库和主库放一起,随机访问)
/slave:localslave,slaveonly,20/ 优先访问同机房从库(随机访问小于20的同机房的从库),次之异机房从库(随机小于20的访问异机房的从库)否则失败
/slave:localnode,slaveonly,20/ 优先访问同机房从库(随机访问小于20的同机房的从库),次之异机房从库(随机小于20的访问异机房的从库)否则失败

示例

赤兔上创建普通用户后,连接网关执行sql语句查询sbtest1表:

/*slave:localslave*/ select a,info from sbtest1;
/*slave:localnode*/ select a,b from sbtest1;
/*slave:localslave,slaveonly*/ select a,info from sbtest1;
/*slave:localnode,slaveonly*/  select b,info from sbtest1;
/*slave:localslave,20*/  select info from sbtest1;
/*slave:localnode,20*/ select a from sbtest1;
/*slave:localslave,slaveonly,20*/ select b from sbtest1;
/*slave:localnode,slaveonly,20*/  select a,b,info from sbtest1;