Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 48B9047651B for ; Tue, 19 Nov 2002 11:26:31 -0500 (EST) Received: from wolf.urban.csuohio.edu (wolf.urban.csuohio.edu [137.148.96.16]) by postgresql.org (Postfix) with ESMTP id 207C8476501 for ; Tue, 19 Nov 2002 11:26:25 -0500 (EST) Received: from UR016B.urban.csuohio.edu (urban96-236.dhcp.csuohio.edu [137.148.96.236]) by wolf.urban.csuohio.edu (8.11.3/8.11.3) with ESMTP id gAJGQIX12637; Tue, 19 Nov 2002 11:26:18 -0500 (EST) Message-Id: <5.1.1.6.0.20021119112128.01ab52e0@wolf.csuohio.edu> X-Sender: ellen@wolf.csuohio.edu X-Mailer: QUALCOMM Windows Eudora Version 5.1.1 Date: Tue, 19 Nov 2002 11:26:15 -0500 To: Stephan Szabo From: Ellen Cyran Subject: Re: slow group by query Cc: In-Reply-To: <20021119065715.V65980-100000@megazone23.bigpanda.com> References: <5.1.1.6.0.20021118124632.01b5dcf0@wolf.csuohio.edu> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-MailScanner: Found to be clean X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/279 X-Sequence-Number: 10421 I had to modify your query somewhat, but the one below that is pretty much the same took about 12 seconds so once I run it on five years it will take just as long. Thanks for the suggestion though. select distinct on (b.msa_code, b.sic, b.own, b.ind_div) b.msa_code, b.sic, b.own, b.ind_div, y1975.emp from tbl_bls_msa as b left outer join (select msa_code, sic, own, ind_div, emp from tbl_bls_msa as bls where bls.year='1975' ) as y1975 on (b.msa_code=y1975.msa_code and b.sic=y1975.sic and b.own=y1975.own and b.ind_div=y1975.ind_div) where b.msa_code in ('1680', '1640', '0040', '0120', '0080'); I would be interested in knowing more about what postgres extensions are available. Where are those documented at? >Have you tried doing the subqueries in from? Right now you're running >each subquery once for each output row AFAICS. > >Maybe something like (doing only one year for example - and using a >postgres extension), would work... > >select distinct on (b.msa_code, b.sic, b.own, b.ind_div) > b.msa_code, b.sic, b.own, b.ind_div, y1975.emp1975 from >tbl_bls_msa as b left outer join (select emp from tbl_bls_mas as bls > where bls.year='1975') y1975 on (b.msa_code=y1975.msa_code and > b.sic=y1975.sic and b.own=y1975.own and b.ind_div=y1975.ind_div) >where msa_code in ('1680', '1640', '0040', '0120', '0080');