SQL改写, 聚合语句, left join 改写标量子查询
来源:互联网 发布:python高级编程最新版 编辑:程序博客网 时间:2024/05/17 07:39
最近遇到一个SQL 改写, 其实改写难度一般,但连续强调用 left join 改写标量子查询, 不知道为何 如此多的标量子查询
案例 说明: 为了公司机密, 所以用scott 用户表示
select d.*, (select count(*) from scott.emp) cnt1,
(select count(*) from scott.emp p where p.deptno = d.deptno) cnt2,
(select count(*) from scott.emp p where p.deptno = d.deptno and p.job='CLERK') ccnt,
(select count(*) from scott.emp p where p.deptno = d.deptno and p.job='SALESMAN') scnt
from scott.dept d;
select d.deptno, d.dname, d.loc, sum(decode(count(*), 1, 0, count(*))) over() cnt1 ,
decode(count(*), 1, 0, count(*)) cnt2 ,
count( case when p.job='CLERK' then 1 else null end) ccnt,
count( case when p.job='SALESMAN' then 1 else null end) scnt
from scott.dept d
left join scott.emp p on d.deptno = p.deptno
group by d.deptno, d.dname, d.loc, d.deptno ;
decode(count(*), 1, 0, count(*)) cnt2 , 由于是left join 所以这里是 1 就是 0 。
count( case when p.job='CLERK' then 1 else null end) ccnt, 就是统计 'CLERK' 的数量, 类似 (select count(*) from scott.emp p where p.deptno = d.deptno and p.job='CLERK') ccnt,
结果测试 数据完全相同。 效率提升 10倍。
第一个 的执行 计划
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 80 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | | |
| 2 | INDEX FULL SCAN | PK_EMP | 14 | | 1 (0)| 00:00:01 |
| 3 | SORT AGGREGATE | | 1 | 3 | | |
|* 4 | TABLE ACCESS FULL| EMP | 5 | 15 | 3 (0)| 00:00:01 |
| 5 | SORT AGGREGATE | | 1 | 11 | | |
|* 6 | TABLE ACCESS FULL| EMP | 1 | 11 | 3 (0)| 00:00:01 |
| 7 | SORT AGGREGATE | | 1 | 11 | | |
|* 8 | TABLE ACCESS FULL| EMP | 1 | 11 | 3 (0)| 00:00:01 |
| 9 | TABLE ACCESS FULL | DEPT | 4 | 80 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - filter("P"."DEPTNO"=:B1)
6 - filter("P"."JOB"='CLERK' AND "P"."DEPTNO"=:B1)
8 - filter("P"."JOB"='SALESMAN' AND "P"."DEPTNO"=:B1)
统计信息
----------------------------------------------------------
1 recursive calls
0 db block gets
93 consistent gets
0 physical reads
0 redo size
1098 bytes sent via SQL*Net to client
519 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: 2617737508
--------------------------------------------------------------------------------
----------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
Time |
--------------------------------------------------------------------------------
----------
| 0 | SELECT STATEMENT | | 14 | 434 | 7 (29)|
00:00:01 |
| 1 | WINDOW BUFFER | | 14 | 434 | 7 (29)|
00:00:01 |
| 2 | HASH GROUP BY | | 14 | 434 | 7 (29)|
00:00:01 |
| 3 | MERGE JOIN OUTER | | 14 | 434 | 6 (17)|
00:00:01 |
| 4 | TABLE ACCESS BY INDEX ROWID| DEPT | 4 | 80 | 2 (0)|
00:00:01 |
| 5 | INDEX FULL SCAN | PK_DEPT | 4 | | 1 (0)|
00:00:01 |
|* 6 | SORT JOIN | | 14 | 154 | 4 (25)|
00:00:01 |
| 7 | TABLE ACCESS FULL | EMP | 14 | 154 | 3 (0)|
00:00:01 |
--------------------------------------------------------------------------------
----------
Predicate Information (identified by operation id):
---------------------------------------------------
6 - access("D"."DEPTNO"="P"."DEPTNO"(+))
filter("D"."DEPTNO"="P"."DEPTNO"(+))
统计信息
----------------------------------------------------------
1 recursive calls
0 db block gets
9 consistent gets
0 physical reads
0 redo size
1094 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
4 rows processed
SQL>
93 逻辑 VS 9 个逻辑读。 期间和 一个高级程序员 交流 他说 在 PL/SQL 中 的花费时间来判断 SQL 的效率比较, 我只是说道 有时候 不精准, 应该 看 逻辑读数量。
反正我关心的是执行计划的 统计信息。
- SQL改写, 聚合语句, left join 改写标量子查询
- left outer join 改写标量子查询
- 标量子查询SQL改写
- 标量子查询改写
- SQL优化-标量子查询的改写
- 标量子查询SQL改写一则(包括WITH的改写)
- 【SQL改写】notexists-leftjoin(distinct)whereisnull改写_标量子查询
- 不等值标量子查询改写
- oracle标量子查询简介和表连接改写
- postgresql中IN子查询改写为hash join连接
- SQL中带有NOT IN 子查询改写
- 一个sql语句的改写
- SQL语句-- LEFT JOIN 关联表查询
- sql 标量子查询
- Oracle 查询优化器 -- 改写查询语句
- 优化系列 | DELETE子查询改写优化
- 优化系列 | DELETE子查询改写优化
- sql 语句left join
- hdu 5489 Removed Interval 2015合肥网络赛 树状数组 dp 离散化/dp
- 在proteus软件80C51芯片隐藏电源接口VCC
- Block传值和传值之间的注意事项
- memory prefetch浅析
- 【UIKit-110-6】#import <UIKit/UITableView.h>外观
- SQL改写, 聚合语句, left join 改写标量子查询
- 学生选课系统(1)-功能模块
- xUtils基本介绍
- 字符编码(一)
- 表达式求值
- 操作系统基础知识(一)
- Android性能调优利器StrictMode
- D. Three Logos (CF Round #322 (Div.2) 瞎搞 分情况)
- Java中wait()与sleep()的区别