Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 6B44A6501AD for ; Wed, 5 Nov 2008 16:37:34 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 61412-01 for ; Wed, 5 Nov 2008 16:37:34 -0400 (AST) X-Greylist: delayed 00:36:47.68983 by SQLgrey-1.7.6 Received: from wa-out-1112.google.com (wa-out-1112.google.com [209.85.146.178]) by mail.postgresql.org (Postfix) with ESMTP id 0575364FC9C for ; Wed, 5 Nov 2008 16:37:27 -0400 (AST) Received: by wa-out-1112.google.com with SMTP id j4so100901wah.19 for ; Wed, 05 Nov 2008 12:37:27 -0800 (PST) Received: by 10.115.14.1 with SMTP id r1mr12197wai.171.1225915238551; Wed, 05 Nov 2008 12:00:38 -0800 (PST) Received: by 10.142.80.14 with HTTP; Wed, 5 Nov 2008 12:00:38 -0800 (PST) Message-ID: Date: Wed, 5 Nov 2008 12:00:38 -0800 From: "Scott Carey" To: "pgsql-performance@postgresql.org" Subject: Query planner cost estimate less than the sum of its parts? MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_15902_5790430.1225915238558" X-Archive-Number: 200811/13 X-Sequence-Number: 31744 ------=_Part_15902_5790430.1225915238558 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline So, we had a query run accidentally without going through the right checks to ensure that it had the right limits in a where clause for our table partitioning, resulting in an attempt to scan TB's of data. Obviously, we fixed the query, but the curious result is this explain plan (shortened, in full form its ~3500 lines long). A true cost estimate of ~ 4 million isn't a big deal on this server. It is plainly wrong... wouldn't a nested loop of this sort estimate at least 128266*4100644 for the cost? Or something on that order of magnitude? Certainly, a cost estimate that is ... LESS than one of the sub sections of the query is wrong. This was one hell of a broken query, but it at least should have taken an approach that was not a nested loop, and I'm curious if that choice was due to a bad estimate here. Nested Loop IN Join (cost=0.00..3850831.86 rows=128266 width=8) Join Filter: ((log.p_p_logs.s_id)::text = (log.s_r_logs.s_id)::text) -> Append (cost=0.00..6078.99 rows=128266 width=46) -> Seq Scan on p_p_logs (cost=0.00..1.01 rows=1 width=14) Filter: ((date >= '2008-10-27'::date) AND (sector = 12)) -> Seq Scan on p_p_logs_012_2008_10_27 p_p_logs (cost=0.00..718.22 rows=15148 width=46) Filter: ((date >= '2008-10-27'::date) AND (sector = 12)) [ Snipped ~ 10 more tables] -> Append (cost=0.00..4100644.78 rows=29850181 width=118) -> Seq Scan on s_r_logs (cost=0.00..1.01 rows=1 width=14) Filter: log.s_r_logs.source -> Seq Scan on s_r_logs_002_2008_10_01 s_r_logs (cost=0.00..91.00 rows=1050 width=33) Filter: p_log.s_r_logs.source -> Seq Scan on s_r_logs_002_2008_10_02 s_r_logs (cost=0.00..65.00 rows=750 width=33) [ Snipped ~1500 tables of various sizes ] ------=_Part_15902_5790430.1225915238558 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline So, we had a query run accidentally without going through the right checks to ensure that it had the right limits in a where clause for our table partitioning, resulting in an attempt to scan TB's of data.

Obviously, we fixed the query, but the curious result is this explain plan (shortened, in full form its ~3500 lines long).  A true cost estimate of ~ 4 million isn't a big deal on this server.  It is plainly wrong...  wouldn't a nested loop of this sort estimate at least 128266*4100644 for the cost?  Or something on that order of magnitude? 
Certainly, a cost estimate that is ... LESS than one of the sub sections of the query is wrong.   This was one hell of a broken query, but it at least should have taken an approach that was not a nested loop, and I'm curious if that choice was due to a bad estimate here. 

Nested Loop IN Join  (cost=0.00..3850831.86 rows=128266 width=8)
   Join Filter: ((log.p_p_logs.s_id)::text = (log.s_r_logs.s_id)::text)
   ->  Append  (cost=0.00..6078.99 rows=128266 width=46)
         ->  Seq Scan on p_p_logs  (cost=0.00..1.01 rows=1 width=14)
               Filter: ((date >= '2008-10-27'::date) AND (sector = 12))
         ->  Seq Scan on p_p_logs_012_2008_10_27 p_p_logs  (cost=0.00..718.22 rows=15148 width=46)
               Filter: ((date >= '2008-10-27'::date) AND (sector = 12))
      [ Snipped ~ 10 more tables]

   ->  Append  (cost=0.00..4100644.78 rows=29850181 width=118)
         ->  Seq Scan on s_r_logs  (cost=0.00..1.01 rows=1 width=14)
               Filter: log.s_r_logs.source
         ->  Seq Scan on s_r_logs_002_2008_10_01 s_r_logs  (cost=0.00..91.00 rows=1050 width=33)
               Filter: p_log.s_r_logs.source
         ->  Seq Scan on s_r_logs_002_2008_10_02 s_r_logs  (cost=0.00..65.00 rows=750 width=33)
      [ Snipped ~1500 tables of various sizes ]
------=_Part_15902_5790430.1225915238558--