
# 指定是否使用minmax优化的hint
#### 功能描述
指定语句是否使用minmax改写。
#### 语法格式
```
[no] use_minmax[(@queryblock)]
```
#### 参数说明
- no表示不使用minmax查询改写。

- **@** queryblock请参见[指定Hint所处的查询块Queryblock](https://support.huaweicloud.com/distributed-devg-v8-gaussdb/gaussdb-12-0273.html#ZH-CN_TOPIC_0000002559105637)，可省略，表示在当前查询块生效。
- use_minmax使用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成功生效。
![](https://support.huaweicloud.com/distributed-devg-v8-gaussdb/public_sys-resources/note_3.0-zh-cn.png)
use_minmax优化只有在表扫描使用indexscan的时候生效。
