首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

一个SQL语句优化,解决办法

2011-12-27 
一个SQL语句优化,急!!!SQL:selectfexp_bill_no,bill_type,fexp_remarkfrom(selectfexp_bill_no,casewhenex

一个SQL语句优化,急!!!
SQL:
select   fexp_bill_no,   bill_type,   fexp_remark
                    from   (select   fexp_bill_no,
                                              case
                                                  when   exp.FEXP_FORWARD_FLAG   =   'N '   then   exp.fexp_rp_flag
                                                  when   exp.FEXP_FORWARD_FLAG   =   'Y '   and   exp.fexp_rp_flag   =   '0 '   then   '5 '
                                                  when   exp.FEXP_FORWARD_FLAG   =   'Y '   and   exp.fexp_rp_flag   =   '1 '   then   '6 '
                                              end   bill_type,
                                              fexp_remark,
                                              row_number()   over(partition   by   exp.fexp_bill_no   order   by   exp.fexp_charge_id,   exp.fexp_currency_code)   rn
                                    from   fexpense   exp
                                  where   exp.fexp_cancel_flag   =   'N '   and
                                              exp.fexp_remark   is   not   null)
                  where   rn   =   1

fexpense表的数据为638721条,查出记录数为2261条,执行时间为2.641s,大家有什么好的优化方法(建立了对fexp_bill_no,fexp_charge_id,fexp_currency_code的联合索引,更慢,要7S多)

[解决办法]
換種方法試試呢
select fexp_bill_no, bill_type, fexp_remark
from
(
select fexp_bill_no,
case
when exp.FEXP_FORWARD_FLAG = 'N ' then exp.fexp_rp_flag
when exp.FEXP_FORWARD_FLAG = 'Y ' and exp.fexp_rp_flag = '0 ' then '5 '
when exp.FEXP_FORWARD_FLAG = 'Y ' and exp.fexp_rp_flag = '1 ' then '6 '
end bill_type,
fexp_remark
from fexpense exp
where exp.fexp_cancel_flag = 'N ' and
exp.fexp_remark is not null and
rowid in
(select rowid from(select rowid from fexpense t group by t.fexp_bill_no order by t.fexp_charge_id,t.fexp_currency_code ) where rownum <=1)
[解决办法]
考虑建个fexp_cancel_flag+fexp_remark的索引,建完索引对表作个分析。

热点排行