Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 3B80C632A15 for ; Wed, 10 Mar 2010 11:44:34 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 04967-09 for ; Wed, 10 Mar 2010 15:44:14 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 7598063333A for ; Wed, 10 Mar 2010 11:44:23 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id o2AFiKlR015948; Wed, 10 Mar 2010 10:44:20 -0500 (EST) To: Benoit Delbosc cc: pgsql-performance@postgresql.org Subject: Re: Bad query plan inside EXISTS clause In-reply-to: <4B979DFC.7080509@nuxeo.com> References: <4B979DFC.7080509@nuxeo.com> Comments: In-reply-to Benoit Delbosc message dated "Wed, 10 Mar 2010 14:26:20 +0100" Date: Wed, 10 Mar 2010 10:44:20 -0500 Message-ID: <15947.1268235860@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.455 tagged_above=-10 required=5 tests=AWL=0.144, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201003/114 X-Sequence-Number: 37939 Benoit Delbosc writes: > I am trying to understand why inside an EXISTS clause the query planner > does not use the index: I'm not sure this plan is as bad as all that. The key point is that the planner is expecting 52517 rows that match that users_md5 value (and the true number is evidently 51446, so that estimate isn't far off). That's about 1/48th of the table. It knows that the EXISTS case can stop as soon as it finds one match, so it's betting that a plain seqscan will hit a match faster than an index lookup would be able to, ie, seqscanning about 48 tuples is faster than one index lookup. This might be a bad bet if the users_md5 values are correlated with physical order, ie the matches are not randomly scattered but are all towards the end of the table. Barring that, though, it could be a good bet if the table isn't swapped in. Which is what the default cost parameters are set up to assume. I suspect your real complaint is that you expect the table to be swapped in, in which case what you ought to be doing is adjusting the planner's cost parameters. Some playing around here with a similar case suggests that even a small reduction in random_page_cost would make it prefer an indexscan for this type of situation. regards, tom lane