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 processedSQL> 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理论上应该是第一种方法效率低才对,为什么我的结果是这样?请高手指点。
----------------------------------------------------------
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 processedSQL> 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理论上应该是第一种方法效率低才对,为什么我的结果是这样?请高手指点。
解决方案 »
- 帮看看这条语句为什么不行
- sql转成oracl,.net程序员可看
- 求助,关于Oracle 11g 存储过程,调用ftp.put上传文件
- 问一个SQL语句的问题
- oracle10g ome无法通过网页启动
- ora-12170 pl/sql链接错误
- 为什么不执行insert into adep values(2000);
- SQLServer转Oracle,这个语句不知道怎样转,哪位高手帮帮忙!!!
- 求购二手书:《Oracle编程入门经典——覆盖至Oracle 9i版本》
- 一个简单问题,在线等……
- oracle net manager 的问题
- 急!分区表建在日期字段上,主键是其他字段,在每个分区上物理数据顺序是怎样?
这话你从哪里得到的结果啊?
因为我外层每一个deptno到内层去查,只查询我该deptno对应的avg(sal),不会查询出其它deptno的信息,因此内层的全表扫描读取数据块就少!这就是你第一个执行计划的 0 db block gets 比第二个少的原因。对于第二个SQL,虽然执行计划和第一个一样,但读取的数据块却多了,就是因为你两次全表扫描都是扫描出所有deptno的信息,然后才关联过滤资料的,再求avg(sal)的时候,第二个SQL显然读取的数据块多于第一个,它是查询的所有deptno对应的avg(sal)
看红色部分1.
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 processed2
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
你说“由于使用了子查询,前条语句的性能更好。”,你说出你的理由。
1、对 EMP 表执行全表扫描,获取每一行数据并将 deptno 值作为条件用于子查询;
2、对 EMP 表执行全表扫描,并使用步骤 1 获取 deptno 值对结果进行筛选;
3、对步骤 3 获取的查询结果执行聚合操作,获取 avg(sal) 值用于外层查询;
4、使用步骤 3 获取的 avg(sal) 值对步骤 1 获取的表行进行筛选。从查询计划看对 EMP 表只执行了 2 次表扫描;而各个步骤所产生的结果缓存在 PGA 中供下一步使用。
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing optionsSQL> set autotrace traceonly
SQL> select * from scott.emp e1 where e1.sal>(select avg(sal) from scott.emp e2 where e1.deptno=e2.deptno);
执行计划
----------------------------------------------------------
Plan hash value: 1245077725--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 58 | 8 (25)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 58 | 8 (25)| 00:00:01 |
| 2 | VIEW | VW_SQ_1 | 3 | 78 | 4 (25)| 00:00:01 |
| 3 | HASH GROUP BY | | 3 | 21 | 4 (25)| 00:00:01 |
| 4 | TABLE ACCESS FULL| EMP | 15 | 105 | 3 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | EMP | 15 | 480 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------------Predicate Information (identified by operation id):
--------------------------------------------------- 1 - access("E1"."DEPTNO"="ITEM_1")
filter("E1"."SAL">"AVG(SAL)")
统计信息
----------------------------------------------------------
720 recursive calls
0 db block gets
136 consistent gets
15 physical reads
0 redo size
992 bytes sent via SQL*Net to client
416 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
9 sorts (memory)
0 sorts (disk)
4 rows processed
执行计划
----------------------------------------------------------
Plan hash value: 269884559-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 58 | 8 (25)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 58 | 8 (25)| 00:00:01 |
| 2 | VIEW | | 3 | 78 | 4 (25)| 00:00:01 |
| 3 | HASH GROUP BY | | 3 | 15 | 4 (25)| 00:00:01 |
| 4 | TABLE ACCESS FULL| EMP | 15 | 75 | 3 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | EMP | 15 | 480 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------Predicate Information (identified by operation id):
--------------------------------------------------- 1 - access("E1"."DEPTNO"="E2"."DNO")
filter("E1"."SAL">"E2"."A_SAL")
统计信息
----------------------------------------------------------
256 recursive calls
0 db block gets
60 consistent gets
0 physical reads
0 redo size
1178 bytes sent via SQL*Net to client
416 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
4 rows processed
----------------------------------------------------------
Plan hash value: 1245077725--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 58 | 8 (25)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 58 | 8 (25)| 00:00:01 |
| 2 | VIEW | VW_SQ_1 | 3 | 78 | 4 (25)| 00:00:01 |
| 3 | HASH GROUP BY | | 3 | 21 | 4 (25)| 00:00:01 |
| 4 | TABLE ACCESS FULL| EMP | 15 | 105 | 3 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | EMP | 15 | 480 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------------Predicate Information (identified by operation id):
--------------------------------------------------- 1 - access("E1"."DEPTNO"="ITEM_1")
filter("E1"."SAL">"AVG(SAL)")
统计信息
----------------------------------------------------------
1 recursive calls
0 db block gets
14 consistent gets
0 physical reads
0 redo size
992 bytes sent via SQL*Net to client
416 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4 rows processed
----------------------------------------------------------
Plan hash value: 269884559-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 58 | 8 (25)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 58 | 8 (25)| 00:00:01 |
| 2 | VIEW | | 3 | 78 | 4 (25)| 00:00:01 |
| 3 | HASH GROUP BY | | 3 | 15 | 4 (25)| 00:00:01 |
| 4 | TABLE ACCESS FULL| EMP | 15 | 75 | 3 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | EMP | 15 | 480 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------Predicate Information (identified by operation id):
--------------------------------------------------- 1 - access("E1"."DEPTNO"="E2"."DNO")
filter("E1"."SAL">"E2"."A_SAL")
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
14 consistent gets
0 physical reads
0 redo size
1178 bytes sent via SQL*Net to client
416 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
4 rows processedSQL>