X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 8B439D1B195 for ; Mon, 5 Jul 2004 20:14:41 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 72520-08 for ; Mon, 5 Jul 2004 23:14:35 +0000 (GMT) Received: from web50708.mail.yahoo.com (web50708.mail.yahoo.com [206.190.38.106]) by svr1.postgresql.org (Postfix) with SMTP id 66130D1B1AB for ; Mon, 5 Jul 2004 20:14:31 -0300 (ADT) Message-ID: <20040705231434.81471.qmail@web50708.mail.yahoo.com> Received: from [69.138.204.74] by web50708.mail.yahoo.com via HTTP; Mon, 05 Jul 2004 16:14:34 PDT Date: Mon, 5 Jul 2004 16:14:34 -0700 (PDT) From: Eugene Subject: Forcing HashAggregation prior to index scan? To: pgsql-performance@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.1 tagged_above=0.0 required=5.0 tests=RCVD_IN_SORBS X-Spam-Level: X-Archive-Number: 200407/22 X-Sequence-Number: 7413 I have a very simple problem. I run two select statments, they are identical except for a single where condition. The first select statment runs in 9 ms, while the second statment runs for 4000 ms SQL1 - fast 9ms explain analyse select seq_ac from refseq_sequence S where seq_ac in (select seq_ac2 from refseq_refseq_hits where seq_ac1 = 'NP_001217') SQL2 - very slow 4000ms explain analyse select seq_ac from refseq_sequence S where seq_ac in (select seq_ac2 from refseq_refseq_hits where seq_ac1 = 'NP_001217') AND S.species = 'Homo sapiens' I think the second sql statment is slower than the first one because planner is not using HashAggregate. Can I force HashAggregation before index scan? Here is the full output from EXPLAIN ANALYZE explain analyse select seq_ac from refseq_sequence S where seq_ac in (select seq_ac2 from refseq_refseq_hits where seq_ac1 = 'NP_001217'); QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------- Nested Loop (cost=169907.83..169919.88 rows=3 width=24) (actual time=1.450..8.707 rows=53 loops=1) -> HashAggregate (cost=169907.83..169907.83 rows=2 width=19) (actual time=1.192..1.876 rows=53 loops=1) -> Index Scan using refseq_refseq_hits_pkey on refseq_refseq_hits (cost=0.00..169801.33 rows=42600 width=19) (actual time=0.140..0.894 rows=54 loops=1) Index Cond: ((seq_ac1)::text = 'NP_001217'::text) -> Index Scan using refseq_sequence_pkey on refseq_sequence s (cost=0.00..6.01 rows=1 width=24) (actual time=0.105..0.111 rows=1 loops=53) Index Cond: ((s.seq_ac)::text = ("outer".seq_ac2)::text) Total runtime: 9.110 ms explain analyse select seq_ac from refseq_sequence S where seq_ac in (select seq_ac2 from refseq_refseq_hits where seq_ac1 = 'NP_001217') and S.species = 'Homo sapiens'; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------- Nested Loop IN Join (cost=0.00..4111.66 rows=1 width=24) (actual time=504.176..3857.340 rows=30 loops=1) -> Index Scan using refseq_sequence_key2 on refseq_sequence s (cost=0.00..1516.06 rows=389 width=24) (actual time=0.352..491.107 rows=27391 loops=1) Index Cond: ((species)::text = 'Homo sapiens'::text) -> Index Scan using refseq_refseq_hits_pkey on refseq_refseq_hits (cost=0.00..858.14 rows=213 width=19) (actual time=0.114..0.114 rows=0 loops=27391) Index Cond: (((refseq_refseq_hits.seq_ac1)::text = 'NP_001217'::text) AND (("outer".seq_ac)::text = (refseq_refseq_hits.seq_ac2)::text)) Total runtime: 3857.636 ms