从两表连接看Oracle sql优化器的效果

复制代码
select emp.*,dept.* from tb_emp03 emp,tb_dept03 dept where emp.deptno=dept.id 

-- 不加hint SQL
> select emp.*,dept.* from tb_emp03 emp,tb_dept03 dept where emp.deptno=dept.id ; 已用时间: 00: 00: 00.00 执行计划 ---------------------------------------------------------- Plan hash value: 2666916627 -------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1818K| 97M| 3619 (2)| 00:00:44 | |* 1 | HASH JOIN | | 1818K| 97M| 3619 (2)| 00:00:44 | | 2 | TABLE ACCESS FULL| TB_DEPT03 | 10 | 160 | 3 (0)| 00:00:01 | | 3 | TABLE ACCESS FULL| TB_EMP03 | 2000K| 76M| 3601 (1)| 00:00:44 | -------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("EMP"."DEPTNO"="DEPT"."ID") select /*+ leading(emp,dept) use_nl(emp) */emp.*,dept.* from tb_emp03 emp,tb_dept03 dept where emp.deptno=dept.id; SQL> select /*+ leading(emp,dept) use_nl(emp) */emp.*,dept.* from tb_emp03 emp,tb_dept03 dept where emp.deptno=dept.id; 已用时间: 00: 00: 00.00 执行计划 ---------------------------------------------------------- Plan hash value: 960397288 ---------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | ---------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1818K| 97M| | 8558 (1)| 00:01:43 | |* 1 | HASH JOIN | | 1818K| 97M| 99M| 8558 (1)| 00:01:43 | | 2 | TABLE ACCESS FULL| TB_EMP03 | 2000K| 76M| | 3601 (1)| 00:00:44 | | 3 | TABLE ACCESS FULL| TB_DEPT03 | 10 | 160 | | 3 (0)| 00:00:01 | ---------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("EMP"."DEPTNO"="DEPT"."ID") select /*+ leading(dept,emp) use_nldept) */emp.*,dept.* from tb_emp03 emp,tb_dept03 dept where emp.deptno=dept.id; SQL> select /*+ leading(dept,emp) use_nldept) */emp.*,dept.* from tb_emp03 emp,tb_dept03 dept where emp.deptno=dept.id; 已用时间: 00: 00: 00.00 执行计划 ---------------------------------------------------------- Plan hash value: 2666916627 -------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1818K| 97M| 3619 (2)| 00:00:44 | |* 1 | HASH JOIN | | 1818K| 97M| 3619 (2)| 00:00:44 | | 2 | TABLE ACCESS FULL| TB_DEPT03 | 10 | 160 | 3 (0)| 00:00:01 | | 3 | TABLE ACCESS FULL| TB_EMP03 | 2000K| 76M| 3601 (1)| 00:00:44 | -------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("EMP"."DEPTNO"="DEPT"."ID") select /*+ ordered */emp.*,dept.* from tb_emp03 emp,tb_dept03 dept where emp.deptno=dept.id; SQL> select /*+ ordered */emp.*,dept.* from tb_emp03 emp,tb_dept03 dept where emp.deptno=dept.id; 已用时间: 00: 00: 00.00 执行计划 ---------------------------------------------------------- Plan hash value: 960397288 ---------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | ---------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1818K| 97M| | 8558 (1)| 00:01:43 | |* 1 | HASH JOIN | | 1818K| 97M| 99M| 8558 (1)| 00:01:43 | | 2 | TABLE ACCESS FULL| TB_EMP03 | 2000K| 76M| | 3601 (1)| 00:00:44 | | 3 | TABLE ACCESS FULL| TB_DEPT03 | 10 | 160 | | 3 (0)| 00:00:01 | ---------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("EMP"."DEPTNO"="DEPT"."ID") select /*+ use_hash(dept,emp) */emp.*,dept.* from tb_emp03 emp,tb_dept03 dept where emp.deptno=dept.id; SQL> select /*+ use_hash(dept,emp) */emp.*,dept.* from tb_emp03 emp,tb_dept03 dept where emp.deptno=dept.id; 已用时间: 00: 00: 00.00 执行计划 ---------------------------------------------------------- Plan hash value: 2666916627 -------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1818K| 97M| 3619 (2)| 00:00:44 | |* 1 | HASH JOIN | | 1818K| 97M| 3619 (2)| 00:00:44 | | 2 | TABLE ACCESS FULL| TB_DEPT03 | 10 | 160 | 3 (0)| 00:00:01 | | 3 | TABLE ACCESS FULL| TB_EMP03 | 2000K| 76M| 3601 (1)| 00:00:44 | -------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("EMP"."DEPTNO"="DEPT"."ID") select /*+ use_hash(emp,dept) */emp.*,dept.* from tb_emp03 emp,tb_dept03 dept where emp.deptno=dept.id; SQL> select /*+ use_hash(emp,dept) */emp.*,dept.* from tb_emp03 emp,tb_dept03 dept where emp.deptno=dept.id; 已用时间: 00: 00: 00.00 执行计划 ---------------------------------------------------------- Plan hash value: 2666916627 -------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1818K| 97M| 3619 (2)| 00:00:44 | |* 1 | HASH JOIN | | 1818K| 97M| 3619 (2)| 00:00:44 | | 2 | TABLE ACCESS FULL| TB_DEPT03 | 10 | 160 | 3 (0)| 00:00:01 | | 3 | TABLE ACCESS FULL| TB_EMP03 | 2000K| 76M| 3601 (1)| 00:00:44 | -------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("EMP"."DEPTNO"="DEPT"."ID")
复制代码

 

posted @   逆火狂飙  阅读(181)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示