Oracle Sql优化一例---利用函数索引

在awr报告中,发现有个Sql效率很低:

select batch_status from  t_batch_info

where BATCH_STATUS='0' 

and sys_id='STM06';

查看执行计划发现查询利用了索引,索引中包含了batch_status字段,但是通过以下sql查询:

select batch_status,count(*) from t_batch_info 
group by BATCH_STATUS

发现batch_status字段的值很少:

batch_status count(*)

0   40350

1   4237357
2   1227
3   433515

之前sql的查询条件是batch_status='0',它只有4万多条数据,而绝大部分都是1,有400多万条数据,因此虽然用到了索引,但是使用效率低,

为解决这个问题,创建了函数索引,利用null值不进索引的特性,

create index ix_t_batch_info  on t_batch_info (decode(BATCH_STATUS,'1',null,'2',null,'3',null,BATCH_STATUS)) online;

而查询语句改成:

select batch_status from  t_batch_info

where decode(BATCH_STATUS,'1',null,'2',null,'3',null,BATCH_STATUS)='0' 

and sys_id='STM06';

经过验证,效率提高很多。

 

posted @ 2016-12-01 13:35  cooltank  阅读(631)  评论(0编辑  收藏  举报