[TOC] # EXCHANGE EXCHANGE 算子用于线程间进行数据交互的算子。 EXCHANGE 算子适用于在分布式场景,一般都是成对出现的,数据源端有一个 OUT 算子,目的端会有一个 IN 算子。 ## EXCH-IN/OUT EXCH-IN/OUT 即 EXCHANGE IN/ EXCHANGE OUT 用于将多个分区上的数据汇聚到一起,发送到查询所在的主节点上。 如下例所示,下面的查询中访问了 5 个分区的数据(p0-p4),其中 1 号算子接受 2 号算子产生的输出,并将数据传出;0 号算子接收多个分区上 1 号算子产生的输出,并将结果汇总输出。 ~~~ obclient>CREATE TABLE t (c1 INT, c2 INT) PARTITION BY HASH(c1) PARTITIONS 5; Query OK, 0 rows affected (0.12 sec) obclient>EXPLAIN SELECT * FROM t\G; *************************** 1. row *************************** Query Plan: ============================================== |ID|OPERATOR |NAME|EST. ROWS|COST | ---------------------------------------------- |0 |EXCHANGE IN DISTR | |500000 |545109| |1 | EXCHANGE OUT DISTR| |500000 |320292| |2 | TABLE SCAN |T |500000 |320292| ============================================== Outputs & filters: ------------------------------------- 0 - output([T.C1], [T.C2]), filter(nil) 1 - output([T.C1], [T.C2]), filter(nil) 2 - output([T.C1], [T.C2]), filter(nil), access([T.C1], [T.C2]), partitions(p[0-4]) ~~~ 上述示例的执行计划展示中的 outputs & filters 详细列出了 EXCH-IN/OUT 算子的输出信息如下: <table data-tag="table" id="table-r2s-ra3-31b" class="table"><colgroup span="1" width="200" data-tag="col" id="col-bc7-0ww-mlj" colwidth="1*" colnum="1" colname="col1" style="width:50%" class="col"></colgroup><colgroup span="1" width="540" data-tag="col" id="col-x8o-w8s-k5h" colwidth="1*" colnum="2" colname="col2" style="width:50%" class="col"></colgroup><thead id="thead-z6r-7qu-4hh" class="thead"><tr id="tr-8l9-pe4-z5n"><th id="td-cop-k71-dj4"><p id="p-xl0-7k4-559">信息名称</p></th><th id="td-72i-wtg-goq"><p id="p-zgq-njb-lz6">含义</p></th></tr></thead><tbody data-tag="tbody" id="tbody-i9z-tvy-nx2" class="tbody"><tr data-tag="tr" id="tr-k0d-10d-ayp" class="tr"><td data-tag="td" id="td-7q9-w4g-ijl" class="td"><p id="p-yc9-poh-myx">output</p></td><td data-tag="td" id="td-0gg-lgw-mye" class="td"><p id="p-1o5-5n7-jrz">该算子输出的表达式。</p></td></tr><tr data-tag="tr" id="tr-0ke-ug0-v24" class="tr"><td data-tag="td" id="td-n3c-1br-0j2" class="td"><p id="p-iar-ov7-8xx">filter</p></td><td data-tag="td" id="td-g4q-u97-dke" class="td"><p id="p-ee3-rbc-gex">该算子上的过滤条件。</p><p id="p-451-gvd-kef">由于示例中 EXCH-IN/OUT 算子没有设置 filter,所以为 nil。</p></td></tr></tbody></table> ## EXCH-IN/OUT (REMOTE) EXCH-IN/OUT (REMOTE) 算子用于将远程的数据(单个分区的数据)拉回本地。 如下例所示,在 A 机器上创建了一张非分区表,在 B 机器上执行查询,读取该表的数据。此时,由于待读取的数据在远程,执行计划中分配了 0 号算子和 1 号算子来拉取远程的数据。其中,1 号算子在 A 机器上执行,读取 t 表的数据,并将数据传出;0 号算子在 B 机器上执行,接收 1 号算子产生的输出。 ~~~ obclient>CREATE TABLE t (c1 INT, c2 INT); Query OK, 0 rows affected (0.12 sec) obclient>EXPLAIN SELECT * FROM t\G; *************************** 1. row *************************** Query Plan: =============================================== |ID|OPERATOR |NAME|EST. ROWS|COST | ----------------------------------------------- |0 |EXCHANGE IN REMOTE | |100000 |109029| |1 | EXCHANGE OUT REMOTE| |100000 |64066 | |2 | TABLE SCAN |T |100000 |64066 | =============================================== Outputs & filters: ------------------------------------- 0 - output([T.C1], [T.C2]), filter(nil) 1 - output([T.C1], [T.C2]), filter(nil) 2 - output([T.C1], [T.C2]), filter(nil), access([T.C1], [T.C2]), partitions(p0) ~~~ 上述示例的执行计划展示中的 outputs & filters 详细列出了 EXCH-IN/OUT (REMOTE) 算子的输出信息,字段的含义与 EXCH-IN/OUT 算子相同。 ## EXCH-IN/OUT (PKEY) EXCH-IN/OUT (PKEY) 算子用于数据重分区。它通常用于二元算子中,将一侧孩子节点的数据按照另外一些孩子节点的分区方式进行重分区。 如下示例中,该查询是对两个分区表的数据进行联接,执行计划将 s 表的数据按照 t 的分区方式进行重分区,4 号算子的输入是 s 表扫描的结果,对于 s 表的每一行,该算子会根据 t 表的数据分区,以及根据查询的联接条件,确定一行数据应该发送到哪个节点进行。 此外,可以看到 3 号算子是一个 EXCHANGE IN MERGE SORT DISTR,它是一个特殊的 EXCHANGE IN 算子,它用于在汇总多个分区的数据时,会进行一定的归并排序,在这个执行计划中,3 号算子接收到的每个分区的数据都是按照 c1 有序排列的,它会对每个接收到的数据进行归并排序,从而保证结果输出结果也是按照 c1 有序排列的。 ~~~ obclient>CREATE TABLE t (c1 INT, c2 INT) PARTITION BY HASH(c1) PARTITIONS 5; Query OK, 0 rows affected (0.12 sec) obclient>CREATE TABLE s (c1 INT PRIMARY KEY, c2 INT) PARTITION BY HASH(c1) PARTITIONS 4; Query OK, 0 rows affected (0.12 sec) obclient>EXPLAIN SELECT * FROM s, t WHERE s.c1 = t.c1\G; *************************** 1. row *************************** Query Plan: =============================================================== |ID|OPERATOR |NAME|EST. ROWS |COST | --------------------------------------------------------------- |0 |EXCHANGE IN DISTR | |1960200000|3090308367| |1 | EXCHANGE OUT DISTR | |1960200000|1327558071| |2 | MERGE JOIN | |1960200000|1327558071| |3 | EXCHANGE IN MERGE SORT DISTR| |400000 |436080 | |4 | EXCHANGE OUT DISTR (PKEY) | |400000 |256226 | |5 | TABLE SCAN |S |400000 |256226 | |6 | TABLE SCAN |T |500000 |320292 | =============================================================== Outputs & filters: ------------------------------------- 0 - output([S.C1], [S.C2], [T.C1], [T.C2]), filter(nil) 1 - output([S.C1], [S.C2], [T.C1], [T.C2]), filter(nil) 2 - output([S.C1], [S.C2], [T.C1], [T.C2]), filter(nil), equal_conds([S.C1 = T.C1]), other_conds(nil) 3 - output([S.C1], [S.C2]), filter(nil), sort_keys([S.C1, ASC]) 4 - (#keys=1, [S.C1]), output([S.C1], [S.C2]), filter(nil) 5 - output([S.C1], [S.C2]), filter(nil), access([S.C1], [S.C2]), partitions(p[0-3]) 6 - output([T.C1], [T.C2]), filter(nil), access([T.C1], [T.C2]), partitions(p[0-4]) ~~~ 上述示例的执行计划展示中的 outputs & filters 详细列出了 EXCH-IN/OUT (PKEY) 算子的输出信息如下: <table data-tag="table" id="table-y58-uwx-zz9" class="table"><colgroup span="1" width="200" data-tag="col" id="col-i0l-jbv-xo7" colwidth="1*" colnum="1" colname="col1" style="width:50%" class="col"></colgroup><colgroup span="1" width="540" data-tag="col" id="col-2kd-y1l-rqi" colwidth="1*" colnum="2" colname="col2" style="width:50%" class="col"></colgroup><thead id="thead-im2-pwz-alv" class="thead"><tr id="tr-fab-ydv-ny8"><th id="td-qhn-ula-51y"><p id="p-ko7-ult-hhs">信息名称</p></th><th id="td-myw-gzs-dad"><p id="p-ajb-upm-pbj">含义</p></th></tr></thead><tbody data-tag="tbody" id="tbody-adb-yw3-qz1" class="tbody"><tr data-tag="tr" id="tr-jci-sxr-jlh" class="tr"><td data-tag="td" id="td-510-kj0-5rn" class="td"><p id="p-6tx-3tq-ehj">output</p></td><td data-tag="td" id="td-1c4-fi3-96y" class="td"><p id="p-iol-zui-4al">该算子输出的表达式。</p></td></tr><tr data-tag="tr" id="tr-ljn-gdw-9xf" class="tr"><td data-tag="td" id="td-8zu-mj9-5h6" class="td"><p id="p-a20-z5k-8tq">filter</p></td><td data-tag="td" id="td-3xg-qe3-rc5" class="td"><p id="p-6fu-627-6gc">该算子上的过滤条件。</p><p id="p-fqr-ihc-61w">由于示例中 EXCH-IN/OUT(PKEY)算子没有设置 filter,所以为 nil。</p></td></tr><tr data-tag="tr" id="tr-9wx-qu5-cyl" class="tr"><td id="td-bdg-t2z-loj"><p id="p-l42-77e-inz">pkey</p></td><td id="td-kr3-l9p-0lm"><p id="p-q17-irl-7xi">按照哪一列进行重分区。</p><p id="p-nof-78y-tlz">例如,<code data-tag="code" class="code">#keys=1, [s.c1]</code> 表示按照 c1 这一列重分区</p></td></tr></tbody></table> ## EXCH-IN/OUT (HASH) EXCH-IN/OUT (HASH) 算子用于对数据使用一组 HASH 函数进行重分区。 如下例所示的执行计划中,3-5 号以及 7-8 号是两组使用 HASH 重分区的 EXCHANGE 算子。这两组算子的作用是把 t 表和 s 表的数据按照一组新的 HASH 函数打散成多份,在这个例子中 HASH 的列为 t.c2 和 s.c2,这保证了 c2 取值相同的行会被分发到同一份中。基于重分区之后的数据,2 号算子 HASH JOIN 会对每一份数据按照`t.c2= s.c2`进行联接。 此外,由于查询中执行了并行度为 2,计划中展示了 dop = 2 (dop 是 Degree of Parallelism 的缩写)。 ~~~ obclient>CREATE TABLE t (c1 INT, c2 INT) PARTITION BY HASH(c1) PARTITIONS 4; Query OK, 0 rows affected (0.12 sec) obclient>CREATE TABLE s (c1 INT, c2 INT) PARTITION BY HASH(c1) PARTITIONS 4; Query OK, 0 rows affected (0.12 sec) obclient>EXPLAIN SELECT /*+PQ_DISTRIBUTE(@"SEL$1" ("TEST.S"@"SEL$1" ) HASH HASH), PARALLEL(2)*/ * FROM t, s WHERE t.c2 = s.c2\G; *************************** 1. row *************************** Query Plan: ================================================================= |ID|OPERATOR |NAME |EST. ROWS |COST | ----------------------------------------------------------------- |0 |PX COORDINATOR | |1568160000|2473629500| |1 | EXCHANGE OUT DISTR |:EX10002|1568160000|1063429263| |2 | HASH JOIN | |1568160000|1063429263| |3 | EXCHANGE IN DISTR | |400000 |436080 | |4 | EXCHANGE OUT DISTR (HASH)|:EX10000|400000 |256226 | |5 | PX PARTITION ITERATOR | |400000 |256226 | |6 | TABLE SCAN |T |400000 |256226 | |7 | EXCHANGE IN DISTR | |400000 |436080 | |8 | EXCHANGE OUT DISTR (HASH)|:EX10001|400000 |256226 | |9 | PX PARTITION ITERATOR | |400000 |256226 | |10| TABLE SCAN |S |400000 |256226 | ================================================================= Outputs & filters: ------------------------------------- 0 - output([T.C1], [T.C2], [S.C1], [S.C2]), filter(nil) 1 - output([T.C1], [T.C2], [S.C1], [S.C2]), filter(nil), dop=2 2 - output([T.C1], [T.C2], [S.C1], [S.C2]), filter(nil), equal_conds([T.C2 = S.C2]), other_conds(nil) 3 - output([T.C1], [T.C2]), filter(nil) 4 - (#keys=1, [T.C2]), output([T.C1], [T.C2]), filter(nil), dop=2 5 - output([T.C1], [T.C2]), filter(nil) 6 - output([T.C1], [T.C2]), filter(nil), access([T.C1], [T.C2]), partitions(p[0-3]) 7 - output([S.C1], [S.C2]), filter(nil) 8 - (#keys=1, [S.C2]), output([S.C1], [S.C2]), filter(nil), dop=2 9 - output([S.C1], [S.C2]), filter(nil) 10 - output([S.C1], [S.C2]), filter(nil), access([S.C1], [S.C2]), partitions(p[0-3]) ~~~ 其中,PX PARTITION ITERATO 算子用于按照分区粒度迭代数据,详细信息请参见[GI](https://open.oceanbase.com/docs/community/oceanbase-database/V3.1.0/GI-1)。 上述示例的执行计划展示中的 outputs & filters 详细列出了 EXCH-IN/OUT (HASH) 算子的输出信息如下: <table data-tag="table" id="table-aoh-27c-kxz" class="table"><colgroup span="1" width="200" data-tag="col" id="col-cmr-hqo-zvx" colwidth="1*" colnum="1" colname="col1" style="width:50%" class="col"></colgroup><colgroup span="1" width="540" data-tag="col" id="col-5k0-01h-m1j" colwidth="1*" colnum="2" colname="col2" style="width:50%" class="col"></colgroup><thead id="thead-ow6-5km-ka4" class="thead"><tr id="tr-u7s-4kf-8sr"><th id="td-atw-ra4-wn8"><p id="p-1ht-dl6-eev">信息名称</p></th><th id="td-hjt-wg2-1bk"><p id="p-oau-6xw-u5d">含义</p></th></tr></thead><tbody data-tag="tbody" id="tbody-qfd-tdk-vcr" class="tbody"><tr data-tag="tr" id="tr-fj3-cwm-r3l" class="tr"><td data-tag="td" id="td-5se-18g-io5" class="td"><p id="p-s4w-9d7-5wb">output</p></td><td data-tag="td" id="td-qt3-gzg-w11" class="td"><p id="p-fp9-jjq-eqy">该算子输出的表达式。</p></td></tr><tr data-tag="tr" id="tr-ws8-gjb-fop" class="tr"><td data-tag="td" id="td-xqs-gti-6q8" class="td"><p id="p-yoh-tsl-g8z">filter</p></td><td data-tag="td" id="td-zuq-46a-x5d" class="td"><p id="p-yb8-4vx-xj8">该算子上的过滤条件。</p><p id="p-3r0-zwt-lrb">由于示例中 EXCH-IN/OUT (HASH) 算子没有设置 filter,所以为 nil。</p></td></tr><tr data-tag="tr" id="tr-pj8-0ak-kvi" class="tr"><td id="td-18c-f9y-po5"><p id="p-345-xem-2p5">pkey</p></td><td id="td-66t-otm-8zd"><p id="p-36o-2u3-r7u">按照哪一列进行 HASH 重分区。</p><p id="p-dkl-9t7-jul">例如,<code data-tag="code" class="code">#keys=1, [s.c2]</code> 表示按照 c2 这一列进行 HASH 重分区。</p></td></tr></tbody></table> ## EXCH-IN/OUT(BROADCAST) EXCH-IN/OUT(BROADCAST) 算子用于对输入数据使用 BROADCAST 的方法进行重分区,它会将数据广播到其他线程上。 如下示例的执行计划中,3-4 号是一组使用 BROADCAST 重分区方式的 EXCHANGE 算子。它会将 t 表的数据广播到每个线程上,s 表每个分区的数据都会尝试和被广播的 t 表数据进行联接。 ~~~ obclient>CREATE TABLE t (c1 INT, c2 INT) PARTITION BY HASH(c1) PARTITIONS 4; Query OK, 0 rows affected (0.12 sec) obclient>CREATE TABLE s (c1 INT, c2 INT) PARTITION BY HASH(c1) PARTITIONS 4; Query OK, 0 rows affected (0.12 sec) obclient>INSERT INTO s VALUES (1, 1), (2, 2), (3, 3), (4, 4); Query OK, 1 rows affected (0.12 sec) obclient>EXPALIN SELECT /*+PARALLEL(2) */ * FROM t, s WHERE t.c2 = s.c2\G; *************************** 1. row *************************** Query Plan: ====================================================================== |ID|OPERATOR |NAME |EST. ROWS |COST | ---------------------------------------------------------------------- |0 |PX COORDINATOR | |1568160000|2473449646| |1 | EXCHANGE OUT DISTR |:EX10001|1568160000|1063249409| |2 | HASH JOIN | |1568160000|1063249409| |3 | EXCHANGE IN DISTR | |400000 |436080 | |4 | EXCHANGE OUT DISTR (BROADCAST)|:EX10000|400000 |256226 | |5 | PX PARTITION ITERATOR | |400000 |256226 | |6 | TABLE SCAN |T |400000 |256226 | |7 | PX PARTITION ITERATOR | |400000 |256226 | |8 | TABLE SCAN |S |400000 |256226 | ====================================================================== Outputs & filters: ------------------------------------- 0 - output([T.C1], [T.C2], [S.C1], [S.C2]), filter(nil) 1 - output([T.C1], [T.C2], [S.C1], [S.C2]), filter(nil), dop=2 2 - output([T.C1], [T.C2], [S.C1], [S.C2]), filter(nil), equal_conds([T.C2 = S.C2]), other_conds(nil) 3 - output([T.C1], [T.C2]), filter(nil) 4 - output([T.C1], [T.C2]), filter(nil), dop=2 5 - output([T.C1], [T.C2]), filter(nil) 6 - output([T.C1], [T.C2]), filter(nil), access([T.C1], [T.C2]), partitions(p[0-3]) 7 - output([S.C1], [S.C2]), filter(nil) 8 - output([S.C1], [S.C2]), filter(nil), access([S.C1], [S.C2]), partitions(p[0-3]) ~~~ 上述示例的执行计划展示中的 outputs & filters 详细列出了 EXCH-IN/OUT (BROADCAST) 算子的信息,字段的含义与 EXCH-IN/OUT 算子相同。