Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TQklz-0004Es-ED for pgsql-performance@postgresql.org; Tue, 23 Oct 2012 20:08:23 +0000 Received: from cerberus.pinpointresearch.com ([66.7.238.130] helo=polaris.pinpointresearch.com) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TQklt-0000GU-U6 for pgsql-performance@postgresql.org; Tue, 23 Oct 2012 20:08:22 +0000 Received: from [192.168.1.179] (betelgeuse.pinpointresearch.com [192.168.1.179]) by polaris.pinpointresearch.com (Postfix) with ESMTP id BDD97E00EC82; Tue, 23 Oct 2012 13:08:15 -0700 (PDT) Message-ID: <5086F92F.50406@pinpointresearch.com> Date: Tue, 23 Oct 2012 13:08:15 -0700 From: Steve Crawford User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 MIME-Version: 1.0 To: Kevin Grittner CC: henk de wit , pgsql-performance@postgresql.org Subject: Re: Query with limit goes from few ms to hours References: <20121023183316.179560@gmx.com> In-Reply-To: <20121023183316.179560@gmx.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201210/294 X-Sequence-Number: 48253 On 10/23/2012 11:33 AM, Kevin Grittner wrote: > henk de wit wrote: > >> Well, what do you know! That did work indeed. Immediately after the >> ANALYZE on that parent table (taking only a few seconds) a fast >> plan was created and the query executed in ms again. Silly me, I >> should have tried that earlier. > Of course, if your autovacuum settings are aggressive enough, you > should gernerally not need to run ANALYZE explicitly. You should > double-check that autovacuum is turned on and configured at least as > aggressively as the default settings, or you will probably get little > surprises like this when you least expect them. > > The exception I'd make to Kevin's good advice is for cases when a process makes substantial statistics-altering changes to your data (bulk insert/delete/update) immediately followed by a query against the updated table(s). In those cases there is a good possibility that the statistics will not have been automatically updated before the subsequent query is planned so an explicit ANALYZE between the update and the query can be of value. Cheers, Steve