X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 33A2D475CEE for ; Thu, 10 Apr 2003 10:17:09 -0400 (EDT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by postgresql.org (Postfix) with ESMTP id 7D183475AE5 for ; Thu, 10 Apr 2003 10:17:08 -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 h3AEH92L003743; Thu, 10 Apr 2003 10:17:09 -0400 (EDT) To: joostje@komputilo.org Cc: pgsql-sql@postgresql.org Subject: Re: estimates for nested loop very wrong? In-reply-to: <20030410112144.GA22716@co.uea.org> References: <20030410112144.GA22716@co.uea.org> Comments: In-reply-to joostje@komputilo.org message dated "Thu, 10 Apr 2003 13:21:44 +0200" Date: Thu, 10 Apr 2003 10:17:09 -0400 Message-ID: <3742.1049984229@sss.pgh.pa.us> From: Tom Lane X-Spam-Status: No, hits=-32.5 required=5.0 tests=BAYES_01,EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT, REFERENCES,REPLY_WITH_QUOTES 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/159 X-Sequence-Number: 12860 joostje@komputilo.org writes: > When JOINing two tabels (one with 23 entries, one with 2.5e6 entries), > psql estimates the cost of the nested loop method way to high, causing > it to use Hash Join, even though Hash Join actually takes 30 seconds, > agianst 0.020 seconds for Nested Loop. Have you done an ANALYZE or VACUUM ANALYZE recently? > Nested Loop (cost=0.00..208256.60 rows=61140 width=38) (actual time=0.92..18.49 rows=756 loops=1) > -> Seq Scan on tmp1 (cost=0.00..1.23 rows=23 width=7) (actual time=0.24..0.39 rows=23 loops=1) > -> Index Scan using db_id_idx on db (cost=0.00..9021.35 rows=2658 width=31) (actual time=0.32..0.69 rows=33 loops=23) > Total runtime: 19.20 msec The planner is evidently estimating that each row of tmp1 will match 2600+ rows of db, whereas in reality there is only one match. Rather than mess with enable_hashjoin, you need to find out why that estimate is so badly off. Are the entries in tmp1 specially selected to correspond to unique rows of db? regards, tom lane