------------SQL 1:出结果需要2分多钟----------------
表达式A 
where  ( h.A_NUM2 = 'S' 
         OR  h.A_NUM2 in
                ( SELECT to_char(customer.CUSTOMERID)  
                  FROM customer ) 
       )
-------------------------------------------------------------SQL 2:马上能出结果-------------------
表达式A 
where h.A_NUM2 in
       (SELECT to_char(customer.CUSTOMERID)  
        FROM customer)
union all
表达式A 
where h.A_NUM2 = 'S'       
-------------------------------------------------为什么这样?
      

解决方案 »

  1.   

    SQL1 没有用上索引,有 Or语句,不过可以强制指定索引进行查询
    SQL2 用上了索引
      

  2.   

    or谓词会让dbms放弃使用索引来执行指定的查询。
    所以,考虑将语句修改
    select ..
    where  ( h.A_NUM2 =  'S '  
             OR  h.A_NUM2 in 
                    ( SELECT to_char(customer.CUSTOMERID)   
                      FROM customer )  
           ) 
    union all
    select 
    where  h.A_NUM2 =  'S '  
      

  3.   

    or谓词会让dbms放弃使用索引来执行指定的查询。 
    所以,考虑将语句修改 
    select .. 
    where   h.A_NUM2 in  
                    ( SELECT to_char(customer.CUSTOMERID)    
                      FROM customer )   
           
    union all 
    select  
    where  h.A_NUM2 =   'S  '  
      

  4.   

    是不是这个意思:有or的话DBMS会一次检查or两边的条件,会打断索引的使用,用union就相当于全表用索引扫描一次?
      

  5.   

    第一个是因为or的原因,where 子句解析是从右到左的,你不妨把两个条件调换一下看看;第二个两个条件同时解析,union all不排除重复,因此不影响效率。