您的位置:首页 > 博客中心 > 数据库 >

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

时间:2022-03-14 04:42

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

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_poor

from emp;

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

热门排行

今日推荐

热门手游