In that simple example the OR is almost certainly faster...

1)  UNION forces a sort operation to be performed (UNION ALL is better in
this respect).

2)  If the query is forced to perform a full table scan then the union
option will perform two scans.  There will be similar issues if a full
index scan is performed.  Of course a lot of data will be cached for the
second pass but it must still be done.

Therefore, in your simple example (not knowing table volumes, indexing
strategies, etc) I would suggest the OR option.  Having said that, there
are examples in complex queries where a union may perform better (and it
may be easier to write as well).

Regards,
      Mark.



                                                                                       
                                               
                      Saminathan_Seeran                                                
                                               
                      [EMAIL PROTECTED]               To:       Multiple recipients of 
list ORACLE-L <[EMAIL PROTECTED]>                  
                      Sent by:                 cc:                                     
                                               
                      [EMAIL PROTECTED]        Subject:  OR Vs UNION                   
                                               
                      .com                                                             
                                               
                                                                                       
                                               
                                                                                       
                                               
                      24/06/2003 15:34                                                 
                                               
                      Please respond to                                                
                                               
                      ORACLE-L                                                         
                                               
                                                                                       
                                               
                                                                                       
                                               








Hi All,

Could someone explain to me which one is best in the following two queries
w.r.t performance?

Thanks
Sami

Query1)
========
select distinct empployee_id from employees where department_id=10 or
department_id=20
/

Query2)
=======
select employee_id from employees where department_id=10
union
select employee_id from employees where department_id=20
/


--
Please see the official ORACLE-L FAQ: http://www.orafaq.net
--
Author:
  INET: [EMAIL PROTECTED]

Fat City Network Services    -- 858-538-5051 http://www.fatcity.com
San Diego, California        -- Mailing list and web hosting services
---------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).



<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<---->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
   Privileged/Confidential information may be contained in this message.
          If you are not the addressee indicated in this message
       (or responsible for delivery of the message to such person),
            you may not copy or deliver this message to anyone.
In such case, you should destroy this message and kindly notify the sender
           by reply e-mail or by telephone on (61 3) 9612-6999.
   Please advise immediately if you or your employer does not consent to
                Internet e-mail for messages of this kind.
        Opinions, conclusions and other information in this message
              that do not relate to the official business of
                         Transurban City Link Ltd
         shall be understood as neither given nor endorsed by it.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<---->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.net
-- 
Author: Mark Richard
  INET: [EMAIL PROTECTED]

Fat City Network Services    -- 858-538-5051 http://www.fatcity.com
San Diego, California        -- Mailing list and web hosting services
---------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).

Reply via email to