PostgreSQL 商用版本EPAS(阿里云ppas) 自定义custom plan次数

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

标签

PostgreSQL , 绑定变量 , custom plan , generic plan


背景

PostgreSQL 数据库与商用数据库Oracle一样,支持绑定变量,绑定变量的目的是降低SQL硬解析的性能开销。

使用绑定变量后,执行变成这样的流程,一次prepare,后面都只需要bind和execute:

prepare      
      
bind      
      
execute      

同时,prepare statement也降低了优化器的plan开销,流程这样:

前5次执行时,采样 custom plan(每次都需要根据SQL输入的变量,规划最佳plan tree),并增加custom plan次数计数,平均COST计数。

第六次开始,就会生成generic plan,也就是说后面不再主动重新规划最佳plan tree,而是使用plan cache,将变量代入plan cache,直接通过plan cache产生这次bind 变量计算得到的COST,这个COST与custom plan的平均COST对比,如果COST小于或等于custom plan COST的1.1倍,则使用这个plan tree执行。否则才会主动进行custom plan,并继续累加custom plan次数计数,平均COST计数。

原理详见:

《执行计划选择算法 与 绑定变量 - PostgreSQL prepared statement: SPI_prepare, prepare|execute COMMAND, PL/pgsql STYLE: custom & generic plan cache》

但是到底执行几次custom plan更好,这个不同的数据或和用户前几次调用有关,浙江直接导致后面的generic plan的选择和效率。

默认PostgreSQL是硬编码,5次,PostgreSQL 商用版本EPAS(阿里云ppas)允许用户自定义custom plan的次数。

PostgreSQL 商用版本EPAS(阿里云ppas),用户自定义custom plan的次数

edb_custom_plan_tries

你可以在会话层面设置edb_custom_plan_tries 参数,也可以在用户、库、实例 层面设置。

Parameter Type: Numeric

Default Value: 5

Range: {0 | 100}

Minimum Scope of Effect: Per session

When Value Changes Take Effect: Immediate

Required Authorization to Activate: Session User

This configuration parameter controls the number of custom execution plans considered by the planner before the planner settles on a generic execution plan.

When a client application repeatedly executes a prepared statement, the server may decide to evaluate several execution plans before deciding to choose a custom plan or a generic plan.

  • A custom plan is a plan built for a specific set of parameter values.

  • A generic plan is a plan that will work with any set of parameter values supplied by the client application.

By default, the optimizer will generate five custom plans before evaluating a generic plan. That means that if you execute a prepared statement six times, the optimizer will generate five custom plans, then one generic plan, and then decide whether to stick with the generic plan.

In certain workloads, this extra planning can have a negative impact on performance. You can adjust the edb_custom_plan_tries configuration parameter to decrease the number of custom plans considered before evaluating a generic plan. Setting edb_custom_plan_tries to 0 will effectively disable custom plan generation.

Consider the following query:

PREPARE custQuery AS SELECT * FROM customer WHERE salesman >= $1

The $1 token in this query is a parameter marker - the client application must provide a value for each parameter marker each time the statement executes.

If an index has been defined on customer.salesman, the optimizer may choose to execute this query using a sequential scan, or using an index scan. In some cases, an index is faster than a sequential scan; in other cases, the sequential scan will win. The optimal plan will depend on the distribution of salesman values in the table and on the search value (the value provided for the $1 parameter).

When the client application repeatedly executes the custQuery prepared statement, the optimizer will generate some number of parameter-value-specific execution plans (custom plans), followed by a generic plan (a plan that ignores the parameter values), and then decide whether to stick with the generic plan or to continue to generate custom plans for each execution. The decision process takes into account not only the cost of executing the plans, but the cost of generating custom plans as well.

参考

《执行计划选择算法 与 绑定变量 - PostgreSQL prepared statement: SPI_prepare, prepare|execute COMMAND, PL/pgsql STYLE: custom & generic plan cache》

《PostgreSQL 函数调试、诊断、优化 & auto_explain》

https://www.enterprisedb.com/docs/en/10.0/EPAS_Guide_v10/EDB_Postgres_Advanced_Server_Guide.1.27.html#pID0E0GNF0HA

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
15天前
|
关系型数据库 MySQL 数据库
mysql卸载、下载、安装(window版本)
mysql卸载、下载、安装(window版本)
|
1月前
|
缓存 安全 Java
阿里云数据库 SelectDB 内核 Apache Doris 2.0.6 版本正式发布
阿里云数据库 SelectDB 内核 Apache Doris 2.0.6 版本正式发布
|
1月前
|
SQL 存储 JSON
阿里云数据库 SelectDB 内核 Apache Doris 2.1.0 版本发布:开箱盲测性能大幅优化,复杂查询性能提升 100%
亲爱的社区小伙伴们,Apache Doris 2.1.0 版本已于 2024 年 3 月 8 日正式发布,新版本开箱盲测性能大幅优化,在复杂查询性能方面提升100%,新增Arrow Flight接口加速数据读取千倍,支持半结构化数据类型与分析函数。异步多表物化视图优化查询并助力仓库分层建模。引入自增列、自动分区等存储优化,提升实时写入效率。Workload Group 资源隔离强化及运行时监控功能升级,保障多负载场景下的稳定性。新版本已经上线,欢迎大家下载使用!
阿里云数据库 SelectDB 内核 Apache Doris 2.1.0 版本发布:开箱盲测性能大幅优化,复杂查询性能提升 100%
|
2月前
|
存储 监控 安全
阿里云网盘与相册问题之如何开通历史版本
阿里云网盘与相册是阿里云提供的云存储服务,用户可以安全便捷地存储和管理个人文件、照片等数据;本合集将介绍如何使用阿里云网盘和相册服务,包括文件上传、同步、分享,以及处理常见使用问题的技巧。
37 1
|
2月前
|
存储 安全 网络协议
阿里云网盘与相册问题之服务开通历史版本如何解决
阿里云网盘与相册是阿里云提供的云存储服务,用户可以安全便捷地存储和管理个人文件、照片等数据;本合集将介绍如何使用阿里云网盘和相册服务,包括文件上传、同步、分享,以及处理常见使用问题的技巧。
41 1
|
2月前
|
搜索推荐 机器人 开发工具
5 天学会阿里云 RPA:自定义组件
机器人流程自动化(RPA)技术在各个行业都取得了显著的成就,阿里云 RPA 作为其中的佼佼者,为用户提供了一种高效、灵活的自动化解决方案。除了内置的组件和功能,阿里云 RPA 还支持自定义组件的开发,使得用户能够根据自己的需求和业务流程进行个性化的定制。
|
3月前
|
JSON 安全 Serverless
在使用阿里云函数计算(FC)服务时,您可以通过自定义域名来访问部署好的云函数
在使用阿里云函数计算(FC)服务时,您可以通过自定义域名来访问部署好的云函数【1月更文挑战第23天】【1月更文挑战第112篇】
222 7
|
3月前
|
关系型数据库 MySQL
若依框架----如何降低mysql驱动版本?
若依框架----如何降低mysql驱动版本?
58 3
|
3月前
|
运维 Linux Windows
【帕鲁更新攻略】如何快速在阿里云上更新游戏服务端版本
幻兽帕鲁游戏服务端版本升级后,需要玩家同步更新才能继续游戏,您可以按照以下操作完成游戏服务端升级。
46218 840
【帕鲁更新攻略】如何快速在阿里云上更新游戏服务端版本
|
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()`函数
110 0

相关产品

  • 云原生数据库 PolarDB