select * from COREMOUNT.FJ_LEAVE_APPLIES 
 where 1=1    
 and OWNER = 'yszx10'  
 and cast(leave_apply_id as VARCHAR2(20) ) not in 
 (select key1 from b_t_ack_rslt 
 where ACK_KIND_CD='5hf6d8fa32hzt9t' 
 union 
 select distinct bm.key1 from b_m_ack_rslt bm 
 where bm.ACK_KIND_CD ='5hf6d8fa32hzt9t' ) 
 order by LEAVE_APPLY_ID desc

解决方案 »

  1.   

    把not in 和union改为两个not exists试试。
      

  2.   

    主要是这个UNION,可以把UNION 改写成外连接。NOT IN 改为NOT EXISTS
      

  3.   

    cast(leave_apply_id as VARCHAR2(20) )
    改成:leave_apply_id
      

  4.   

    select * from COREMOUNT.FJ_LEAVE_APPLIES  t1
    where 1=1    
    and OWNER = 'yszx10'  
    and cast(leave_apply_id as VARCHAR2(20) ) not exists(
    select key1 from 
    (select key1 from b_t_ack_rslt 
    where ACK_KIND_CD='5hf6d8fa32hzt9t' 
    union 
    select distinct bm.key1 from b_m_ack_rslt bm 
    where bm.ACK_KIND_CD ='5hf6d8fa32hzt9t' )t2 
    where t1.key1=t2.key1

    order by LEAVE_APPLY_ID desc
      

  5.   

    select * from COREMOUNT.FJ_LEAVE_APPLIES t1,
                  (select key1 from b_t_ack_rslt 
                   where ACK_KIND_CD='5hf6d8fa32hzt9t' 
                   union  all
                   select bm.key1 from b_m_ack_rslt bm 
                   where bm.ACK_KIND_CD ='5hf6d8fa32hzt9t' )   t2
    where 1=1    
    and OWNER = 'yszx10'  
    and t1.leave_apply_id = to_number(t2.key1)(+) and t1.leave_apply_id is null
    order by LEAVE_APPLY_ID desc
      

  6.   

    如果还要优化,那么UNION部分还可以再调整优化。要看你的语句调用频度有多高了
      

  7.   

    select * from COREMOUNT.FJ_LEAVE_APPLIES 
    where OWNER = 'yszx10'  
    and  not exists 
    (
    select * from (select key1 from b_t_ack_rslt 
    where ACK_KIND_CD='5hf6d8fa32hzt9t' 
    union 
    select bm.key1 from b_m_ack_rslt bm 
    where bm.ACK_KIND_CD ='5hf6d8fa32hzt9t') where cast(leave_apply_id as VARCHAR2(20))=key1)
    order by LEAVE_APPLY_ID desc