| From: | Steve Adams |
| Date: | 22-Sep-2000 00:41 |
| Subject: | Set operators and query transformation |
|
|
Yes, you should rewrite your query. If you explicitly code a UNION [ALL] into the query, Oracle takes it like a USE_CONCAT hint and does not attempt a transformation to enable the INLIST ITERATOR row source. Another problem with this query is that as written Oracle cannot replace the INTERSECT with a join. You also have several unwanted sorts. A better query would be
select
l.email
from
list l,
rental r
where
l.list_id in (1, 2) and
r.email = l.email and
r.rental_id = 3 and
r.client_id = 1
minus
select
p.email
from
rental p
where
p.rental_id in (1, 2) and
p.client_id = 1;
In general, you should avoid the set operators, and the DISTINCT keyword.
|
![]() |
Does this mean that I can rewrite my query with the minuses and the unions to be a distinct where in? For example:
select email from
(
(
select email from list
where list_id =1
union
select email from list
where list_id =2
)
minus
( select email from rental
where rental_id = 1
and client_id = 1
union
select email from rental
where rental_id = 2
and client_id = 1
)
)
intersect
( select email from rental
where rental_id = 3
and client_id = 1
);
Would become
select email from
(
(
select distinct email from list
where list_id in (1,2)
)
minus
( select distinct email from rental
where rental_id in ( 1,2 )
and client_id = 1
)
)
intersect
( select email from rental
where rental_id = 3
and client_id = 1
);
If I read your previous response properly, this may allow Oracle to decide to
perform an optimization on the read some of the time. I was under the
impression that Oracle would automatically rewrite queries to juggle this
sort of thing.
True?
|