Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 006514761D4 for ; Wed, 9 Oct 2002 13:26:36 -0400 (EDT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id 9A26A475F25 for ; Wed, 9 Oct 2002 13:26:31 -0400 (EDT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 52D1FD60E; Wed, 9 Oct 2002 10:26:31 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 386035C03; Wed, 9 Oct 2002 10:26:31 -0700 (PDT) Date: Wed, 9 Oct 2002 10:26:31 -0700 (PDT) From: Stephan Szabo To: John Pauley Cc: Subject: Re: SELECT statement never completes. In-Reply-To: <20021009171021.25211.qmail@web13408.mail.yahoo.com> Message-ID: <20021009102456.H4728-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200210/126 X-Sequence-Number: 9843 On Wed, 9 Oct 2002, John Pauley wrote: > pgsql-sql, > > We are porting a database from IBM DB2 to PostgreSQL. > In several related scripts, there is a SELECT > statement that never completes in Postgres but > completes in a few seconds using DB2, for example: > > Table row count: > SELECT count(*) FROM tableX; > 112671 > SELECT count(*) from tableY; > 314625 > > This statement does not complete: > SELECT id FROM tableX WHERE id NOT IN (SELECT id FROM > tableY); > > Any suggestions? Unfortunately IN tends to have poor performance in postgresql. Often you can get better performance out of exists, but not always. You might want to try: select id from tableX WHERE NOT EXISTS (select * from tableY where tableY.id=tableX.id); and see if it runs better.