X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id CC25447689E for ; Fri, 16 May 2003 13:29:13 -0400 (EDT) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with ESMTP id 02687-01 for ; Fri, 16 May 2003 13:29:12 -0400 (EDT) Received: from anchor-post-35.mail.demon.net (anchor-post-35.mail.demon.net [194.217.242.85]) by postgresql.org (Postfix) with ESMTP id 2B9D5476B39 for ; Fri, 16 May 2003 13:29:11 -0400 (EDT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-35.mail.demon.net with esmtp (Exim 3.36 #2) id 19Gj13-000129-0Z; Fri, 16 May 2003 18:29:10 +0100 Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 571941682C; Fri, 16 May 2003 18:29:09 +0100 (BST) Received: from client.archonet.com (client.archonet.com [192.168.1.16]) by mainbox.archonet.com (Postfix) with ESMTP id B0F5916827; Fri, 16 May 2003 18:29:08 +0100 (BST) Content-Type: text/plain; charset="iso-8859-1" From: Richard Huxton Organization: Archonet Ltd To: Mike Winter , Subject: Re: Efficiency Issue with "SELECT IN (..)" and "SELECT .. OR .." Date: Fri, 16 May 2003 18:29:09 +0100 User-Agent: KMail/1.4.3 References: In-Reply-To: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200305161829.09631.dev@archonet.com> X-Virus-Scanned: by AMaViS snapshot-20020531 X-Virus-Scanned: by amavisd-new X-Archive-Number: 200305/352 X-Sequence-Number: 13517 On Thursday 15 May 2003 10:44 pm, Mike Winter wrote: > Hi, when doing queries of the type: > > SELECT id FROM foo WHERE id IN (1, 4, 3, 2, 10, 11, 14) .., I get > terrible performance on tables of any resonable size. I see the > same behaviour when doing queries of the form "SELECT id FROM > foo WHERE id =3D 5 OR id =3D 6 OR ..." [snip] > It looks to me like the query parser is recursively calling > an index scan for each row in the 'IN' clause rather than just > doing one index scan that it seems it should be. Hmm - not sure how you could. When it says index-scan it's actually travers= ing=20 a btree (probably), not scanning a list of indexes. The IN is basically=20 treated like a series of a OR b OR c, hence the similar behaviour. > My question is, does anyone have any alternate ideas for how I > can do a query like this and have it perform well? The tables I > am working with are big enough that a sequential scan is not > helpful. Is this a bug I am encountering or an error in my > query? Is this a known issue? Known issue - the usual advice is to rewrite in the form of EXISTS, but I= =20 can't think how to do that if you have a long list of literal values. You= =20 could create a temp table to hold your matching values and join against it,= =20 but I realise that's not a terribly elegant solution. Unless of course, it'= s=20 a search-engine type of situation where it makes a certain amount of sense. > I have seen this beahaviour on 7.2.1 and 7.3.2 on both Solaris > and Linux platforms. Supposed to be some improvements in the forthcoming 7.4 but I don't know if= =20 that will help your particular case. --=20 Richard Huxton