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

指定是否使用minmax优化的hint

功能描述

指定语句是否使用minmax改写。

语法格式

[no] use_minmax[(@queryblock)]

参数说明

  • no表示不使用minmax查询改写。

示例

--准备
create table minmaxtest(f1 int);
create index minmaxtesti on minmaxtest(f1);
insert into minmaxtest values(11), (12);
set explain_perf_mode=pretty;  --打开explain pretty选项,可以看到更详尽计划

--常规场景
gaussdb=# explain (costs off) select min(f1), max(f1) from minmaxtest;
 id |                                operation
----+--------------------------------------------------------------------------
  1 | ->  Result
  2 |    ->  Aggregate  [1, InitPlan 1 (returns $0)]
  3 |       ->  Streaming (type: GATHER)
  4 |          ->  Limit
  5 |             ->  Index Only Scan using minmaxtesti on minmaxtest
  6 |    ->  Aggregate  [1, InitPlan 2 (returns $1)]
  7 |       ->  Streaming (type: GATHER)
  8 |          ->  Limit
  9 |             ->  Index Only Scan Backward using minmaxtesti on minmaxtest
(9 rows)

         Predicate Information (identified by plan id)
----------------------------------------------------------------
   5 --Index Only Scan using minmaxtesti on minmaxtest
         Index Cond: (f1 IS NOT NULL)
   9 --Index Only Scan Backward using minmaxtesti on minmaxtest
         Index Cond: (f1 IS NOT NULL)
(4 rows)


--使用hint不使用minmax改写
gaussdb=# explain (costs off)select /*+ no use_minmax*/ min(f1), max(f1) from minmaxtest;
id |              operation
----+-------------------------------------
  1 | ->  Aggregate
  2 |    ->  Streaming (type: GATHER)
  3 |       ->  Aggregate
  4 |          ->  Seq Scan on minmaxtest
(4 rows)

可以看到使用no use_minmax hint后SQL语句不再使用minmax优化。

--使用minmax hint
analyze;  --收集统计信息
gaussdb=# explain (costs off) select min(f1), max(f1) from minmaxtest;
id |              operation
----+-------------------------------------
  1 | ->  Aggregate
  2 |    ->  Streaming (type: GATHER)
  3 |       ->  Aggregate
  4 |          ->  Seq Scan on minmaxtest
(4 rows)

--使用use_minmax hint 选择minmax改写
gaussdb=# explain (costs off) select  /*+ indexonlyscan(minmaxtest) use_minmax*/ min(f1), max(f1) from minmaxtest;
 id |                                operation
----+--------------------------------------------------------------------------
  1 | ->  Result
  2 |    ->  Aggregate  [1, InitPlan 1 (returns $0)]
  3 |       ->  Streaming (type: GATHER)
  4 |          ->  Limit
  5 |             ->  Index Only Scan using minmaxtesti on minmaxtest
  6 |    ->  Aggregate  [1, InitPlan 2 (returns $1)]
  7 |       ->  Streaming (type: GATHER)
  8 |          ->  Limit
  9 |             ->  Index Only Scan Backward using minmaxtesti on minmaxtest
(9 rows)

可以看到use_minmax hint成功生效。

use_minmax优化只有在表扫描使用indexscan的时候生效。

分享:

    相关文档

    相关产品