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 8DDFF54A31 for ; Wed, 11 May 2005 11:18:47 -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 18348-02 for ; Wed, 11 May 2005 14:18:39 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 7562E533A0 for ; Wed, 11 May 2005 11:18:37 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id j4BEIY1L003743; Wed, 11 May 2005 10:18:35 -0400 (EDT) To: "Edin Kadribasic" Cc: pgsql-performance@postgresql.org Subject: Re: Optimizer wrongly picks Nested Loop Left Join In-reply-to: <001f01c55604$494d2e60$1200000a@intra.emini.dk> References: <001f01c55604$494d2e60$1200000a@intra.emini.dk> Comments: In-reply-to "Edin Kadribasic" message dated "Wed, 11 May 2005 10:34:45 +0200" Date: Wed, 11 May 2005 10:18:34 -0400 Message-ID: <3742.1115821114@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.007 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200505/206 X-Sequence-Number: 12370 "Edin Kadribasic" writes: > I have a query that is giving the optimizer (and me) great headache. The main problem seems to be that the rowcount estimates for axp_temp_order_match and axp_dayschedule are way off: > -> Index Scan using axp_temp_order_match_idx1 on > axp_temp_order_match a (cost=0.00..209.65 rows=426 width=4) (actual > time=0.277..0.512 rows=6 loops=1) > Index Cond: (sid = 16072) > -> Index Scan using axp_dayschedule_day_idx on > axp_dayschedule ds (cost=0.00..3.02 rows=1 width=8) (actual > time=0.036..3.973 rows=610 loops=1) > Index Cond: (("day" >= '2005-05-12'::date) > AND ("day" <= '2005-05-12'::date)) > Filter: (used = B'1'::"bit") > -> Index Scan using axp_temp_order_match_idx1 on > axp_temp_order_match a (cost=0.00..2.45 rows=1 width=4) (actual > time=0.027..2.980 rows=471 loops=1) > Index Cond: (sid = 16092) > -> Index Scan using axp_dayschedule_day_idx on > axp_dayschedule ds (cost=0.00..3.02 rows=1 width=8) (actual > time=0.015..3.557 rows=606 loops=471) > Index Cond: (("day" >= '2005-05-13'::date) AND > ("day" <= '2005-05-13'::date)) > Filter: (used = B'1'::"bit") Do you ANALYZE these tables on a regular basis? If so, it may be necessary to increase the statistics target to the point where you get better estimates. > Please note that sometimes when I get "bad plan" in the logfile, I just > re-run the query and the optimizer chooses the more efficient one. That's fairly hard to believe, unless you've got autovacuum running in the background. regards, tom lane