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

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

标签

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数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
18天前
|
关系型数据库 MySQL 数据库
mysql卸载、下载、安装(window版本)
mysql卸载、下载、安装(window版本)
|
1月前
|
关系型数据库 分布式数据库 数据库
PolarDB常见问题之加了索引但是查询没有使用如何解决
PolarDB是阿里云推出的下一代关系型数据库,具有高性能、高可用性和弹性伸缩能力,适用于大规模数据处理场景。本汇总囊括了PolarDB使用中用户可能遭遇的一系列常见问题及解答,旨在为数据库管理员和开发者提供全面的问题指导,确保数据库平稳运行和优化使用体验。
|
1月前
|
关系型数据库 MySQL 数据库
django4版本提示 django.db.utils.NotSupportedError: MySQL 8 or later is required (found 5.7.26)
在学习Django时,用户遇到`django.db.utils.NotSupportedError`,提示需要MySQL 8.0.25或更高版本,但其系统上是5.7.26。为解决这个问题,用户决定不升级MySQL,而是选择注释掉Django源码中的数据库版本检查。通过Python命令行找到Django安装路径,进入`db/backends/base/base.py`,注释掉`self.check_database_version_supported()`函数
127 0
|
28天前
|
SQL 关系型数据库 MySQL
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(8.0版本升级篇)
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(8.0版本升级篇)
96 0
|
1月前
|
算法 Java 开发工具
使用阿里云KMS产品针对 Springboot 接口参数加密解密功能
针对Springboot里面使用开源工具使用加解密,替换成阿里云KMS产品进行加解密;
147 1
|
2月前
|
文字识别 安全 API
阿里云文字识别OCR的发票凭证识别功能可以通过API接口的形式进行调用
【2月更文挑战第5天】阿里云文字识别OCR的发票凭证识别功能可以通过API接口的形式进行调用
89 5
|
2月前
|
弹性计算 Ubuntu Linux
阿里云助力《幻兽帕鲁》快速开服:全新自动部署功能上线,仅需10秒即可完成部署!
阿里云助力《幻兽帕鲁》快速开服:全新自动部署功能上线,仅需10秒即可完成部署!阿里云服务器搭建帕鲁服务器游戏,服务器稳定无卡顿,先下载SteamCMD,并运行;然后下载Palserver,修改服务ini配置,启动PalServer,进入游戏服务器。今天分享阿里云创建幻兽帕鲁服务器教程。
|
2月前
|
弹性计算 搜索推荐 Linux
阿里云自动部署【幻兽帕鲁/Palworld】功能上线,10秒全自动开服!
阿里云轻量应用主机Lighthouse上线幻兽帕鲁应用模板功能 ,用户只需要点三次鼠标,最快10秒就能自动安装好幻兽帕鲁主机,和自己的朋友一起开心“抓帕鲁”。同时,用户还可以在阿里云控制台的面板直接调整幻兽帕鲁的经验倍率、死亡惩罚等游戏配置,实现便捷、个性化的游戏体验。
阿里云自动部署【幻兽帕鲁/Palworld】功能上线,10秒全自动开服!
|
2月前
|
关系型数据库 MySQL 数据安全/隐私保护
【极光系列】Windows安装Mysql8.0版本
【极光系列】Windows安装Mysql8.0版本
|
2月前
|
API 网络安全
阿里云视觉智能平台中人脸搜索功能在长时间不使用后第一次使用时出现连接失败
阿里云视觉智能平台中人脸搜索功能在长时间不使用后第一次使用时出现连接失败
230 2

相关产品

  • 云原生数据库 PolarDB