以前在做项目时,是真的没有太注意Oracle的查询效率这回事,就算前面在blog中有写过或者转载过类似的blog,但在实际应用中,却很少去应用它们,今天又在项目里面看到了,就记录下来,希望下次在做项目的时候能够记得使用这些,以提高项目对数据查询的效率。
1、在Oracle解析SQL语句的规则,where子句,的解析是由后至前,也可以说是由下至上的,所以当我们把where子句中能够大量过滤数据的条件写在where语句的最后边,而把其它条件写在where子句的前面这样可能很好的运用Oracle的这个解析规则提高查询效率,如以下例子:
(低效,执行时间156.3秒)
select …
from emp e where sal > 50000 and job = ‘manager' and 25 <
(select count(*) from emp where mgr=e.empno);
(高效,执行时间10.6秒)
select …
from emp e where 25 < (select count(*) from emp where
mgr=e.empno) and sal > 50000 and job = ‘manager';
2、如果有3个以上的表连接查询,
那就需要选择交叉表(intersection table)作为基础表,
交叉表是指那个被其他表所引用的表。
select *from location l ,category c,emp e where e.emp_no between
1000 and 2000 and e.cat_no = c.cat_no and e.locn =
l.locn(效率高)
select *from
emp e ,location l ,category c where e.cat_no = c.cat_no and e.locn
= l.locn and e.emp_no between 1000 and 2000(效率低)
3、在select子句中尽量避免使用‘*’,因为在oarcle的解析过程中,它会把*依次地转换成所有的列名,而这又需要通过对数据字典进行查询来完成,所以会比较耗时。
这里还有其它的优化方案就不一一列举了,可以查看
http://blog.chinaunix.net/uid-7431665-id-2627247.html这个地方。
<!-- 正文结束 -->
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22068756/viewspace-1116885/
,如需转载,请注明出处,否则将追究法律责任。