oracle 性能优化操作十二: 用Case语句合并多重扫描

来源:互联网 发布:淘宝二级页面怎么打开 编辑:程序博客网 时间:2024/05/05 08:00

【本文转载自:http://blog.csdn.net/helloboat/article/details/42486713,对作者表示感谢。】


我们常常必须基于多组数据表计算不同的聚集。例如下例通过三个独立查询:

select count(*) from emp where sal<1000;select count(*) from emp where sal between 1000 and 5000;select count(*) from emp where sal>5000;

这样我们需要进行三次全表查询,但是如果我们使用case语句:

select     count (sale when sal <1000    then 1 else null end)              count_poor,    count (sale when between 1000 and 5000    then 1 else null end)              count_blue_collar,    count (sale when sal >5000    then 1 else null end)              count_poorfrom emp;

这样查询的结果一样,但是执行计划只进行了一次全表查询。

0 0
原创粉丝点击