减少表扫描次数的优化(三)
-------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 136 | 4 (25)| 00:00:01 |
|* 1 | FILTER | | | | | |
| 2 | HASH GROUP BY | | 4 | 136 | 4 (25)| 00:00:01 |
| 3 | TABLE ACCESS FULL| TEST | 4 | 136 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
四、求各部门最高工资信息
1、自连接方式:select * from emp a where sal=(select max(sal) max_sal from emp w where w.deptno=a.deptno) order by a.deptno;
2、内联视图:select a.* from emp a ,(select max(sal) max_sal ,deptno from emp group by deptno) w where w.deptno=a.deptno and a.sal=w.max_sal order by a.deptno;
3、分析函数:select * from (select max(sal) over(partition by deptno ) max_sal ,a.* from emp a) w where w.max_sal=w.sal order by w.deptno;
其中3、分析函数 的方法效率最高,这个例子可以应用到查询用户最后登录的信息上。
以上几个优化的宗旨,都是在减少子查询,减少表扫描,从而达到优化查询的目的。