找到员工表中薪水大于本部门平均薪水的员工
select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal
from emp e
where sal>(select avg(sal) from emp where deptno=e.deptno);
-- 用多表查询实现
select e.empno,e.ename,e.sal,d.avgsal
from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) d
where e.deptno=d.deptno and e.sal > d.avgsal;
--- 查询sql语句运算效率
explain plan for
select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal
from emp e
where sal>(select avg(sal) from emp where deptno=e.deptno);
select * from table (dbms_xplan.display);
explain plan for
select e.empno,e.ename,e.sal,d.avgsal
from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) d
where e.deptno=d.deptno and e.sal > d.avgsal;
select * from table (dbms_xplan.display);
************************************************************************
SQL> select rownum,r,empno,ename,sal
2 from (select rownum r,empno,ename,sal
3 from (select rownum,empno,ename,sal from emp order by sal desc) e1
4 where rownum<=8) e2
5 where r>=5;
ROWNUM R EMPNO ENAME SAL
---------- ---------- ---------- ---------- ----------
1 5 7698 BLAKE 2850
2 6 7782 CLARK 2450
3 7 7499 ALLEN 1600
4 8 7844 TURNER 1500
SQL> select e.empno,e.ename,e.sal,d.avgsal
2 from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) d
3 where e.deptno=d.deptno and e.sal > d.avgsal;
EMPNO ENAME SAL AVGSAL
---------- ---------- ---------- ----------
7499 ALLEN 1600 1566.66667
7566 JONES 2975 2175
7698 BLAKE 2850 1566.66667
7788 SCOTT 3000 2175
7839 KING 5000 2916.66667
7902 FORD 3000 2175
已选择6行。
SQL> select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal
2 from emp e
3 where sal>(select avg(sal) from emp where deptno=e.deptno);
EMPNO ENAME SAL AVGSAL
---------- ---------- ---------- ----------
7499 ALLEN 1600 1566.66667
7566 JONES 2975 2175
7698 BLAKE 2850 1566.66667
7788 SCOTT 3000 2175
7839 KING 5000 2916.66667
7902 FORD 3000 2175
已选择6行。
SQL> explain plan for
2 select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal
3 from emp e
4 where sal>(select avg(sal) from emp where deptno=e.deptno);
已解释。
SQL>
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------
------------------------------
Plan hash value: 2385781174
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 43 | 8 (25)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 7 | | |
|* 2 | TABLE ACCESS FULL | EMP | 5 | 35 | 3 (0)| 00:00:01 |
|* 3 | HASH JOIN | | 1 | 43 | 8 (25)| 00:00:01 |
| 4 | VIEW | VW_SQ_1 | 3 | 78 | 4 (25)| 00:00:01 |
| 5 | HASH GROUP BY | | 3 | 21 | 4 (25)| 00:00:01 |
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------
------------------------------
| 6 | TABLE ACCESS FULL| EMP | 14 | 98 | 3 (0)| 00:00:01 |
| 7 | TABLE ACCESS FULL | EMP | 14 | 238 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("DEPTNO"=:B1)
3 - access("ITEM_1"="E"."DEPTNO")
filter("SAL">"AVG(SAL)")
已选择21行。
SQL> explain plan for
2 select e.empno,e.ename,e.sal,d.avgsal
3 from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) d
4 where e.deptno=d.deptno and e.sal > d.avgsal;
已解释。
SQL> select * from table (dbms_xplan.display);
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------
------------------------------
Plan hash value: 269884559
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 43 | 8 (25)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 43 | 8 (25)| 00:00:01 |
| 2 | VIEW | | 3 | 78 | 4 (25)| 00:00:01 |
| 3 | HASH GROUP BY | | 3 | 21 | 4 (25)| 00:00:01 |
| 4 | TABLE ACCESS FULL| EMP | 14 | 98 | 3 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | EMP | 14 | 238 | 3 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------
------------------------------
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."DEPTNO"="D"."DEPTNO")
filter("E"."SAL">"D"."AVGSAL")
已选择18行。
案例二:
使用explain plan for 得到Sql语句的执行计划,查看其耗费的系统资源,查看执行计划:select* from table (dbms_xplan.display);得到使用相关子查询的方式效果更好一些
explain plan for 生成SQL的执行计划
多表 查询
select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal from emp e where sal>(select avg(sal) from emp where deptno=e.deptno) select e.empna,e.ename,e.sal,d.avgsal from emp e,select deptno,avg(sal) avgsal from emp group by deptno)d
where e.deptno=d.deptno and e.sal>d.avgsal
示例2
找到员工表中薪水大于本部门平均薪水的员工
1、相关子查询
select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal from emp e where sal >(select avg(sal) from emp where deptno=e.deptno);
-------------------------------------------
2、多表查询嵌套子查询
select e.empno,e.ename,e.sal,d.avgsal from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) d where e.deptno=d.deptno and e.sal>d.avgsal;
---------------------------------------------
写执行计划
explain plan for
select e.empno,e.ename,e.sal,d.avgsal from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) d where e.deptno=d.deptno and e.sal>d.avgsal;
查看执行计划
select * from table (dbms_xplan.display);
用相关子查询方式更省资源
explain plan for
select 。。。
select * from table (dbms_xplan.display);
相关子查询,需要先从主查询查询出一个值后,子查询才能执行查询
执行计划: explain plan for sql语句
查看执行计划:select * from table(dbms_xplan.display);
执行计划对比
样式:
explain plan for
select ...
from...
where...
select * from table(dbms_xplan.display);【查看】
当一个结果有两种解法,就是实现一个功能可以是多种sql语句时,可以通过执行计划查看哪个的效率更高 -----
explain plan for sql语句;
select * from table (dbm_xplan.display);
oracle:执行计划生成
explain plan for sql语句;
查询执行计划消耗资源
select * from table (dbms_xplan.display);
相关子查询优于多表查询
使用相关子查询比多表查询性能更好,消耗cpu的性能更少
explain for select a from b;
select * from table(dbms_xplan.display);
select * from v$sql;
select * from v$sql_plan where sql_id;
select * from v$sql
select * from table(dbms_xplan.display_cursor(sql_id,0));
select * from table(dbms_xplan.display_cursor(nul,null));
--执行计划:explain plan for select .......
--查看执行计划:select * from table(dbms_xplan.display);
explain plan for
(
select *
from emp e,(select e.deptno,avg(e.sal) avgsal from emp e group by e.deptno) d
where e.deptno=d.deptno and e.sal>d.avgsal
)
select * from table(dbms_xplan.display);
执行计划:explain plan from select .......
查看执行计划:select * from table(dbms_xplan.display);
select empno,empname,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal from emp e
where sal>(select avg(sal) from emp where e.deptno=deptno)
select e.depno,e.empno,e.empname,f.avgsal
from emp e,(select depno,avg(sal) avgsal from emp group by depno) f
where e.depno=f.depno and e.sal>f.avgsal
查询工资大于部门平均工资的员工
--相关子查询:
select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsalfrom emp e
where sal>(select avg(sal) from emp where deptno=e.deptno)
--多表查询:
select e.empno,e.ename,e.sal,d.avgsal
from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) d
where e.deptno=d.deptno and e.sal>d.avgsal
--比较解释计划窗口 的相关子查询比多表查询占用cpu少
explain plan for select * from table; select * from table(dbms xplan.display);