Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 19AD89FB324 for ; Tue, 3 Oct 2006 15:41:55 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 77683-03 for ; Tue, 3 Oct 2006 18:41:44 +0000 (UTC) X-Greylist: delayed 00:18:25.102173 by SQLgrey- Received: from mail.refractions.net (mail.refractions.net [24.68.236.214]) by postgresql.org (Postfix) with ESMTP id 1EE9C9FB314 for ; Tue, 3 Oct 2006 15:41:44 -0300 (ADT) Received: from lion.animals (lion [192.168.50.200]) by mail.refractions.net (Postfix) with ESMTP id 27E9F122C3A8 for ; Tue, 3 Oct 2006 11:31:26 -0700 (PDT) Received: by lion.animals (Postfix, from userid 88) id F14EBE3CC; Tue, 3 Oct 2006 11:23:14 -0700 (PDT) Received: from refractions.net (unknown [192.168.50.43]) by lion.animals (Postfix) with ESMTP id 9E27BE3C3 for ; Tue, 3 Oct 2006 11:22:29 -0700 (PDT) Message-ID: <4522AA01.4020709@refractions.net> Date: Tue, 03 Oct 2006 11:20:49 -0700 From: Graham Davis Reply-To: gdavis@refractions.net User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.6b) Gecko/20031205 Thunderbird/0.4 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: BUG #2658: Query not using index References: <200609272056.k8RKuWjY040283@wwwmaster.postgresql.org> <20061003000147.GA31475@wolff.to> In-Reply-To: <20061003000147.GA31475@wolff.to> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200610/49 X-Sequence-Number: 21178 Hi, Adding DESC to both columns in the SORT BY did not make the query use the multikey index. So both SELECT DISTINCT ON (assetid) assetid, ts FROM asset_positions ORDER BY assetid, ts DESC; and SELECT DISTINCT ON (assetid) assetid, ts FROM asset_positions ORDER BY assetid DESC, ts DESC; use the same query plans and both do sequential scans without using either the (assetid, ts) or (ts) indexes. Any other ideas on how to make this query use an index? Thanks, -- Graham Davis Refractions Research Inc. gdavis@refractions.net >On Wed, Sep 27, 2006 at 20:56:32 +0000, > Graham Davis wrote: > > >>SELECT assetid, max(ts) AS ts >>FROM asset_positions >>GROUP BY assetid; >> >>I have an index on (ts), another index on (assetid) and a multikey index on >>(assetid, ts). I know the assetid index is pointless since the multikey one >>takes its place, but I put it there while testing just to make sure. The >>ANALYZE EXPLAIN for this query is: >> >> QUERY PLAN >>---------------------------------------------------------------------------- >>------------------------------------------------------------- >> HashAggregate (cost=125423.96..125424.21 rows=20 width=12) (actual >>time=39693.995..39694.036 rows=20 loops=1) >> -> Seq Scan on asset_positions (cost=0.00..116654.64 rows=1753864 >>width=12) (actual time=20002.362..34724.896 rows=1738693 loops=1) >> Total runtime: 39694.245 ms >>(3 rows) >> >>You can see it is doing a sequential scan on the table when it should be >>using the (assetid, ts) index, or at the very least the (ts) index. This >>query takes about 40 seconds to complete with a table of 1.7 million rows. >>I tested running the query without the group by as follows: >> >> > > > >>SELECT DISTINCT ON (assetid) assetid, ts >>FROM asset_positions >>ORDER BY assetid, ts DESC; >> >> > >This is almost what you want to do to get an alternative plan. But you >need to ORDER BY assetid DESC, ts DESC to make use of the multicolumn >index. If you really need the other output order, reverse it in your >application or use the above as a subselect in another query that orders >by assetid ASC. > >