pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
left join where not null vs. inner join
2+ messages / 2 participants
[nested] [flat]

* left join where not null vs. inner join
@ 2009-05-22 20:51 Emi Lu <emilu@encs.concordia.ca>
  2009-05-22 21:05 ` Re: left join where not null vs. inner join Erik Jones <ejones@engineyard.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Emi Lu @ 2009-05-22 20:51 UTC (permalink / raw)
  To: pgsql-sql

Two tables, each contains more than hundreds of thousands records. Is 
there any efficiency differences between (1) and (2)?

(1) T1 inner join T2 using (c1, c2)


(2) T1 left join T2 using (c1, c2) where c2 is not null


Thanks,
--
Lu Ying



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: left join where not null vs. inner join
  2009-05-22 20:51 left join where not null vs. inner join Emi Lu <emilu@encs.concordia.ca>
@ 2009-05-22 21:05 ` Erik Jones <ejones@engineyard.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Erik Jones @ 2009-05-22 21:05 UTC (permalink / raw)
  To: emilu@encs.concordia.ca; +Cc: pgsql-sql


On May 22, 2009, at 1:51 PM, Emi Lu wrote:

> Two tables, each contains more than hundreds of thousands records.  
> Is there any efficiency differences between (1) and (2)?
>
> (1) T1 inner join T2 using (c1, c2)
>
>
> (2) T1 left join T2 using (c1, c2) where c2 is not null

Yes, stick with the first.  In the second you're asking the db to  
generate a result set with tuples for every row in T1 and then filter  
it down to where there are only matching T2 rows whereas in the first  
it does the filtering as it goes.  The LEFT JOIN ... WHERE X NOT NULL  
construct is typically used as an alternative to a NOT IN or NOT  
EXISTS (<subquery>).  So, this:

SELECT *
FROM t1
WHERE id NOT IN (SELECT some_id
				    FROM T2);

becomes

SELECT t1.*
FROM t1 LEFT JOIN t2 ON (t1.id = t2.some_id)
WHERE t2.id IS NULL;

Basically, it's used in the opposite case of what you're asking about.

Erik Jones, Database Administrator
Engine Yard
Support, Scalability, Reliability
866.518.9273 x 260
Location: US/Pacific
IRC: mage2k








^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2009-05-22 21:05 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-05-22 20:51 left join where not null vs. inner join Emi Lu <emilu@encs.concordia.ca>
2009-05-22 21:05 ` Erik Jones <ejones@engineyard.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox