Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 9EC97634660 for ; Wed, 10 Mar 2010 09:33:34 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 27442-02-2 for ; Wed, 10 Mar 2010 13:33:23 +0000 (UTC) X-Greylist: delayed 00:06:52.765143 by SQLgrey-1.7.6 Received: from mail-ew0-f227.google.com (mail-ew0-f227.google.com [209.85.219.227]) by mail.postgresql.org (Postfix) with ESMTP id 60E91635A2B for ; Wed, 10 Mar 2010 09:33:17 -0400 (AST) Received: by ewy27 with SMTP id 27so3913053ewy.34 for ; Wed, 10 Mar 2010 05:33:15 -0800 (PST) Received: by 10.213.39.140 with SMTP id g12mr4972963ebe.48.1268227582431; Wed, 10 Mar 2010 05:26:22 -0800 (PST) Received: from [192.168.1.100] (hau59-1-87-90-41-82.dsl.club-internet.fr [87.90.41.82]) by mx.google.com with ESMTPS id 14sm3842475ewy.10.2010.03.10.05.26.21 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 10 Mar 2010 05:26:21 -0800 (PST) Message-ID: <4B979DFC.7080509@nuxeo.com> Date: Wed, 10 Mar 2010 14:26:20 +0100 From: Benoit Delbosc User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Bad query plan inside EXISTS clause Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201003/108 X-Sequence-Number: 37933 Hi all, I am trying to understand why inside an EXISTS clause the query planner does not use the index: EXPLAIN ANALYZE SELECT 1 WHERE EXISTS (SELECT 1 FROM read_acls_cache WHERE users_md5 = '9bc9012eb29c0bb2ae3cc7b5e78c2acf'); QUERY PLAN -------------------------------------------------------------------------------------------- Result (cost=1.19..1.20 rows=1 width=0) (actual time=466.317..466.318 rows=1 loops=1) One-Time Filter: $0 InitPlan 1 (returns $0) -> Seq Scan on read_acls_cache (cost=0.00..62637.01 rows=52517 width=0) (actual time=466.309..466.309 rows=1 loops=1) Filter: ((users_md5)::text = '9bc9012eb29c0bb2ae3cc7b5e78c2acf'::text) Total runtime: 466.369 ms (6 rows) While it does use the index when executing only the subquery: EXPLAIN ANALYZE SELECT 1 FROM read_acls_cache WHERE users_md5 = '9bc9012eb29c0bb2ae3cc7b5e78c2acf'; QUERY PLAN -------------------------------------------------------------------------- Bitmap Heap Scan on read_acls_cache (cost=2176.10..35022.98 rows=52517 width=0) (actual time=9.065..21.988 rows=51446 loops=1) Recheck Cond: ((users_md5)::text = '9bc9012eb29c0bb2ae3cc7b5e78c2acf'::text) -> Bitmap Index Scan on read_acls_cache_users_md5_idx (cost=0.00..2162.97 rows=52517 width=0) (actual time=8.900..8.900 rows=51446 loops=1) Index Cond: ((users_md5)::text = '9bc9012eb29c0bb2ae3cc7b5e78c2acf'::text) Total runtime: 25.464 ms (5 rows) The table has been vacuumed, analyzed and reindexed. Thanks for your support. Regards ben Here are some more info : \d read_acls_cache Table "public.read_acls_cache" Column | Type | Modifiers -----------+-----------------------+----------- users_md5 | character varying(34) | not null acl_id | character varying(34) | Indexes: "read_acls_cache_users_md5_idx" btree (users_md5) SELECT COUNT(*) FROM read_acls_cache; count --------- 2520899 (1 row) SELECT COUNT(DISTINCT(users_md5)) FROM read_acls_cache ; count ------- 49 (1 row) SELECT Version(); version ------------------------------------------------------------------ PostgreSQL 8.4.2 on x86_64-pc-linux-gnu, compiled by GCC gcc-4.2.real (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu4), 64 (1 row)