PostgreSQL 商用版本EPAS(阿里云ppas) 索引推荐功能使用

简介:

标签

PostgreSQL , PPAS , enterprisedb , 索引推荐


背景

PostgreSQL商用版本EnterpriseDB,内置了索引推荐功能,原理与这里描述类似。

《PostgreSQL SQL自动优化案例 - 极简,自动推荐索引》

目前仅支持BTREE索引的(单列或多列)推荐,(有一些限制,暂时不支持继承表的推荐,暂时不支持表达式索引的推荐)。

索引推荐用法

1、创建推荐存储表与推荐查阅函数。

-- 在需要用到索引推荐的数据库中,安装索引推荐用到的表、函数、视图   
-- 调用 index_advisor.sql    
psql -f $PGHOME/share/contrib/index_advisor.sql    
   
psql.bin:/opt/edb/as10/share/contrib/index_advisor.sql:22: ERROR:  relation "index_advisor_log" already exists   
psql.bin:/opt/edb/as10/share/contrib/index_advisor.sql:25: ERROR:  relation "ia_reloid" already exists   
psql.bin:/opt/edb/as10/share/contrib/index_advisor.sql:28: ERROR:  relation "ia_backend_pid" already exists   
CREATE FUNCTION   
CREATE FUNCTION   
CREATE VIEW   

2、加载推荐模块

-- 在需要用到索引推荐的会话中加载推荐模块   
load 'index_advisor';   

或者可以设置为自动加载。

vi $PGDATA/postgresql.conf   
   
shared_preload_libraries = 'index_advisor'   
   
   
# - Other Defaults -   
   
#dynamic_library_path = '$libdir'   
#local_preload_libraries = ''   
#session_preload_libraries = ''   
   
#oracle_home =''        # path to the Oracle home directory;   
                                        # only used by OCI Dblink; defaults   
                                        # to ORACLE_HOME environment variable.   

3、设置会话参数,选择是否需要推荐。如果需要推荐,则会自动对这个会话接下来的SQL进行索引推荐(如果多次执行,并被推荐的话,SQL都会被记录在表中)。

postgres=# set index_advisor.enabled TO on;   
SET   

设置后,这个会话的所有QUERY都会被推荐模块进行计算。并将需要推荐的SQL记录下来,包括PID,索引列,索引估算大小,成本估算等。

postgres=# select * from pgbench_history where bid=2;   
 tid  | bid |   aid   | delta |           mtime           | filler    
------+-----+---------+-------+---------------------------+--------   
  444 |   2 | 4438685 | -2029 | 12-JAN-18 20:46:43.07816  |    
   
postgres=# select pg_backend_pid();   
 pg_backend_pid    
----------------   
          32898   
(1 row)   
   
postgres=# select * from index_advisor_log ;   
 reloid |     relname      | attrs | benefit | index_size | backend_pid |            timestamp                
--------+------------------+-------+---------+------------+-------------+----------------------------------   
  16397 | pgbench_history  | {2}   |  175955 |    1260456 |       32898 | 14-JAN-18 21:32:19.564707 +08:00   

4、查阅推荐。

postgres=# select * from show_index_recommendations(32898);   
                                                      show_index_recommendations                                                         
--------------------------------------------------------------------------------------------------------------------------------------   
 create index idx_pgbench_history_bid on public.pgbench_history(bid);/* size: 1260456 KB, benefit: 175955, gain: 0.139596676718584 */   
(1 row)   
   
或   
   
postgres=# select * from show_index_recommendations();   
                                                      show_index_recommendations                                                         
--------------------------------------------------------------------------------------------------------------------------------------   
 create index idx_pgbench_history_bid on public.pgbench_history(bid);/* size: 1260456 KB, benefit: 175955, gain: 0.139596676718584 */   
(1 row)   

5、查看虚拟索引执行计划(HYPOTHETICAL PLAN 表示包含虚拟索引时的执行计划)。

postgres=# explain select * from pgbench_history where bid=2;   
                                             QUERY PLAN                                                 
-----------------------------------------------------------------------------------------------------   
 Gather  (cost=1000.00..455329.29 rows=470413 width=116)   
   Workers Planned: 6   
   ->  Parallel Seq Scan on pgbench_history  (cost=0.00..407287.99 rows=78402 width=116)   
         Filter: (bid = 2)   
 Result  (cost=0.00..0.00 rows=0 width=0)   
   One-Time Filter: '===[ HYPOTHETICAL PLAN ]==='::text   
   ->  Bitmap Heap Scan on pgbench_history  (cost=5339.17..279373.83 rows=470413 width=116)   
         Recheck Cond: (bid = 2)   
         ->  Bitmap Index Scan on "<hypothetical-index>:8"  (cost=0.00..5221.56 rows=470413 width=0)   
               Index Cond: (bid = 2)   
(10 rows)   

6、关闭推荐。

postgres=# set index_advisor.enabled TO off;   
SET   

关闭推荐后,不会使用虚拟索引,也不会再计算是否需要推荐索引。

postgres=# explain select * from pgbench_history where bid=2;   
                                       QUERY PLAN                                           
-----------------------------------------------------------------------------------------   
 Gather  (cost=1000.00..455329.29 rows=470413 width=116)   
   Workers Planned: 6   
   ->  Parallel Seq Scan on pgbench_history  (cost=0.00..407287.99 rows=78402 width=116)   
         Filter: (bid = 2)   
(4 rows)   

7、解读推荐索引信息。

postgres=# select * from show_index_recommendations();   
                                                      show_index_recommendations                                                         
--------------------------------------------------------------------------------------------------------------------------------------   
 create index idx_pgbench_history_bid on public.pgbench_history(bid);/* size: 1260456 KB, benefit: 175955, gain: 0.139596676718584 */   
(1 row)   

size,这个索引占用的空间,对应index_advisor_log.index_size。

benefit,这个推荐索引涉及的index_advisor_log.benefit,即加上索引后,相比没有索引时的explain成本估算COST差异。

gain,index_advisor_log -> sum(benefit)/greatest(size(s)),即多次推荐的统计,获得了多少提升空间,越大,表示提升效果越好。

如何让普通用户支持推荐

1、允许普通用户加载推荐模块

需要将$PGHOME/lib/index_advisor.so拷贝到$PGHOME/lib/plugins/index_advisor.so。普通用户才能LOAD。

2、允许普通用户增删改查推荐存储表

  • Grant SELECT and INSERT privileges on the index_advisor_log table to allow a user to invoke Index Advisor.

  • Grant DELETE privileges on the index_advisor_log table to allow the specified user to delete the table contents.

  • Grant SELECT privilege on the index_recommendations view.

推荐的配置

1、在template1模板库、以及已有的数据库(如果这些库需要使用索引推荐功能的话)中安装index_advisor.sql

psql template1 -f $PGHOME/share/contrib/index_advisor.sql    

psql template1 <<EOF
grant select,insert,delete on index_advisor_log to public;
grant select on index_recommendations to public;
EOF

2、配置postgresql.conf,默认加载模块

vi $PGDATA/postgresql.conf   
   
shared_preload_libraries = 'index_advisor'   

3、配置postgresql.conf,index_advisor.enabled=off,默认关闭计算

vi $PGDATA/postgresql.conf   
   
index_advisor.enabled=off   

4、当用户需要用到推荐模块时,只要打开index_advisor.enabled参数即可。

psql   
psql.bin (10.1.5)   
Type "help" for help.   
   
postgres=# show index_advisor.enabled ;   
 index_advisor.enabled    
-----------------------   
 off   
(1 row)   
   
postgres=# explain select * from pgbench_history where bid=2;   
                                       QUERY PLAN                                           
-----------------------------------------------------------------------------------------   
 Gather  (cost=1000.00..455329.29 rows=470413 width=116)   
   Workers Planned: 6   
   ->  Parallel Seq Scan on pgbench_history  (cost=0.00..407287.99 rows=78402 width=116)   
         Filter: (bid = 2)   
(4 rows)   
   
postgres=# set index_advisor.enabled =on;   
SET   
postgres=# explain select * from pgbench_history where bid=2;   
                                             QUERY PLAN                                                 
-----------------------------------------------------------------------------------------------------   
 Gather  (cost=1000.00..455329.29 rows=470413 width=116)   
   Workers Planned: 6   
   ->  Parallel Seq Scan on pgbench_history  (cost=0.00..407287.99 rows=78402 width=116)   
         Filter: (bid = 2)   
 Result  (cost=0.00..0.00 rows=0 width=0)   
   One-Time Filter: '===[ HYPOTHETICAL PLAN ]==='::text   
   ->  Bitmap Heap Scan on pgbench_history  (cost=5339.17..279373.83 rows=470413 width=116)   
         Recheck Cond: (bid = 2)   
         ->  Bitmap Index Scan on "<hypothetical-index>:2"  (cost=0.00..5221.56 rows=470413 width=0)   
               Index Cond: (bid = 2)   
(10 rows)   
   
postgres=# set index_advisor.enabled =off;   
SET   

参考

《PostgreSQL SQL自动优化案例 - 极简,自动推荐索引》

阿里云PPAS商用版本(兼容Oracle)

https://www.enterprisedb.com/docs/en/10.0/EPAS_Guide_v10/EDB_Postgres_Advanced_Server_Guide.1.36.html#pID0E0ZUE0HA

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
6月前
|
存储 监控 关系型数据库
B-tree不是万能药:PostgreSQL索引失效的7种高频场景与破解方案
在PostgreSQL优化实践中,B-tree索引虽承担了80%以上的查询加速任务,但因多种原因可能导致索引失效,引发性能骤降。本文深入剖析7种高频失效场景,包括隐式类型转换、函数包裹列、前导通配符等,并通过实战案例揭示问题本质,提供生产验证的解决方案。同时,总结索引使用决策矩阵与关键原则,助你让索引真正发挥作用。
432 0
|
3月前
|
存储 人工智能 关系型数据库
阿里云AnalyticDB for PostgreSQL 入选VLDB 2025:统一架构破局HTAP,Beam+Laser引擎赋能Data+AI融合新范式
在数据驱动与人工智能深度融合的时代,企业对数据仓库的需求早已超越“查得快”这一基础能力。面对传统数仓挑战,阿里云瑶池数据库AnalyticDB for PostgreSQL(简称ADB-PG)创新性地构建了统一架构下的Shared-Nothing与Shared-Storage双模融合体系,并自主研发Beam混合存储引擎与Laser向量化执行引擎,全面解决HTAP场景下性能、弹性、成本与实时性的矛盾。 近日,相关研究成果发表于在英国伦敦召开的数据库领域顶级会议 VLDB 2025,标志着中国自研云数仓技术再次登上国际舞台。
402 0
|
10月前
|
SQL 关系型数据库 OLAP
云原生数据仓库AnalyticDB PostgreSQL同一个SQL可以实现向量索引、全文索引GIN、普通索引BTREE混合查询,简化业务实现逻辑、提升查询性能
本文档介绍了如何在AnalyticDB for PostgreSQL中创建表、向量索引及混合检索的实现步骤。主要内容包括:创建`articles`表并设置向量存储格式,创建ANN向量索引,为表增加`username`和`time`列,建立BTREE索引和GIN全文检索索引,并展示了查询结果。参考文档提供了详细的SQL语句和配置说明。
335 2
|
11月前
|
JSON 关系型数据库 PostgreSQL
PostgreSQL 9种索引的原理和应用场景
PostgreSQL 支持九种主要索引类型,包括 B-Tree、Hash、GiST、SP-GiST、GIN、BRIN、Bitmap、Partial 和 Unique 索引。每种索引适用于不同场景,如 B-Tree 适合范围查询和排序,Hash 仅用于等值查询,GiST 支持全文搜索和几何数据查询,GIN 适用于多值列和 JSON 数据,BRIN 适合非常大的表,Bitmap 适用于低基数列,Partial 只对部分数据创建索引,Unique 确保列值唯一。
|
人工智能 自然语言处理 关系型数据库
阿里云云原生数据仓库 AnalyticDB PostgreSQL 版已完成和开源LLMOps平台Dify官方集成
近日,阿里云云原生数据仓库 AnalyticDB PostgreSQL 版已完成和开源LLMOps平台Dify官方集成。
|
关系型数据库 分布式数据库 数据库
开源云原生数据库PolarDB PostgreSQL 15兼容版本正式发布
PolarDB进行了深度的内核优化,从而实现以更低的成本提供商业数据库的性能。
|
SQL 关系型数据库 MySQL
SQL Server、MySQL、PostgreSQL:主流数据库SQL语法异同比较——深入探讨数据类型、分页查询、表创建与数据插入、函数和索引等关键语法差异,为跨数据库开发提供实用指导
【8月更文挑战第31天】SQL Server、MySQL和PostgreSQL是当今最流行的关系型数据库管理系统,均使用SQL作为查询语言,但在语法和功能实现上存在差异。本文将比较它们在数据类型、分页查询、创建和插入数据以及函数和索引等方面的异同,帮助开发者更好地理解和使用这些数据库。尽管它们共用SQL语言,但每个系统都有独特的语法规则,了解这些差异有助于提升开发效率和项目成功率。
1628 0
|
3月前
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
165 3

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版
  • 推荐镜像

    更多