当前位置:网站首页>89-oracle SQL写法与优化器缺陷一例
89-oracle SQL写法与优化器缺陷一例
2022-06-22 19:10:00 【老虎刘】
今天有个学员在微信群里问了一个问题 : 书上说exists 子查询带union只能使用filter的执行计划, 但是他实验模拟得到的执行计划却是hash semi join,为什么?
下面是书上写的(图1):
下面是学员实验得出的(图2):
请注意我在图1红框中标注的两个emp.的位置, 与图2中E.deptno位置的区别.
(与是否使用别名无关)
为什么书上的写法与实验结果对不上呢? 对此我做了分析与模拟, 得到了下面的结论:
如果把图2 SQL其中一个E.deptno放到=的后面(即E.DEPTNO=D.DEPTNO改成D.DEPTNO=E.DEPTNO), 那么也只能得到图1的执行计划. 加hint也不行(至少我没有尝试成功), 19c版本也是一样的.
但是如果union各部分关联条件的写法顺序是一致的, 比如都是E.DEPTNO在前或都是E.DEPTNO在后, 还是可以得出不使用filter的执行计划(unnest的效果).
一个简单的等值关联条件的前后顺序调换, 就对执行计划产生如此严重的影响,说明oracle的优化器还是有一定的不足. 另外, 书上如果用的是图2写法的SQL, 也不会得出exists+union只能使用filter执行计划的片面说法.
另外, SQL的写法也很重要, 这种exists 子查询带union/union all的写法, 一般都会使用in的写法, 这样就不会出现上面的情况了.
大家有时间可以自己动手, 比较一下下面两个sql的执行计划:
--只能使用filter
select ename, deptno
from emp
where exists
(
select deptno from dept where dname = 'CHICAGO' and emp.deptno =dept.deptno
union
select deptno from dept where loc = 'CHICAGO' and dept.deptno = emp.deptno
);
--可以不使用filter:
select ename, deptno
from emp e
where exists
(
select deptno from dept d where d.dname = 'CHICAGO' and e.deptno =d.deptno
union
select deptno from dept d where d.loc = 'CHICAGO' and e.deptno = d.deptno
);
边栏推荐
- An error is reported when idea writes JSP code, but it is solved by normal operation
- AAAI 2022 | 传统GAN修改后可解释,并保证卷积核可解释性和生成图像真实性
- Random talk about redis source code onehundredandtwenty
- R语言数据预处理、把类型变量转化为因子变量,把数据集转化为h2o格式、数据集划分(训练集、测试集、验证集)
- 6月第3周B站榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!
- NFT 中可能存在的安全漏洞
- AAAI 2022 | traditional Gan can be interpreted after modification, and the interpretability of convolution kernel and the authenticity of generated image are guaranteed
- 深度学习常用损失函数总览:基本形式、原理、特点
- ROS从入门到精通(八) 常用传感器与消息数据
- Introduction to JWT
猜你喜欢
随机推荐
Precautions for Apollo use
An IPFs enabled email - skiff
[proteus simulation] 8x8LED dot matrix digital cyclic display
IVX no code challenge five second game production
A detailed solution to mysql8.0 forgetting password
Random talk about redis source code 122
How to consider the arrangement of complete knapsack
[deeply understand tcapulusdb technology] tcapulusdb process
Gossip about redis source code 121
MySQL Basics - functions
CVPR 2022 Oral | 视频文本预训练新SOTA,港大、腾讯ARC Lab推出基于多项选择题的借口任务
[in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance
Cross domain cors/options
[deeply understand tcapulusdb technology] tcapulusdb table management - delete table
智能計算之神經網絡(BP)介紹
数字化转型的失败原因及成功之道
软件压力测试有哪些方法,如何选择软件压力测试机构?
Unityeditor editor script execution menu
Gradle Build Cache引发的Task缓存编译问题
LORA技术---LoRa信号从数据流变为LoRa扩频信号,再从射频信号通过解调变为数据







