当前位置: 首页 > 工具软件 > Shard-Query > 使用案例 >

Orcle 12c Sharding新特性---语句级别路由和Cross-Shard查询

乐刚毅
2023-12-01

1 说明

Proxy routing enables aggregation of data and reporting across shards. It also allows the flexibility for any database application to execute SQL statements (including SELECT and DML) in a system where tables are sharded or duplicated without the need to specify the sharding key (during connect) where the query should execute.

代理路由支持数据的聚合和跨shards的报表。它还允许任何数据库应用程序在sharded 表或duplicated表的系统中执行SQL语句(包括SELECT和DML)的灵活性,而无需指定查询应执行的分片键(在连接期间)。

在SDB中有两种类型的表:sharded tables 和duplicated tables.

  • Sharded tables are equi-partitioned on a sharding key.
    S=S1 U S2 U … U Sn

  • Duplicated tables are identical on all shards.
    R = R1 = … = Rn

Sharded和Duplicated表介绍:https://blog.csdn.net/qianglei6077/article/details/90405688

Proxy routing in an SDB provides a transparent mechanism to execute typical SQL queries that access data from sharded and duplicated tables without requiring the application to specify the relevant shards The SQL compiler identifies the relevant shards automatically and coordinates the query execution across all the participating shards. Database links are used for the communication between the coordinator and the shards.

SDB中的代理路由提供了一种透明的机制来执行典型的SQL查询,以从sharded 和duplicated 表中访问数据,而不需要应用程序指定相关的分片。SQL编译器自动识别相关的分片,并协调所有参与分片的查询执行。 数据库链接用于协调器和分片之间的通信。

2 实验

搭建Sharding Database参考:http://www.cndba.cn/Expect-le/article/2160

2.1 创建Sharded表并插入数据

–只支持sharded 和duplicated 表

SQL> create table lei(id number,name varchar2(30));
create table lei(id number,name varchar2(30))
*
ERROR at line 1:
ORA-02556: Only sharded or duplicated tables are supported across shards.
SQL> CREATE SHARDED TABLE LEI
(USERID NUMBER NOT NULL
,NAME VARCHAR2(30)
)
PARTITION BY CONSISTENT HASH (USERID)
PARTITIONS AUTO
TABLESPACE SET TPS_1;
Table created.

注意:如果该用户下已经有其他表了,那么再创建表时需要指定父表,否则“ORA-02530: parent table not specified

insert into LEI values(1,'sihong');
insert into LEI values(2,'suyi');
insert into LEI values(3,'ruyan');
insert into LEI values(4,'dongsheng');
insert into LEI values(5,'wenqing');
commit;

–查看shard节点1上的数据,可以看到该节点上存储了两条数据,那么其他三条数据就存储在其他shard节点上

SQL> select * from lei;
    USERID NAME
---------- ------------------------------
 5 wenqing
 2 suyi

–查看shard节点2

SQL> select * from lei;
    USERID NAME
---------- ------------------------------
 1 sihong
 3 ruyan
 4 dongsheng

2.2 用于Multi-Shard Queries的代理路由

Multi-Shard必须要从多个shard上获取数据,并且每个shard上的处理与任何其他shard无关。

多分片查询映射到多个分片,协调器可能需要在将结果发送给客户端之前进行一些处理。 例如,以下查询将获取每个客户的订单数量。

Q2: SELECT count(*), c.custno
    FROM customers c, orders o
    WHERE c.custno = o.custno
    GROUP BY c.custno;

协调器将查询转换为以下内容。

Q3: SELECT sum(count_col), custno
    FROM (SELECT count(*) count_col, c.custno
          FROM customers c, orders o
          WHERE c.custno = o.custno
          GROUP BY c.custno)
GROUP BY custno;

注意:在12.2中,Multi-Shard Queries只支持SELECT。

—具体例子及执行计划

SQL> set autot on
SQL> select count(*) from lei;
  COUNT(*)
----------
 5

在Oracle Database 12c Release 2(12.2.0.1)中,分片查询的执行计划仅显示协调器的执行计划。 必须连接到shard以查看查询的分片计划。 shard上查询的SQL ID与协调器上的原始查询的SQL ID不同,因此分片查询SQL文本嵌入了协调器的SQL ID,分片用户必须使用SQL ID查找SQL 的原始SQL作为SQL文本中的注释。

注意:在原始SQL中使用的HINT,也会在shard上执行。

2.3 用于SSQ(Single-Shard Queries)的代理路由

SSQ类似于连接到特定分片并在该分片上发出查询的客户端。 在这种情况下,整个查询将在单个分片上执行,协调器将已处理的行传递回客户端。 协调器上的计划将类似于远程映射的游标。

SSQ支持:

  • Equality and In-list, such as Area = ‘West’

  • Conditions containing literal, bind, or expression of literals and binds, such as

    • Area = :bind
    • Area = CASE :bind <10 THEN ‘West’ ELSE ‘East’ END
  • SELECT, UPDATE, DELETE and INSERT

更详细信息查看:
http://docs.oracle.com/database/122/ADMIN/sharding-data-routing.htm#ADMIN-GUID-8CB6442A-9326-4242-892A-4383E05C1C48

 类似资料: