Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TPGH9-0003QA-1B for pgsql-performance@postgresql.org; Fri, 19 Oct 2012 17:22:23 +0000 Received: from plane.gmane.org ([80.91.229.3]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TPGH3-0001Hy-7m for pgsql-performance@postgresql.org; Fri, 19 Oct 2012 17:22:22 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1TPGH5-0001u7-R8 for pgsql-performance@postgresql.org; Fri, 19 Oct 2012 19:22:19 +0200 Received: from ppp-188-174-150-157.dynamic.mnet-online.de ([188.174.150.157]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 19 Oct 2012 19:22:19 +0200 Received: from spam_eater by ppp-188-174-150-157.dynamic.mnet-online.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 19 Oct 2012 19:22:19 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-performance@postgresql.org From: Thomas Kellerer Subject: Re: Recursive query gets slower when adding an index Date: Fri, 19 Oct 2012 19:22:05 +0200 Lines: 32 Message-ID: References: <18218.1350656456@sss.pgh.pa.us> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: ppp-188-174-150-157.dynamic.mnet-online.de User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.21) Gecko/20090302 Thunderbird/2.0.0.21 Mnenhy/0.7.5.666 In-Reply-To: <18218.1350656456@sss.pgh.pa.us> X-Pg-Spam-Score: -2.3 (--) X-Archive-Number: 201210/277 X-Sequence-Number: 48236 Tom Lane wrote on 19.10.2012 16:20: > Thomas Kellerer writes: >> This is the execution plan without index: http://explain.depesz.com/s/ecCT >> When I create an index on parent_id execution time increases to something between 110ms and 130ms >> This is the execution plan with index: http://explain.depesz.com/s/xiL > > The reason you get a bad plan choice here is the severe underestimate of > the average number of rows coming out of the worktable scan (ie, the > size of the "recursive" result carried forward in each iteration). > > Unfortunately, it's really hard to see how we might make that number > better. The current rule of thumb is "10 times the size of the > nonrecursive term", which is why you get 10 here. We could choose > another multiplier but it'd be just as bogus as the current one > (unless somebody has some evidence about typical expansion factors?) > > I suppose though that there's some argument for discouraging the planner > from assuming that the carried-forward result is small; so maybe we > should use something larger than 10. > Thanks for the feedback. I just noticed this behaviour because we ran the same query on SQL Server 2008 and that took well over 30seconds without the index SQL Server *really* improved with the index and returned the result in 0.5 seconds whith the index in place. So I was curious how much faster Postgres would be *with* the index ;) Regards Thomas