Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 788F99FB1FD for ; Tue, 3 Oct 2006 19:39:39 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 64688-09 for ; Tue, 3 Oct 2006 19:39:36 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 588379FB1EC for ; Tue, 3 Oct 2006 19:39:36 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k93MdWJs025228; Tue, 3 Oct 2006 18:39:32 -0400 (EDT) To: Mark Lewis cc: gdavis@refractions.net, Chris Browne , pgsql-performance@postgresql.org Subject: Re: BUG #2658: Query not using index In-reply-to: <1159912457.18640.96.camel@archimedes> References: <200609272056.k8RKuWjY040283@wwwmaster.postgresql.org> <20061003000147.GA31475@wolff.to> <4522AA01.4020709@refractions.net> <60irj1gr3r.fsf@dba2.int.libertyrms.com> <4522B5B9.2050206@refractions.net> <60d599gqcz.fsf@dba2.int.libertyrms.com> <4522C8D8.4010601@refractions.net> <10941.1159908489@sss.pgh.pa.us> <4522CD8C.1000108@refractions.net> <1159909612.18640.80.camel@archimedes> <4522D4CE.4070806@refractions.net> <1159911267.18640.83.camel@archimedes> <4522D7B1.3040008@refractions.net> <1159912457.18640.96.camel@archimedes> Comments: In-reply-to Mark Lewis message dated "Tue, 03 Oct 2006 14:54:17 -0700" Date: Tue, 03 Oct 2006 18:39:32 -0400 Message-ID: <25227.1159915172@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.093 tagged_above=0 required=5 tests=AWL, SPF_HELO_PASS, SPF_PASS X-Spam-Level: X-Archive-Number: 200610/66 X-Sequence-Number: 21195 Mark Lewis writes: > Have you considered creating one partial index per assetid? Something > along the lines of "CREATE INDEX asset_index_N ON asset_positions(ts) > WHERE assetid=N"? I'd guess that the planner probably wouldn't be smart > enough to use the partial indexes unless you issued a separate query for > each assetid, but each one of those queries should be really fast. Actually, a single index on (assetid, ts) is sufficient to handle select max(ts) from asset_positions where assetid = constant The problem is to know what values of "constant" to issue the query for, and this idea doesn't seem to help with that. If Graham is willing to assume that the set of assetids changes slowly, perhaps he could keep a summary table that contains all the valid assetids (or maybe there already is such a table? is assetid a foreign key?) and do select pk.assetid, (select max(ts) from asset_positions where assetid = pk.assetid) from other_table pk; I'm pretty sure the subselect would be planned the way he wants. regards, tom lane