select b.productdutycode,b.producttypecode,p.insuredcode,p.instcode, p.applcode, p.polcode, p.productcode, p.newamt, p.newprem, p.effdate,  p.feeinperiod, p.insurperiod,p.termdate,p.expirydate,p.feeinway, p.polstate, p.drawtime, p.appldate, p.insurperiodtype, p.feeinperiodtype,p.firstprm  from icont_polinfo  p  join base_prod_type_rel b on substr(p.productcode, 1, 3) = substr(b.productdutycode,1,3)  where  insuredcode='2009000001600010223'   and effdate<= to_date('2011-02-25','YYYY-MM-DD') and p.instcode='410000'  and (p.expirydate>= to_date('2011-02-25','YYYY-MM-DD') or p.expirydate is null)   and b.productdutycode = 'D40'  order by p.effdate ,b.puborder desc, p.applcode 
这条sql 怎么给他优化一下

解决方案 »

  1.   


     select b.productdutycode,b.producttypecode,p.insuredcode,p.instcode, p.applcode, p.polcode, p.productcode, p.newamt, p.newprem, p.effdate, p.feeinperiod, p.insurperiod,p.termdate,p.expirydate,p.feeinway, p.polstate, p.drawtime, p.appldate, p.insurperiodtype, p.feeinperiodtype,p.firstprm 
     from icont_polinfo p ,(select * from base_prod_type_rel where productdutycode = 'D40' ) b 
     where 
      insuredcode='2009000001600010223'
      and p.productcode like 'D40%' 
      and effdate<= to_date('2011-02-25','YYYY-MM-DD') 
      and p.instcode='410000' 
      and (p.expirydate>= to_date('2011-02-25','YYYY-MM-DD') or p.expirydate is null)  
      order by p.effdate ,b.puborder desc, p.applcode  
      

  2.   

    去掉order by,如果需求没要求,最好少用order by语句! 
      

  3.   

    貌似sql语句就那样了,优化加个索引试试
      

  4.   

    如果有方法
       p.expirydate>= to_date('2011-02-25','YYYY-MM-DD') or p.expirydate is null
    中的 or去掉的话还是能优点的。具体操作没想出来,或许根本就行不通
      

  5.   

    ……substr(p.productcode, 1, 3) = substr(b.productdutycode,1,3)……
    -----
    条件中不要使用函数,不然会走全表扫描,即使建了索引也不会走索引。
      

  6.   

    看了半天,看明白是 Oracle 的,但是:表结构怎么样?
    索引结构怎么样?
    查询做什么的?三个优化主要的问题一个都没提,把 SQL 语句往这里一贴就来让人优化了!
      

  7.   

    不知道你的数据库结构
    oracle的话给你提几点建议
    1:首先把能过滤大量记录的条件放在where的最后面
    2:p.expirydate is null如果expirydate 上有索引将不适用索引
    3:不需要的话就不使用order by,他会产生大量的对临时表空间的IO操作,以及占用大量的PGA内存