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

Home -> Community -> Usenet -> c.d.o.server -> Re: Newbie question: select row in a table that not exist in other table.

Re: Newbie question: select row in a table that not exist in other table.

From: <sybrandb_at_yahoo.com>
Date: 21 Sep 2005 05:23:15 -0700
Message-ID: <1127305395.762825.68010@z14g2000cwz.googlegroups.com>

jose_luis_fdez_diaz_news_at_yahoo.es wrote:
> Hi,
>
>
> 1)
>
> select GPRS_CO_MSISDN
> from GPRS_SERVICIO_GPRS
> where NOT EXISTS (select 1 from SAPN_SERVICIO_APN
> where SAPN_CO_MSISDN =
> GPRS_SERVICIO_GPRS.GPRS_CO_MSISDN
> )
> group by GPRS_CO_MSISDN;
>
>
> 2)
>
> select g.GPRS_CO_MSISDN from GPRS_SERVICIO_GPRS g, SAPN_SERVICIO_APN a
> where NOT (g.GPRS_CO_MSISDN = a.SAPN_CO_MSISDN)
> group by g.GPRS_CO_MSISDN;
>
>
> Two questions:
>
> Is the result set the same in both queries ?
>
> Why query 1) is faster than query 2 ?
>
>
> Thanks in advance,
> Jose Luis.

The resultset is not the same.
1 is equivalent with an outer join
select g.GPRS_CO_MSISDN from GPRS_SERVICIO_GPRS g, SAPN_SERVICIO_APN a where g.GPRS_CO_MSISDN = a.SAPN_CO_MSISDN(+) and a.sapn_co_msisdn is null
group by g.GPRS_CO_MSISDN;
Your solution doesn't work, ad a.sapn_co_msisdn is either identical to g.gprs_co_msisdn or NULL. As NULL doesn't equal anything, the records you want are suppressed.
2 Using Cost Based Optimizer in a properly set up system, there should be no difference. The first statement, if the table is properly indexed, simply looks up 1 single appropiate record from the affected index, it doesn't read the table at all (this is why you are using the dummy 1), the second statement definitely reads the table.

Hth

-- 
Sybrand Bakker,
Senior Oracle DBA
Received on Wed Sep 21 2005 - 07:23:15 CDT

Original text of this message

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