Select 1  from fexpense       e,       fconsign       c,       csubchargeitem s,       ccustsuppview  csv,       cuser          cu1,       cuser          cu2 where (e.fexp_creator = ? or exists (select f.FCSG_CONSIGN_ID from FCONSIGN f where f.fcsg_consign_id=e.fexp_consign_id and f.fcsg_creator = :"SYS_B_04"))
   and e.fexp_consign_id = c.fcsg_consign_id(+)   and e.fexp_charge_id = s.csci_ci_id(+)   and e.fexp_settlerment_object = csv.accountNumber(+)   and c.fcsg_creator = cu1.cusr_user_id(+)   and c.fcsg_canvasser = cu2.cusr_user_id(+)   and (e.fexp_forward_flag > ? or e.fexp_forward_flag < ?)   and (e.fexp_cancel_flag > ?or e.fexp_cancel_flag < ?)   and (e.fexp_lump_flag > ?or e.fexp_lump_flag < ?)   and e.fexp_org_id = ? order by e.fexp_settlerment_object ASC, e.fexp_currency_code ASC看了执行计划,提示fexpense表做了全表扫描,我对该表的WHERE条件中的字段做了个联合索引,还是提示全表,有什么解决办法

解决方案 »

  1.   

    exist结果就全是真的,估计你想要的是
    (e.fexp_creator = ? or e.fexp_creator in  (select f.FCSG_CONSIGN_ID from FCONSIGN f where f.fcsg_creator = :"SYS_B_04"))
    这样,就会用fexpense的fexp_creator索引了
      

  2.   

    SELECT count(:"SYS_B_000") as totalcount  FROM (select 1          from fconsign      c,               cdepartment   cbm,               fexpense      fe,               cuser         cu,               cdepartment   cp,               ccustsuppview csv2         where c.fcsg_canvasser = cu.cusr_user_id(+)           and cu.cusr_dept_id = cp.cdpt_dept_id(+)           and ? = cbm.cdpt_dept_id(+)           and c.fcsg_consign_dept = csv2.accountnumber(+)           and c.fcsg_consign_id = fe.fexp_consign_id(+)           and (c.fcsg_complete_time >= to_date(?,'yyyy-MM-dd')           and c.fcsg_org_id = ?)
    还有这个SQL语句要怎么建立索引来避免fconsign全表扫描
      

  3.   

    我也想问下,听人说
    在where 之后 
    先写   表关联
    然后是   从后往前  查询的是对的吗?
    那索引的顺序和SQL的顺序正好相反,是吗?
      

  4.   

    我有张表tab,分别建立索引IDX_A(字段a),IDX_B(字段b)
    我执行
    select 1 from tab t where t.a=:"1"和
    select 1 from tab t where t.b=:"1"
    看执行计划,第一条有用到索引,第二条没有索引是什么原因??
      

  5.   

    Analyze Table fexpense compute Statistics; ---更新数据统计alter index  index_name  rebuild;  -----如果索引没有生效则重编译因为oracle的优化器默认是按照choose 的,而不是基于规则的。
      

  6.   

    我分别对fexp_charge_id,fexp_settlerment_object,fexp_creator,fexp_consign_audit_flag,fexp_lock_flag,fexp_org_id字段做了索引,然后执行
    Analyze Table fexpense compute Statistics; ---更新数据统计
    还是不行,COST成本反而增加了些急救。。
      

  7.   

    这我知道,我想避免全表扫描。用exists替代in速度肯定快了