欢迎光临
我们一直在努力

ORACLE SQL性能优化系列 (五)-数据库专栏,ORACLE

建站超值云服务器,限时71元/月

17.       使用表的别名(alias)

当在sql语句中连接多个表时, 请使用表的别名并把别名前缀于每个column上.这样一来,就可以减少解析的时间并减少那些由column歧义引起的语法错误.

 

(译者注: column歧义指的是由于sql中不同的表具有相同的column名,当sql语句中出现这个column时,sql解析器无法判断这个column的归属)

 

18.       用exists替代in

在许多基于基础表的查询中,为了满足一个条件,往往需要对另一个表进行联接.在这种情况下, 使用exists(或not exists)通常将提高查询的效率.

 

低效:

select *

from emp (基础表)

where empno > 0

and deptno in (select deptno

from dept

where loc = ‘melb’)

   

高效:

select *

from emp (基础表)

where empno > 0

and exists (select ‘x’

from dept

where dept.deptno = emp.deptno

and loc = ‘melb’)

 

 

 (译者按: 相对来说,用not exists替换not in 将更显著地提高效率,下一节中将指出)

 

 

19.       用not exists替代not in

在子查询中,not in子句将执行一个内部的排序和合并. 无论在哪种情况下,not in都是最低效的 (因为它对子查询中的表执行了一个全表遍历).  为了避免使用not in ,我们可以把它改写成外连接(outer joins)或not exists.

 

例如:

select …

from emp

where dept_no not in (select dept_no

                         from dept

                         where dept_cat=’a’);

 

为了提高效率.改写为:

 

(方法一: 高效)

select ….

from emp a,dept b

where a.dept_no = b.dept(+)

and b.dept_no is null

and b.dept_cat(+) = ‘a’

 

 

(方法二: 最高效)

select ….

from emp e

where not exists (select ‘x’

                    from dept d

                    where d.dept_no = e.dept_no

                    and dept_cat = ‘a’);

 

(待续)

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ORACLE SQL性能优化系列 (五)-数据库专栏,ORACLE
分享到: 更多 (0)

相关推荐

  • 暂无文章