Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id B2AA0633F5F for ; Fri, 21 Aug 2009 12:15:43 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 41554-05 for ; Fri, 21 Aug 2009 15:15:28 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from out01.mta.xmission.com (out01.mta.xmission.com [166.70.13.231]) by mail.postgresql.org (Postfix) with ESMTP id 0694A633A52 for ; Fri, 21 Aug 2009 12:15:26 -0300 (ADT) Received: from in01.mta.xmission.com ([166.70.13.51]) by out01.mta.xmission.com with esmtp (Exim 4.62) (envelope-from ) id 1MeVq5-00089Z-NH; Fri, 21 Aug 2009 09:15:37 -0600 Received: from [208.187.158.94] (helo=[172.16.1.221]) by in01.mta.xmission.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1MeVpo-0000Dr-IZ; Fri, 21 Aug 2009 09:15:20 -0600 Message-ID: <4A8EBA03.4010309@gmail.com> Date: Fri, 21 Aug 2009 09:15:15 -0600 From: Rob Sargent User-Agent: Thunderbird 2.0.0.22 (X11/20090608) MIME-Version: 1.0 To: tripplowe CC: pgsql-sql@postgresql.org Subject: Re: Rewrite multiple joins... References: <25073534.post@talk.nabble.com> In-Reply-To: <25073534.post@talk.nabble.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-XM-SPF: eid=; ; ; mid=; ; ; hst=in01.mta.xmission.com; ; ; ip=208.187.158.94; ; ; frm=robjsargent@gmail.com; ; ; spf=neutral X-SA-Exim-Connect-IP: 208.187.158.94 X-SA-Exim-Rcpt-To: tripplowe@gmail.com, pgsql-sql@postgresql.org, robjsargent@gmail.com X-SA-Exim-Mail-From: robjsargent@gmail.com X-SA-Exim-Scanned: No (on in01.mta.xmission.com); Exit with error (see exim mainlog) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-3.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599, RCVD_IN_DNSWL_LOW=-1 X-Spam-Level: X-Archive-Number: 200908/120 X-Sequence-Number: 33029 Well indexing (or lack thereof) could be the real problem but you could try "chaining" the tables select * from sale s, taxes t, property p, buyer b where s.id = t.id and t.id = p.id and p.id = b.id and see if that (or other combination) changes the query plan appreciably. (I would have to wonder if all those records are really unique per sale.id? e.g. multiple buyers per unit sold or school and property tax in separate records. And wondering further why, if you have sql access, do you need a single table. Maybe you want a view for convenience (e.g. select * from view_of_sales where lower(county) like 'a%' )? tripplowe wrote: > Hey Folks, > > I have a query that seems like it will never end. I'm hoping you can help > me rewrite it. > > I have 4 tables that contain information about house sales within the state > (sale, taxes, property, buyer). Each table has an 'id' field that links > each record. I am just trying to flatten these files so I will have all of > the information in a single file. Below is my attempt to do so. Is there a > more efficient way to rewrite the joins? > > select * from sale, taxes, property, buyer > where sale.id = taxes.id and sale.id = property.id and sale.id = buyer.id > > I'm using postgresql 8.3 on a Windows 7 machine. > > Thank You, > -Tripp >