> -----Original Message-----
> From: Ow Mun Heng
> Subject: Re: [PERFORM] PostgreSQL vs MySQL, and FreeBSD
> 
> Even for Postgresql, nested loops are still evil and hampers 
> performance.


I don't know about that.  There are times when it is the right plan:
 

explain analyze select * from table1 t1 inner join table2 t2 on t1.f_id =
t2.id where t1.id = 'xyzzy';

                                                                 QUERY PLAN
----------------------------------------------------------------------------
----------------------------------------------------------------
 Nested Loop  (cost=0.00..17.65 rows=1 width=344) (actual time=0.080..0.096
rows=1 loops=1)
   ->  Index Scan using table1_pkey on table1 t  (cost=0.00..9.18 rows=1
width=238) (actual time=0.044..0.048 rows=1 loops=1)
         Index Cond: ((id)::text = 'xyzzy'::text)
   ->  Index Scan using table2_pkey on table2 i  (cost=0.00..8.46 rows=1
width=106) (actual time=0.019..0.023 rows=1 loops=1)
         Index Cond: (t.f_id = i.id)
 Total runtime: 0.224 ms


set enable_nestloop=off;
SET


explain analyze select * from table1 t1 inner join table2 t2 on t1.f_id =
t2.id where t1.id = 'xyzzy';

                                                               QUERY PLAN
----------------------------------------------------------------------------
------------------------------------------------------------
 Hash Join  (cost=9.18..72250.79 rows=1 width=344) (actual
time=13493.572..15583.049 rows=1 loops=1)
   Hash Cond: (i.id = t.f_id)
   ->  Seq Scan on table2 i  (cost=0.00..61297.40 rows=2188840 width=106)
(actual time=0.015..8278.347 rows=2188840 loops=1)
   ->  Hash  (cost=9.18..9.18 rows=1 width=238) (actual time=0.056..0.056
rows=1 loops=1)
         ->  Index Scan using table1_pkey on table1 t  (cost=0.00..9.18
rows=1 width=238) (actual time=0.040..0.045 rows=1 loops=1)
               Index Cond: ((id)::text = 'xyzzy'::text)
 Total runtime: 15583.212 ms

(I changed the table names, but everything else is real.)



---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

               http://www.postgresql.org/docs/faq

Reply via email to