Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id F38D42E023E for ; Tue, 27 Nov 2007 19:38:40 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 29419-01 for ; Tue, 27 Nov 2007 19:38:27 -0400 (AST) Received: from cassarossa.samfundet.no (cassarossa.samfundet.no [129.241.93.19]) by postgresql.org (Postfix) with ESMTP id BCEE12E022C for ; Tue, 27 Nov 2007 19:38:36 -0400 (AST) Received: from trofast.ipv6.sesse.net ([2001:700:300:1803:20e:cff:fe36:a766] helo=trofast.sesse.net) by cassarossa.samfundet.no with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.63) (envelope-from ) id 1IxA0g-0002nL-55; Wed, 28 Nov 2007 00:38:34 +0100 Received: from root by trofast.sesse.net with local (Exim 4.68) (envelope-from ) id 1IxA0f-0006GN-GT; Wed, 28 Nov 2007 00:38:33 +0100 Date: Wed, 28 Nov 2007 00:38:33 +0100 From: "Steinar H. Gunderson" To: cluster Cc: pgsql-performance@postgresql.org Subject: Re: Query only slow on first run Message-ID: <20071127233833.GA24031@uio.no> Mail-Followup-To: cluster , pgsql-performance@postgresql.org References: <20071127165543.GY11978@crankycanuck.ca> <23999.1196184368@sss.pgh.pa.us> <056501c83128$55d58a90$8e00a8c0@tridecap.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Operating-System: Linux 2.6.20.4 on a x86_64 X-Message-Flag: Outlook? --> http://www.mozilla.org/products/thunderbird/ User-Agent: Mutt/1.5.17 (2007-11-01) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/330 X-Sequence-Number: 27745 On Tue, Nov 27, 2007 at 11:51:40PM +0100, cluster wrote: > A thing that strikes me is the following. As you can see I have the > constraint: q.status = 1. Only a small subset of the data set has this > status. I have an index on q.status but for some reason this is not used. > Instead the constraint are ensured with a "Filter: (q.status = 1)" in an > index scan for the primary key in the "q" table. If the small subset having > q.status = 1 could be isolated quickly using an index, I would expect the > query to perform better. I just don't know why the planner doesn't use the > index on q.status. An index scan (as opposed to a bitmap index scan) can only use one index at a time, so it will choose the most selective one. Here it quite correctly recognizes that there will only be one matching record for the given question_id, so it uses the primary key instead. You could make an index on (question_id,status) (or a partial index on question id, with status=1 as the filter), but I'm not sure how much it would help you unless the questions table is extremely big. It doesn't appear to be; in fact, it appears to be all in RAM, so that's not your bottleneck. /* Steinar */ -- Homepage: http://www.sesse.net/