Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V78Yw-0003HG-Vh for pgsql-performance@arkaria.postgresql.org; Wed, 07 Aug 2013 18:34:23 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1V78Yw-0001cY-EM for pgsql-performance@arkaria.postgresql.org; Wed, 07 Aug 2013 18:34:22 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V78Yv-0001cR-8A for pgsql-performance@postgresql.org; Wed, 07 Aug 2013 18:34:21 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V78Yo-0000ay-9r for pgsql-performance@postgresql.org; Wed, 07 Aug 2013 18:34:20 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id r77IYCBf009814; Wed, 7 Aug 2013 14:34:12 -0400 From: Tom Lane To: Claudio Freire cc: Robert DiFalco , "pgsql-performance@postgresql.org" Subject: Re: Efficiently query for the most recent record for a given user In-reply-to: References: Comments: In-reply-to Claudio Freire message dated "Wed, 07 Aug 2013 15:19:32 -0300" Date: Wed, 07 Aug 2013 14:34:12 -0400 Message-ID: <9813.1375900452@sss.pgh.pa.us> X-Pg-Spam-Score: -0.0 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Claudio Freire writes: > On Wed, Aug 7, 2013 at 3:12 PM, Robert DiFalco wrote: >> I want to get the last call_activity record for a single user. > Create an index over (user_id, called desc), and do > select * from call_activity where user_id = blarg order by called desc limit 1 Note that there's no particular need to specify "desc" in the index definition. This same index can support searches in either direction on the "called" column. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance