Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id AF2A4930E1E for ; Fri, 3 Aug 2012 11:33:53 -0300 (ADT) Received: from gwmta.wicourts.gov ([165.219.244.99] helo=gw.wicourts.gov) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SxIwq-0003Jx-6V for pgsql-performance@postgresql.org; Fri, 03 Aug 2012 14:33:53 +0000 Received: from Courts-MTA by gw.wicourts.gov with Novell_GroupWise; Fri, 03 Aug 2012 09:33:39 -0500 Message-Id: <501B9AEB02000025000493CD@gw.wicourts.gov> X-Mailer: Novell GroupWise Internet Agent 8.0.2 Date: Fri, 03 Aug 2012 09:33:31 -0500 From: "Kevin Grittner" To: "Russell Keane" , "pgsql-performance@postgresql.org" Subject: Re: query using incorrect index References: <8D0E5D045E36124A8F1DDDB463D548557CEF4D2F7D@mxsvr1.is.inps.co.uk> <501A98FD020000250004935D@gw.wicourts.gov> <8D0E5D045E36124A8F1DDDB463D548557CEF4D2FAD@mxsvr1.is.inps.co.uk> In-Reply-To: <8D0E5D045E36124A8F1DDDB463D548557CEF4D2FAD@mxsvr1.is.inps.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Pg-Spam-Score: -4.2 (----) X-Archive-Number: 201208/9 X-Sequence-Number: 47547 Russell Keane wrote: > "log_min_duration_statement";"1ms" > "shared_buffers";"32MB" > "work_mem";"1MB" Those are pretty low values even for a 4GB machine. I suggest the following changes and additions, based on the fact that you seem to have the active portion of the database fully cached. shared_buffers = '160MB' work_mem = '8MB' seq_page_cost = 0.1 random_page_cost = 0.1 cpu_tuple_cost = 0.03 effective_cache_size = '2GB' > Explain analyse with both indexes present but without the limit > (uses the correct index): > "Total runtime: 0.092 ms" Part of problem is that it thinks it will find a matching row fairly quickly, and having done so using the index it chose will mean it is the *right* row. The problem is that there are no matching rows, so it has to scan the entire index. More fine-grained statistics *might* help. If other techniques don't help, you can rewrite the query slightly to create an optimization fence, but that should be a last resort. I agree with Robert that if you have a lot of queries that select on "incoming" and/or "inactive", a conditional index (with a WHERE clause in its definition) is likely to be very helpful. -Kevin