Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> Re: How to get query to use an index

Re: How to get query to use an index

From: Gints Plivna <gints.plivna_at_gmail.com>
Date: Thu, 13 Apr 2006 15:16:54 +0300
Message-ID: <6e49b6d00604130516nc212f18u@mail.gmail.com>


Yea and BTW there are cases (though in normal applications rare) when you have to use OR, because neither UNION nor UNION ALL works. You can easily take the example provided by Michael and simply insert row insert into mgsx values('x','x'); two times - so you got different results for each of three variations. So you simply have to be careful and know your data and know what you really need as output.

SQL> select * from mgsx where c1 like 'x%' or c2 like 'x%';

C1 C2
---------- ----------

x          y
y          x
x          x
x          x

4 rows selected.

SQL> select * from mgsx where c1 like 'x%'   2 union all
  3 select * from mgsx where c2 like 'x%'   4 /

C1 C2
---------- ----------

x          y
x          x
x          x
y          x
x          x
x          x

6 rows selected.

SQL> select * from mgsx where c1 like 'x%'   2 union
  3 select * from mgsx where c2 like 'x%'   4 /

C1 C2
---------- ----------

x          x
x          y
y          x

3 rows selected.

Gints

2006/4/13, Gints Plivna <gints.plivna_at_gmail.com>:
> Trying to remeber something from set theory ....
>
> Let's imagine you get set A from the first satement in UNION and set B
> from the second statement in UNION. If intersection of A and B is
> empty set then it makes no difference either to use UNION or UNION ALL
> (except that oracle anyway performs sort unique in case of UNION). But
> if intersection of A and B is not empty set as in example where row
> with both c1 and c2 = 'x', then it is important because ORed
> expression gives back only one instance of this particular row but
> UNION ALL gives us two.
> So if you are sure that always will be only one true either c1 = 'x'
> or c2='x' then you can use UNION ALL and it should perform better
> because of lack of sort unique.
> But if you aren't sure and there may be cases when both c1 = 'x' and
> c2 = 'x' then you have to use just UNION.
>
> Gints
>
> 2006/4/12, Tim Gorman <tim_at_evdbt.com>:
> > Michael,
> >
> > Very interesting! Expanding your test to include just using plain UNION
> > operator (which performs a DISTINCT), the results become correct.
> >
> > So, I had always thought UNION-ALL was equivalent to an OR'd expression; is
> > it really UNION?
> >
> > Thanks!!!
> >
> > -Tim
> >
>

--
http://www.freelists.org/webpage/oracle-l
Received on Thu Apr 13 2006 - 07:16:54 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US