更新时间:2024-05-31 GMT+08:00
分享

指定扫描并行度的Hint

功能描述

在并行的执行计划中,指定表扫描的并行度。

语法格式

scandop([@queryblock] table dop_num)

参数说明

  • @queryblock请参见指定Hint所处于的查询块Queryblock,可省略,表示在当前查询块生效。
  • table表示hint指定的表,只能指定一个表,如果表存在别名应优先使用别名进行hint。
  • dop_num表示使用表扫描的并行度。
  • scandop指定扫描并行度的hint。

示例

--准备
CREATE TABLE cst1(a int, b int, c int, d bigint);
set explain_perf_mode = pretty;  --打开explain pretty选项,可以看到更详尽计划
--使用
gaussdb=# EXPLAIN (costs off) SELECT /*+ Set(query_dop 2) scandop(cst1 2)*/ * FROM cst1;
 id |                 operation                  
----+--------------------------------------------
  1 | ->  Streaming(type: LOCAL GATHER dop: 1/2)
  2 |    ->  Seq Scan on cst1
(2 rows)

可以在并行计划中,使用scandop hint可以成功指定扫描并行度。

只有当dop_num和当前并行度(query_dop)一致或为1时,hint才会生效。

gaussdb=# EXPLAIN (costs off) SELECT /*+ Set(query_dop 2) scandop(cst1 4)*/ * FROM cst1;
 id |      operation       
----+----------------------
  1 | ->  Seq Scan on cst1
(1 row)

对于一些带有子查询的场景,Stream算子会退化消除。此时scandop的hint虽然生效,但是不会生成stream计划。

gaussdb=# EXPLAIN(costs off) SELECT /*+ Set(query_dop 2) scandop(cst1 2) */ count(a) FROM cst1 WHERE b > (SELECT max(a) FROM cst1 ORDER BY c LIMIT 1);
 id |                 operation                  
----+--------------------------------------------
  1 | ->  Aggregate
  2 |    ->  Aggregate
  3 |       ->  Seq Scan on cst1
  4 |    ->  Limit  [3, InitPlan 1 (returns $0)]
  5 |       ->  Aggregate
  6 |          ->  Seq Scan on cst1
(6 rows)

 Predicate Information (identified by plan id) 
-----------------------------------------------
   3 --Seq Scan on cst1
         Filter: (b > $0)
(2 rows)

对比不带hint的计划。

 id |                 operation                  
----+--------------------------------------------
  1 | ->  Aggregate
  2 |    ->  Seq Scan on cst1
  3 |    ->  Limit  [2, InitPlan 1 (returns $0)]
  4 |       ->  Aggregate
  5 |          ->  Seq Scan on cst1
(5 rows)

 Predicate Information (identified by plan id) 
-----------------------------------------------
   2 --Seq Scan on cst1
         Filter: (b > $0)
(2 rows)

可以看到带scandop hint的计划多了一个Aggregate算子,这是Stream算子消除后的残留。

普通计划只有一层Aggregate算子,两种计划不影响最终结果。

分享:

    相关文档

    相关产品