【我和openGauss的故事】openGauss价值特性 (二)
  lYE0sTgD5uUi 2023年11月02日 25 0

【我和openGauss的故事】openGauss价值特性 (二)

索引推荐(Index-Advisor)

Index-Advisor共包含三个子功能,单查询索引推荐,虚拟索引和负载级别索引推荐。

单查询索引推荐

功能支持用户在数据库中直接进行操作,本功能基于查询语句的语义信息和数据库的统计信息,对用户输入的单条查询语句生成推荐的索引。本功能涉及的函数接口如下。

gs_index_advise() 功能:只针对单条的SQL语句

环境如下:

openGauss=# select count(*) from employee;
count
---------
1000000
(1 row)

-----------------------------------------------------------------------------

openGauss=# \d employee;
Table "public.employee"
Column | Type | Modifiers
---------+---------+-----------
empid | integer | not null
empname | text | not null
deptid | integer | not null
salary | integer | not null
Indexes:
"idx_empdepid" btree (deptid) TABLESPACE pg_default

openGauss=# ANALYZE employee;
ANALYZE

下面开始测试:

openGauss=# SELECT * FROM gs_index_advise('select * from employee where empid<1');
schema | table | column | indextype
--------+----------+--------+-----------
public | employee | empid |
(1 row)

openGauss=# SELECT * FROM gs_index_advise('select * from employee where empid>1');
schema | table | column | indextype
--------+----------+--------+-----------
public | employee | |
(1 row)

上述结果表明:当empid <1的时候,应在employee 表上的empid 列上创建索引。说明这个功能相当准确适用。

虚拟索引

虚拟索引功能支持用户在数据库中直接进行操作,本功能将模拟真实索引的建立,避免真实索引创建所需的时间和空间开销,用户基于虚拟索引,可通过优化器评估该索引对指定查询语句的代价影响。

命令:hypopg_create_index

下面开始测试:

openGauss=# select * from hypopg_create_index('create index on employee(empid)');
indexrelid | indexname
------------+-----------------------------
32768 | <32768>btree_employee_empid
(1 row)

openGauss=# explain select * from employee where empid=10;
QUERY PLAN
-------------------------------------------------------------
Seq Scan on employee (cost=0.00..14973.40 rows=1 width=16)
Filter: (empid = 10)
(2 rows)

开启GUC参数后:

enable_hypo_index:该参数控制数据库的优化器进行EXPLAIN时是否考虑创建的虚拟索引。通过对特定的查询语句执行explain,用户可根据优化器给出的执行计划评估该索引是否能够提升该查询语句的执行效率。

openGauss=# set enable_hypo_index=on;
SETopenGauss=# explain select * from employee where empid=10;

QUERY PLAN
---------------------------------------------------------------------------------------------
Index Scan using <32768>btree_employee_empid on employee (cost=0.00..8.27 rows=1 width=16)
Index Cond: (empid = 10)
(2 rows)

结论:通过对比二个执行计划,用户可以考虑是否需要创建对应的真实索引。

负载级别索引

对于workload级别的索引推荐,用户可通过运行数据库外的脚本使用此功能,本功能将包含有多条DML语句的workload作为输入,最终生成一批可对整体workload的执行表现进行优化的索引。同时,本功能提供从日志中抽取业务数据SQL流水的功能。

查询脚本位置:

[omm@huaweidb index_advisor]$ find /opt/huawei -name index_advisor_workload.py
/opt/huawei/install/app_02c14696/bin/dbmind/components/index_advisor/index_advisor_workload.py
cat yangkai.sql
select distinct deptname from emp_order_insurance,department where department.deptid in(select deptid from employee where employee.empid=emp_order_insurance.empid);
[omm@huaweidb index_advisor]$ python3 index_advisor_workload.py 15400 postgres yangkai.sql --schema public --max_index_num 10 --multi_iter_mode
Password for database user:
######################################################################################################################## Generate candidate indexes ########################################################################################################################
No candidate indexes generated!
############################################################################################################################## Created indexes ##############################################################################################################################
public: CREATE UNIQUE INDEX pk_department ON department USING btree (deptid) TABLESPACE pg_default;
public: CREATE INDEX idx_empdepid ON employee USING btree (deptid) TABLESPACE pg_default;
public: CREATE UNIQUE INDEX employee_pkey ON employee USING btree (empid) TABLESPACE pg_default;
public: CREATE UNIQUE INDEX pk_insurance ON insurance USING btree (insuranceid) TABLESPACE pg_default;
##################################################################################################################### Current workload useless indexes #####################################################################################################################
DROP INDEX idx_empdepid;
############################################################################################################################# Redundant indexes #############################################################################################################################
No redundant index!
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  LxKByvFwtHdi   2023年11月02日   50   0   0 xmlTomcat数据库
lYE0sTgD5uUi