在PostgreSQL中,如何模拟Oracle的hint效果

本文涉及的产品
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
云原生数据库 PolarDB MySQL 版,通用型 2核8GB 50GB
简介:

Oracle 的SQL文,可以强制指定各种 hint。

但是在PostgreSQL中是不支持的。

其wiki 是这样说的:

http://wiki.postgresql.org/wiki/OptimizerHintsDiscussion

We are not interested in implementing hints in the exact ways they are commonly implemented on other databases. Proposals based on "because they've got them" will not be welcomed. If you have an idea that avoids the problems that have been observed with other hint systems, that could lead to valuable discussion.

但是可以通过如下的postgresql.conf参数来调节:

复制代码
# - Planner Method Configuration -

#enable_bitmapscan = on
#enable_hashagg = on
enable_hashjoin = on
#enable_indexscan = on
#enable_indexonlyscan = on
#enable_material = on
enable_mergejoin = on
enable_nestloop = on
#enable_seqscan = on
#enable_sort = on
#enable_tidscan = on
复制代码

对于我的查询:

explain select  dept.no_emps,emp.age from dept,emp where emp.name = dept.mgr and dept.dept_name = 'shoe';

如果 enable_hashjoin = on,其他也为on,则执行计划是:

复制代码
postgres=# explain select dept.no_emps,emp.age from dept,emp where emp.name = dept.mgr and dept.dept_name = 'shoe';
                            QUERY PLAN                            
------------------------------------------------------------------
 Hash Join  (cost=19.30..45.07 rows=23 width=8)
   Hash Cond: ((emp.name)::text = (dept.mgr)::text)
   ->  Seq Scan on emp  (cost=0.00..21.30 rows=1130 width=42)
   ->  Hash  (cost=19.25..19.25 rows=4 width=42)
         ->  Seq Scan on dept  (cost=0.00..19.25 rows=4 width=42)
               Filter: ((dept_name)::text = 'shoe'::text)
(6 rows)

postgres=#
复制代码

如果 enable_hashjoin=off,其他为on,则执行计划是:

复制代码
postgres=# explain select dept.no_emps,emp.age from dept,emp where emp.name = dept.mgr and dept.dept_name = 'shoe';
                             QUERY PLAN                             
--------------------------------------------------------------------
 Merge Join  (cost=97.89..103.79 rows=23 width=8)
   Merge Cond: ((dept.mgr)::text = (emp.name)::text)
   ->  Sort  (cost=19.29..19.30 rows=4 width=42)
         Sort Key: dept.mgr
         ->  Seq Scan on dept  (cost=0.00..19.25 rows=4 width=42)
               Filter: ((dept_name)::text = 'shoe'::text)
   ->  Sort  (cost=78.60..81.43 rows=1130 width=42)
         Sort Key: emp.name
         ->  Seq Scan on emp  (cost=0.00..21.30 rows=1130 width=42)
(9 rows)

postgres=#
复制代码

如果enable_hashjoin 为 off,而 enable_mergejoin也为 off,则执行计划为:

复制代码
postgres=# explain select dept.no_emps,emp.age from dept,emp where emp.name = dept.mgr and dept.dept_name = 'shoe';
                            QUERY PLAN                            
------------------------------------------------------------------
 Nested Loop  (cost=0.00..108.36 rows=23 width=8)
   Join Filter: ((dept.mgr)::text = (emp.name)::text)
   ->  Seq Scan on emp  (cost=0.00..21.30 rows=1130 width=42)
   ->  Materialize  (cost=0.00..19.27 rows=4 width=42)
         ->  Seq Scan on dept  (cost=0.00..19.25 rows=4 width=42)
               Filter: ((dept_name)::text = 'shoe'::text)
(6 rows)

postgres=#
复制代码
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
打赏
0
0
0
0
60
分享
相关文章
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
1402 2
常用数据库的分页语句(mySQL、oracle、PostgreSQL、SQL Server)
常用数据库的分页语句(mySQL、oracle、PostgreSQL、SQL Server)
PolarDB PostgreSQL版:Oracle兼容的高性能数据库
PolarDB PostgreSQL版是一款高性能的数据库,具有与Oracle兼容的特性。它采用了分布式架构,可以轻松处理大量的数据,同时还支持多种数据类型和函数,具有高可用性和可扩展性。它还提供了丰富的管理工具和性能优化功能,为企业提供了可靠的数据存储和处理解决方案。PolarDB PostgreSQL版在数据库领域具有很高的竞争力,可以满足各种企业的需求。
Polar DB-O (兼容 Oracle 语法版本)和Polar DB PostgreSQL 版本概述(二)
Polar DB-O (兼容 Oracle 语法版本)和Polar DB PostgreSQL 版本概述(二)
2349 0
PostgreSQL和Oracle两种数据库有啥区别?如何选择?
PostgreSQL和Oracle两种数据库有啥区别?如何选择?
612 0

推荐镜像

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等