Re: simple join query runs very slowly - Mailing list pgsql-general

From Tom Lane
Subject Re: simple join query runs very slowly
Date
Msg-id 26439.1080329833@sss.pgh.pa.us
Whole thread Raw
In response to simple join query runs very slowly  (rdnews@dahlsys.com (Roger Dahl))
List pgsql-general
rdnews@dahlsys.com (Roger Dahl) writes:
> select b.name,
> from boxes b
> inner join item_box_maps m on m.box_id = b.id
> inner join items i on m.id = m.item_id
> where i.name = 'hammer';

You didn't say which PG version you are running, but I bet it is one
that strictly follows the JOIN syntax order.  Try flipping the order of
the JOIN clauses:

select b.name,
from boxes b
inner join items i on m.id = m.item_id
inner join item_box_maps m on m.box_id = b.id
where i.name = 'hammer';

There is more about this in the Performance Tips section of the docs.

            regards, tom lane

pgsql-general by date:

Previous
From: Eric Ridge
Date:
Subject: pg 7.3, cursors, and prepared queries
Next
From: Ron St-Pierre
Date:
Subject: Re: [NOVICE] Simple list tables question