Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 5D2892E0242 for ; Tue, 18 Mar 2008 12:42:14 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 63608-09 for ; Tue, 18 Mar 2008 12:41:56 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from wr-out-0506.google.com (wr-out-0506.google.com [64.233.184.230]) by postgresql.org (Postfix) with ESMTP id 5556C2E0221 for ; Tue, 18 Mar 2008 12:41:54 -0300 (ADT) Received: by wr-out-0506.google.com with SMTP id 76so4278163wra.10 for ; Tue, 18 Mar 2008 08:41:52 -0700 (PDT) Received: by 10.141.96.21 with SMTP id y21mr873510rvl.132.1205854508564; Tue, 18 Mar 2008 08:35:08 -0700 (PDT) Received: by 10.140.163.11 with HTTP; Tue, 18 Mar 2008 08:35:08 -0700 (PDT) Message-ID: <3642025c0803180835n20062314g94870b137d8c3ff7@mail.gmail.com> Date: Tue, 18 Mar 2008 11:35:08 -0400 From: "Chris Kratz" To: pgsql-performance@postgresql.org Subject: Planner mis-estimation using nested loops followup MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_4843_7095543.1205854508554" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200803/372 X-Sequence-Number: 29326 ------=_Part_4843_7095543.1205854508554 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline A number of weeks ago, I had posted a request for help regarding join estimates in pg 8.2.6. In moderately complex to very complex ad hoc queries in our system, we were consistently having the system massively underestimate the number of rows coming out of join at a low level making these queries very slow and inefficient. At times the mis-estimation was 1000:1. Ie when it should have been 2000 returned rows from a join, the planner assumed 1 or 2 rows. Modifying stats on the join columns up to the max made little difference (y, we analyzed tables in question after each change). Since the planner sees only one row coming out of the low level join, it uses nested loops all the way up chain when it would be more efficient to use another join type. In our informal testing, we found that by disabling nested loops and forcing other join types, we could get fantastic speedups. Those queries that seem to benefit most from this have a lot of sub-queries being built up into a final query set as well as a fair number of joins in the sub-queries. Since these are user created and are then generated via our tools, they can be quite messy at times. After doing this testing, have since added some functionality in our ad hoc reporting tool to allow us to tune individual queries by turning on and off individual join types at runtime. As we hear of slow reports, we've been individually turning off the nested loops on those reports. Almost always, this has increased the performance of the reports, sometimes in a completely amazing fashion (many, many minutes to seconds at times). It of course doesn't help everything and turning off nested loops in general causes overall slowdown in other parts of the system. As this has gone on over the last couple of weeks, it feels like we either have a misconfiguration on the server, or we are tickling a mis-estimation bug in the planner. I'm hoping it's the former. The db server has 8G of memory and raid1 -wal, raid10- data configuration, os is linux 2.6.9, db is 8.2.6. The db is a utf-8 db if that is of any bearing and autovac and bgwriter are on. Nondefault settings of interest from postgresql.conf shared_buffers = 1024MB # min 128kB or max_connections*16kB work_mem = 256MB # min 64kB maintenance_work_mem = 256MB # min 1MB random_page_cost = 1.75 # same scale as above effective_cache_size = 4096MB default_statistics_target = 100 # range 1-1000 If nothing else, perhaps this will help somebody else who has run into the same problem. If explain analyze of a query shows a large mis-estimation of rows returned on a join (estimate=1, actual=2k) causing the planner to choose nested loops instead of another join type, you might try running the query with nested loops set to off and see if that helps w/ performance. Thanks, -Chris ------=_Part_4843_7095543.1205854508554 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline A number of weeks ago, I had posted a request for help regarding join estim= ates in pg 8.2.6.  In moderately complex to very complex ad hoc querie= s in our system, we were consistently having the system massively underesti= mate the number of rows coming out of join at a low level making these quer= ies very slow and inefficient.  At times the mis-estimation = was 1000:1.  Ie when it should have been 2000 returned rows from a joi= n, the planner assumed 1 or 2 rows.  Modifying stats on the join colum= ns up to the max made little difference (y, we analyzed tables in question = after each change).  Since the planner sees only one row coming out of= the low level join, it uses nested loops all the way up chain when it woul= d be more efficient to use another join type.  In our informal testing= , we found that by disabling nested loops and forcing other join types, we = could get fantastic speedups.  Those queries that seem to benefit most= from this have a lot of sub-queries being built up into a final = query set as well as a fair number of joins in the sub-queries. &= nbsp;Since these are user created and are then generated via our tools, the= y can be quite messy at times.  

After doing this testing,= have since added some functionality in our ad hoc reporting tool to allow = us to tune individual queries by turning on and off individual join types a= t runtime.  As we hear of slow reports, we've been individually tu= rning off the nested loops on those reports.  Almost always, this has = increased the performance of the reports, sometimes in a completely amazing= fashion (many, many minutes to seconds at times).  It of course doesn= 't help everything and turning off nested loops in general causes overa= ll slowdown in other parts of the system.

As this has gone on = over the last couple of weeks, it feels like we either have a misconfigurat= ion on the server, or we are tickling a mis-estimation bug in the planner. =  I'm hoping it's the former.  The db server has 8G of mem= ory and raid1 -wal, raid10- data configuration, os is linux 2.6.9, db is 8.= 2.6.  The db is a utf-8 db if that is of any bearing and autovac and b= gwriter are on.

Nondefault settings = of interest from postgresql.conf

 
s= hared_buffers =3D 1024MB               &= nbsp; # min 128kB or max_connections*16kB
work_mem =3D 256MB               &n= bsp;                # min 64kB
maintenance_work_mem =3D 256MB           &nb= sp;# min 1MB
random_page_cost =3D 1.75        = ;         # same scale as above
effective_cac= he_size =3D 4096MB
default_statistics_target =3D 100         # r= ange 1-1000

 
If nothing else, perhaps t= his will help somebody else who has run into the same problem.  If exp= lain analyze of a query shows a large mis-estimation of rows returned on a = join (estimate=3D1, actual=3D2k) causing the planner to choose nested loops= instead of another join type, you might try running the query with nested = loops set to off and see if that helps w/ performance.

Thanks,
-Chris
------=_Part_4843_7095543.1205854508554--