Received: from mail.postgresql.org (webmail.postgresql.org [216.126.85.28]) by mail.postgresql.org (8.11.1/8.11.1) with SMTP id f0II4O079360 for ; Thu, 18 Jan 2001 13:04:24 -0500 (EST) (envelope-from pgsql-general-owner@postgresql.org) Received: from magenta.timewarp.co.uk (ns2.timewarp.co.uk [195.188.56.7]) by mail.postgresql.org (8.11.1/8.11.1) with SMTP id f0HJU6054543; Wed, 17 Jan 2001 14:30:06 -0500 (EST) (envelope-from ken@kencorey.com) Received: from [195.167.153.42] by magenta.timewarp.co.uk (NTMail 5.06.0016/NT9060.00.3234d82d) with ESMTP id flunhaaa for pgsql-sql@postgresql.org; Wed, 17 Jan 2001 19:26:44 +0000 Message-ID: <3A65F3AD.7FDA0A49@kencorey.com> Date: Wed, 17 Jan 2001 19:34:05 +0000 From: Ken Corey Reply-To: ken@kencorey.com X-Mailer: Mozilla 4.75 [en] (X11; U; Linux 2.2.15-4mdk i686) X-Accept-Language: en MIME-Version: 1.0 To: "Diehl, Jeffrey" CC: pgsql-sql@postgresql.org, pgsql-general@postgresql.org Subject: Re: Help with query. (*) References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Archive-Number: 200101/844 X-Sequence-Number: 3048 [NOTE: I'm a pgsql newbie myself. Take this reply with a large-ish grain of salt!) Shouldn't it be something straightforward like: select a.a, a.b, a.c, ... from a a, b b where a.x = b.x, and a.y = b.y, ... (I'd watch out for too many clauses here... if you've got a lot of clauses, you're probably not normalized as much as you should be.) If you have indexes on the relevant fields, you shouldn't get a table scan and this should return rather quickly, right? -Ken "Diehl, Jeffrey" wrote: > > I'm having difficulty writing a query which I really can't live without... > > I need to get a list of records from table A for which there are > corresponding records in table B. I've tried to use the intersect clause, > but it doesn't seem to work, or it runs far too long. For example: > > select * from A > where 1=1 > intersect select * from A where > B.x=A.x > and A.y=B.y > and A.z=B.z > limit 100 > > I need the most efficient method possible; my A tables have upward of 5 > Million records. The B table, btw, only has about 100 records. > > Any help will be most appreciated.