KingbaseES enable_hint_table 可以看成类似 oracle outline 工具,可以在不修改SQL 的情况下,通过hint 改变SQL 的执行计划。

一、启用enable_hint_table

1、设置shared_preload_libraries 参数,增加 sys_hint_plan , 重启数据库

2、修改参数 sys_hint_plan.enable_hint=on , select sys_reload_conf()

3、create extension sys_hint_plan ;创建后,会新建 hint_plan.hints 表。

4、设置 set sys_hint_plan.enable_hint_table = on

二、举例说明

1、没使用 hint_plan.hints 时的执行计划

test=# delete from  hint_plan.hints;
DELETE 1
test=# explain analyze select * from t1 where t1.id1 = 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on t1 (cost=4.20..13.67 rows=6 width=44) (actual time=0.002..0.002 rows=0 loops=1)
Recheck Cond: (id1 = 1)
-> Bitmap Index Scan on ind_t1 (cost=0.00..4.20 rows=6 width=0) (actual time=0.001..0.001 rows=0 loops=1)
Index Cond: (id1 = 1)
Planning Time: 0.067 ms
Execution Time: 0.011 ms
(6 rows)

默认情况下,SQL 走索引扫描。

2、使用 hint_plan.hints

通过在 hint_plan.hints 插入一条记录,提示使用 SeqScan。

test=# insert into hint_plan.hints(norm_query_string, application_name, hints) VALUES ( 'Explain analyze select * from t1 where t1.id1 = ?;','','SeqScan(t1)');
INSERT 0 1
test=# Explain analyze select * from t1 where t1.id1 = 1;
QUERY PLAN
----------------------------------------------------------------------------------------------
Seq Scan on t1 (cost=0.00..24.12 rows=6 width=44) (actual time=0.004..0.004 rows=0 loops=1)
Filter: (id1 = 1)
Planning Time: 0.169 ms
Execution Time: 0.017 ms
(4 rows)

这里有两个地方需要注意:

1、SQL 后面必须有个 “;” ,否则会因为SQL 不匹配而不生效

2、application_name='' ,表示任意应用,注意中间没有空格,否则会因为应用不匹配而不生效

三、SQL 匹配

1、空格不匹配,无法使用hint_table

test=# explain analyze select * from t1 where t1.id1 =1;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on t1 (cost=4.20..13.67 rows=6 width=44) (actual time=0.002..0.002 rows=0 loops=1)
Recheck Cond: (id1 = 1)
-> Bitmap Index Scan on ind_t1 (cost=0.00..4.20 rows=6 width=0) (actual time=0.001..0.001 rows=0 loops=1)
Index Cond: (id1 = 1)
Planning Time: 0.069 ms
Execution Time: 0.011 ms
(6 rows)

2、别名不匹配,无法使用hint_table

test=# explain analyze select * from t1 a where a.id1 = 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on t1 a (cost=4.20..13.67 rows=6 width=44) (actual time=0.003..0.003 rows=0 loops=1)
Recheck Cond: (id1 = 1)
-> Bitmap Index Scan on ind_t1 (cost=0.00..4.20 rows=6 width=0) (actual time=0.002..0.002 rows=0 loops=1)
Index Cond: (id1 = 1)
Planning Time: 0.053 ms
Execution Time: 0.012 ms
(6 rows)

3、大小写不匹配,可以使用hint_table

test=# explain analyze select * from t1 WHERE t1.id1 = 1;
QUERY PLAN
----------------------------------------------------------------------------------------------
Seq Scan on t1 (cost=0.00..24.12 rows=6 width=44) (actual time=0.002..0.002 rows=0 loops=1)
Filter: (id1 = 1)
Planning Time: 0.080 ms
Execution Time: 0.008 ms
(4 rows) test=# explain analyze select * from T1 where t1.id1 = 1;
QUERY PLAN
----------------------------------------------------------------------------------------------
Seq Scan on t1 (cost=0.00..24.12 rows=6 width=44) (actual time=0.002..0.002 rows=0 loops=1)
Filter: (id1 = 1)
Planning Time: 0.076 ms
Execution Time: 0.008 ms
(4 rows)

  

Enable_hint_table 使用的更多相关文章

随机推荐

  1. 揭开Vue异步组件的神秘面纱

    简介 在大型应用里,有些组件可能一开始并不显示,只有在特定条件下才会渲染,那么这种情况下该组件的资源其实不需要一开始就加载,完全可以在需要的时候再去请求,这也可以减少页面首次加载的资源体积,要在Vue ...

  2. Python自动化办公:将文本文档内容批量分类导入Excel表格

    序言 (https://jq.qq.com/?_wv=1027&k=GmeRhIX0) 它来了,它又来了. 本文实现用Python将文本文件自动保存到Excel表格里面去. 需求 将锦江区.t ...

  3. VMare 设置固定IP和网段

    切换目录 cd /etc/sysconfig/network-scripts ls查看当前目录下的东西 找到ipcfg- 开头的,而且不是iocfg-lo,而上图就是那个ifcfg-ens33. 则进 ...

  4. C#金额数字转换中文繁体

    /// <summary> /// 数字转换中文繁体金钱 /// </summary> /// <param name="Digital">&l ...

  5. Scala的基础用法 和 Java相对应学习(二)变量、循环、语法

    一.配置相关环境 1.增加项目 在idea里面创建新的maven项目 2. 在pom文件中增加依赖 <?xml version="1.0" encoding="UT ...

  6. django项目、vue项目部署云服务器

    目录 上线架构图 服务器购买与远程连接 安装git 安装mysql 安装redis(源码安装) 安装python3.8(源码安装) 安装uwsgi 安装虚拟环境 安装nginx(源码安装) vue项目 ...

  7. jdbc 05: 查询结果集

    jdbc连接mysql,查询结果集 package com.examples.jdbc.o5_结果集查询; import java.sql.*; import java.util.ResourceBu ...

  8. 如何使用API接口批量查询图书信息?

    之前小编讲过在Excel表格中根据ISBN查询图书信息可以使用我们的图书查询公式,但偶然间发现少部分书籍由于年份久远导致查不出来,今天小编就教给大家另一种查询图书信息的方式,即通过API接口返回的JS ...

  9. js屏蔽浏览器默认事件

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  10. 霜皮剥落紫龙鳞,下里巴人再谈数据库SQL优化,索引(一级/二级/聚簇/非聚簇)原理

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_206 举凡后端面试,面试官不言数据库则已,言则必称SQL优化,说起SQL优化,网络上各种"指南"和" ...