【我和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!
文章详细介绍了openGauss数据库中的索引优化工具,包括单查询索引推荐功能,使用gs_index_advise()函数为特定查询建议索引;虚拟索引功能,通过hypopg_create_index命令模拟索引以评估性能;以及负载级别索引推荐,为工作负载生成优化索引。这些工具帮助用户提高查询效率并优化数据库性能。

被折叠的 条评论
为什么被折叠?



