【我和 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 nullempname | text | not nulldeptid | integer | not nullsalary | integer | not nullIndexes:"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.pycat yangkai.sqlselect 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_modePassword 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!
复制代码
评论