X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id BFA4455D73 for ; Wed, 2 Mar 2005 18:11:30 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 45167-08 for ; Wed, 2 Mar 2005 18:11:26 +0000 (GMT) Received: from quasar.skima.is (quasar.skima.is [212.30.200.205]) by svr1.postgresql.org (Postfix) with ESMTP id 55ED6550E3 for ; Wed, 2 Mar 2005 18:11:24 +0000 (GMT) Received: from gnari ([157.157.175.249] [157.157.175.249]) by quasar.skima.is with ESMTP; Wed, 2 Mar 2005 18:11:23 Z Subject: Re: Help with tuning this query From: Ragnar =?ISO-8859-1?Q?Hafsta=F0?= To: Ken Egervari Cc: pgsql-performance@postgresql.org In-Reply-To: <001001c51ef4$3849eeb0$6a01a8c0@KEN> References: <001001c51ef4$3849eeb0$6a01a8c0@KEN> Content-Type: text/plain Date: Wed, 02 Mar 2005 18:13:47 +0000 Message-Id: <1109787227.31084.12.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/32 X-Sequence-Number: 10737 On Wed, 2005-03-02 at 01:51 -0500, Ken Egervari wrote: > > select s.* > from shipment s > inner join carrier_code cc on s.carrier_code_id = cc.id > inner join carrier c on cc.carrier_id = c.id > inner join carrier_to_person ctp on ctp.carrier_id = c.id > inner join person p on p.id = ctp.person_id > inner join shipment_status cs on s.current_status_id = cs.id > inner join release_code rc on cs.release_code_id = rc.id > left join shipment_status ss on ss.shipment_id = s.id > where > p.id = :personId and > s.is_purged = false and > rc.number = '9' and > cs is not null and > cs.date >= current_date - 31 > order by cs.date desc > ... > shipment contains 40,000 rows > shipment_status contains 80,000 rows I may be missing something, but it looks like the second join on shipment_status (the left join) is not adding anything to your results, except more work. ss is not used for output, nor in the where clause, so what is its purpose ? if cs.date has an upper limit, it might be helpful to change the condition to a BETWEEN in any case, i would think you might need an index on shipment(carrier_code_id) shipment(current_status_id) shipment_status(id) gnari