X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 5B9AA4762E1 for ; Fri, 11 Apr 2003 18:12:13 -0400 (EDT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by postgresql.org (Postfix) with ESMTP id E5CB1475F1B for ; Fri, 11 Apr 2003 18:11:56 -0400 (EDT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.9/8.12.9) with ESMTP id h3BMBw2L014253; Fri, 11 Apr 2003 18:11:58 -0400 (EDT) To: Itai Zukerman Cc: pgsql-sql@postgresql.org Subject: Re: count(*), EXISTS, indexes In-reply-to: <87vfxkrap2.fsf@matt.w80.math-hat.com> References: <874r54sr8q.fsf@matt.w80.math-hat.com> <13918.1050095452@sss.pgh.pa.us> <87vfxkrap2.fsf@matt.w80.math-hat.com> Comments: In-reply-to Itai Zukerman message dated "Fri, 11 Apr 2003 17:22:33 -0400" Date: Fri, 11 Apr 2003 18:11:58 -0400 Message-ID: <14252.1050099118@sss.pgh.pa.us> From: Tom Lane X-Spam-Status: No, hits=-31.7 required=5.0 tests=BAYES_00,EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT, REFERENCES,REPLY_WITH_QUOTES,UPPERCASE_25_50 autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200304/191 X-Sequence-Number: 12892 Itai Zukerman writes: >>> I'd like to calculate: >>> SELECT sum(v) FROM A WHERE EXISTS (SELECT 1 FROM B WHERE A.x=B.x); >>> ...but then it won't use the primary key index on A.x. >> >> In CVS tip (7.4-to-be) I think >> SELECT sum(v) FROM A WHERE A.x IN (SELECT B.x FROM B); > Oops. I guess I should've changed the example to "WHERE A.x>=B.x" > since that's what I'm really doing. Oh, well, my goof. Anything else you didn't bother to mention? Because in that case I think your query reduces to SELECT sum(v) FROM A WHERE A.x >= (SELECT min(x) FROM B); which ought to run reasonably well (I'm sure it'll still want to do a seqscan --- but not a nested loop). regards, tom lane