GaussDB-Custom Plan和Generic Plan选择的Hint

92 阅读1分钟

GaussDB-Custom Plan和Generic Plan选择的Hint

功能描述

对于以PBE方式执行的查询语句和DML语句,优化器会基于规则、代价、参数等因素选择生成Custom Plan或Generic Plan执行。用户可以通过use_cplan/use_gplan的hint指定使用哪种计划执行方式。

语法格式
  • 指定使用Custom Plan:

    | ``` use_cplan

    | ------------------ |
    
  • 指定使用Generic Plan:

    | ``` use_gplan

    | ------------------ |
    
    ![](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/7e1dc2882e9b424e99e356e87b29aca6~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAgamVycnl3YW5nMTk4Mw==:q75.awebp?rk3s=f64ab15b&x-expires=1782286945&x-signature=VodRBCe%2Fi3E8vNLwGFiR0iZ%2FA7w%3D)
    
    -   对于非PBE方式执行的SQL语句,设置本hint不会影响执行方式。
    -   本hint的优先级仅高于基于代价的选择和plan_cache_mode参数,即plan_cache_mode无法强制选择执行方式的语句本hint也无法生效。
    
示例

强制使用Custom Plan:

| ``` set enable_fast_query_shipping = off; create table t (a int, b int, c int); prepare p as select /*+ use_cplan */ * from t where a = $1; explain execute p(1);

| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

计划如下,可以看到过滤条件为入参的实际值,即此计划为Custom Plan。

![](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/a81de1b2f07a4f2bbf24702287d02671~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAgamVycnl3YW5nMTk4Mw==:q75.awebp?rk3s=f64ab15b&x-expires=1782286945&x-signature=26z7owp%2FXDJAJE01vSg%2FRbiHung%3D)

强制使用Generic Plan:

| ```
deallocate p; prepare p as select /*+ use_gplan */ * from t where a = $1; explain execute p(1); 
``` |
| -------------------------------------------------------------------------------------------------------- |

计划如下,可以看到过滤条件为待填充的入参,即此计划为Generic Plan。

![](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/5bb586f002224e8181b5d02b5c4706c2~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAgamVycnl3YW5nMTk4Mw==:q75.awebp?rk3s=f64ab15b&x-expires=1782286945&x-signature=hs5fvkL1qHEOrHEhN3aulWlBUjY%3D)

更多详情请参考GaussDB 文档中心:<https://doc.hcs.huawei.com/db/zh-cn/gaussdbqlh/24.1.30/productdesc/qlh_03_0001.html>