标量子查询

--标量子查询select e.empno,e.ename,e.sal,e.deptno,(select d.dname from dept d where e.deptno = d.deptno)as dname from emp e--插入一条数据insert into emp(empno,deptno) values(9999,null)--返回结果15条记录--改成left join(hash outer)select e.empno,d.dname from emp e left join dept d on (e.deptno = d.deptno)--NL outerselect /*+ use_nl(e,d) */e.empno,d.dname from emp e left join dept d on (e.deptno = d.deptno)/*Note:修改后plan一般有outer字样,如果没有,注意是否改错。*/--用left join 优化标量子查询之聚合改写select dp.department_id,dp.department_name,dp.location_id,nvl((select sum(em.salary) from hr.employees em where em.department_id = dp.department_id),0) as sum_dept_salary from hr.departments dp--错误写法select dp.department_id,nvl(sum(em.salary),0) as sum_sal from hr.departments dp left join hr.employees em on dp.department_id = em.department_id --原标量子查询改写为:select em.department_id,sum(em.salary) as sum_sal from hr.employees emgroup by em.department_id--左联改写后的内联视图 select dp.department_id,nvl(sum(e.sum_sal),0) as sum_sal from hr.departments dp left join (select e.department_id,sum(e.salary) as sum_sal from hr.employees e group by e.department_id) e on (dp.department_id = e.department_id) group by dp.department_id,dp.location_id--create table dept2 as select * from scott.dept;insert into dept2 select * from scott.dept where deptno=10select t1.job,t1.deptno,(select distinct dname from dept2 b where b.deptno = t1.deptno) as dname from scott.emp t1order by 1,2,3--以下改写结果变了select distinct t1.job,b.deptno,b.dname from scott.emp t1 left join dept2 b on t1.deptno = b.deptno--正确改写select t1.job,f.dname from scott.emp t1 left join (select b.deptno,b.dname from dept2 b group by b.deptno,b.dname) f on (f.deptno = t1.deptno)

相关文章

数据库版本:11.2.0.4 RAC(1)问题现象从EM里面可以看到,在23号早上8:45~8:55时,数据库等待会话暴增...
(一)问题背景最近在对一个大约200万行数据的表查看执行计划时,发现存在异常,理论上应该返回100多万...
(一)删除备份--DELETE命令用于删除RMAN备份记录及相应的物理文件。当使用RMAN执行备份操作时,会在RM...
(1)DRA介绍 数据恢复顾问(Data Recovery Advise)是一个诊断和修复数据库的工具,DRA能够修复数据文...
RMAN(Recovery Manager)是Oracle恢复管理器的简称,是集数据库备份(backup)、修复(restore)和恢复...
(1)备份对象 可以使用RMAN进行的备份对象如下: --整个数据库:备份所有的数据文件和控制文件; --数...