关于优化sql的疑惑

suixingliu suixingliu 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
SQL> select * from scott.emp e1 where e1.sal>(select avg(sal) from scott.emp e2 where e1.deptno=e2.deptno);

已用时间: 00: 00: 00.00

执行计划
----------------------------------------------------------
Plan hash value: 2649664444

----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 348 | 6 (0)| 00:00:01 |
|* 1 | FILTER | | | | | |
| 2 | TABLE ACCESS FULL | EMP | 15 | 1305 | 3 (0)| 00:00:01 |
| 3 | SORT AGGREGATE | | 1 | 26 | | |
|* 4 | TABLE ACCESS FULL| EMP | 1 | 26 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

1 - filter("E1"."SAL"> (SELECT AVG("SAL") FROM "SCOTT"."EMP" "E2"
WHERE "E2"."DEPTNO"=:B1))
4 - filter("E2"."DEPTNO"=:B1)

Note
-----
- dynamic sampling used for this statement


统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
28 consistent gets

0 physical reads
0 redo size
992 bytes sent via SQL*Net to client
415 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4 rows processed

SQL> select * from scott.emp e1,(select e2.deptno deptno,avg(e2.sal) avg_sal from scott.emp e2 group by deptno) dept2 where e1.deptno=dept2.deptno and e1.sal>dept2.avg_sal;

已用时间: 00: 00: 00.00

执行计划
----------------------------------------------------------
Plan hash value: 2230095667

-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 452 | 8 (25)| 00:00:01 |
|* 1 | HASH JOIN | | 4 | 452 | 8 (25)| 00:00:01 |
| 2 | TABLE ACCESS FULL | EMP | 15 | 1305 | 3 (0)| 00:00:01 |
| 3 | VIEW | | 15 | 390 | 4 (25)| 00:00:01 |
| 4 | HASH GROUP BY | | 15 | 390 | 4 (25)| 00:00:01 |
| 5 | TABLE ACCESS FULL| EMP | 15 | 390 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

1 - access("E1"."DEPTNO"="DEPT2"."DEPTNO")
filter("E1"."SAL">"DEPT2"."AVG_SAL")

Note
-----
- dynamic sampling used for this statement


统计信息
----------------------------------------------------------
350 recursive calls
2 db block gets
125 consistent gets

0 physical reads
0 redo size
1184 bytes sent via SQL*Net to client
415 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
12 sorts (memory)
0 sorts (disk)
4 rows processed

理论上应该是第一种方法效率低才对,为什么我的结果是这样?请高手指点。
...全文

相关推荐

评论 19
收藏 已收藏
分享
返回顶部
suixingliu
Oracle 17,133 成员 55,258 内容