Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 0377F1337B8A for ; Wed, 8 Dec 2010 16:32:22 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 32816-09 for ; Wed, 8 Dec 2010 20:32:10 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from out-0.mx.aerioconnect.net (out-0-39.mx.aerioconnect.net [216.240.47.99]) by mail.postgresql.org (Postfix) with ESMTP id 342AB1337B83 for ; Wed, 8 Dec 2010 16:32:10 -0400 (AST) Received: from idiom.com (postfix@mx0.idiom.com [216.240.32.160]) by out-0.mx.aerioconnect.net (8.13.8/8.13.8) with ESMTP id oB8KW0NN022730; Wed, 8 Dec 2010 12:32:00 -0800 X-Client-Authorized: MaGic Cook1e X-Client-Authorized: MaGic Cook1e X-Client-Authorized: MaGic Cook1e Received: from [192.168.1.178] (75-149-45-155-sfba.hfc.comcastbusiness.net [75.149.45.155]) by idiom.com (Postfix) with ESMTP id 4A5442D6014; Wed, 8 Dec 2010 12:31:59 -0800 (PST) Message-ID: <4CFFEB39.1010601@obviously.com> Date: Wed, 08 Dec 2010 12:31:53 -0800 From: Bryce Nesbitt User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.15) Gecko/20101027 SeaMonkey/2.0.10 MIME-Version: 1.0 To: pgsql-performance@postgresql.org CC: Marc Mamin , Shrirang Chitnis Subject: Re: hashed subplan 5000x slower than two sequential operations References: <4CFFD446.6040609@obviously.com> In-Reply-To: Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.67 on 216.240.47.51 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.176 tagged_above=-10 required=5 tests=BAYES_00=-1.9, HTML_MESSAGE=0.001, MIME_HTML_ONLY=0.723 X-Spam-Level: X-Archive-Number: 201012/114 X-Sequence-Number: 41566 Marc Mamin wrote:
AW: [PERFORM] hashed subplan 5000x slower than two sequential operations

Hello,
are the table freshly analyzed, with a sufficient default_statistics_target ?


autovacuum = on                            # Enable autovacuum subprocess?  'on'
autovacuum_naptime = 5min         # time between autovacuum runs
default_statistics_target = 150       # range 1-1000



You may try to get a better plan while rewriting the query as an UNION to get rid of the OR clause.
Something like (not tested):

It is way better


EXPLAIN ANALYZE SELECT contexts.context_key
FROM contexts
    JOIN articles
    ON (articles.context_key=contexts.context_key)
WHERE (contexts.parent_key = 392210)                                                         
AND articles.indexed

UNION
SELECT collection_data.context_key
FROM collection_data
JOIN articles ON (articles.context_key=collection_data.context_key)
WHERE collection_data.collection_context_key = 392210
AND articles.indexed;

                                                                                 QUERY PLAN                                                                                
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Unique  (cost=418.50..418.61 rows=22 width=4) (actual time=0.582..0.671 rows=28 loops=1)
   ->  Sort  (cost=418.50..418.55 rows=22 width=4) (actual time=0.579..0.608 rows=28 loops=1)
         Sort Key: contexts.context_key
         Sort Method:  quicksort  Memory: 26kB
         ->  Append  (cost=0.00..418.01 rows=22 width=4) (actual time=0.042..0.524 rows=28 loops=1)
               ->  Nested Loop  (cost=0.00..376.46 rows=19 width=4) (actual time=0.040..0.423 rows=28 loops=1)
                     ->  Index Scan using parent_key_idx on contexts  (cost=0.00..115.20 rows=58 width=4) (actual time=0.021..0.082 rows=28 loops=1)
                           Index Cond: (parent_key = 392210)
                     ->  Index Scan using article_key_idx on articles  (cost=0.00..4.49 rows=1 width=4) (actual time=0.007..0.008 rows=1 loops=28)
                           Index Cond: (public.articles.context_key = contexts.context_key)
                           Filter: public.articles.indexed
               ->  Nested Loop  (cost=0.00..41.32 rows=3 width=4) (actual time=0.043..0.043 rows=0 loops=1)
                     ->  Index Scan using collection_data_context_key_index on collection_data  (cost=0.00..14.30 rows=6 width=4) (actual time=0.012..0.015 rows=3 loops=1)
                           Index Cond: (collection_context_key = 392210)
                     ->  Index Scan using article_key_idx on articles  (cost=0.00..4.49 rows=1 width=4) (actual time=0.006..0.006 rows=0 loops=3)
                           Index Cond: (public.articles.context_key = collection_data.context_key)
                           Filter: public.articles.indexed
 Total runtime: 0.812 ms