Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 052541337B4E for ; Thu, 24 Feb 2011 20:20:20 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 21248-09 for ; Fri, 25 Feb 2011 00:20:12 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from outmail149083.authsmtp.com (outmail149083.authsmtp.com [62.13.149.83]) by mail.postgresql.org (Postfix) with ESMTP id 1120B1337B4A for ; Thu, 24 Feb 2011 20:20:11 -0400 (AST) Received: from mail-c194.authsmtp.com (mail-c194.authsmtp.com [62.13.128.121]) by punt7.authsmtp.com (8.14.2/8.14.2/Kp) with ESMTP id p1P0KA8L058051; Fri, 25 Feb 2011 00:20:10 GMT Received: from Sidney-Stratton.local (70-36-197-225.dsl.dynamic.sonic.net [70.36.197.225]) (authenticated bits=0) by mail.authsmtp.com (8.14.2/8.14.2) with ESMTP id p1P0K6fG015104; Fri, 25 Feb 2011 00:20:07 GMT Message-ID: <4D66F5B0.5080502@agliodbs.com> Date: Thu, 24 Feb 2011 16:20:00 -0800 From: Josh Berkus User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20090223 Thunderbird/3.0b2 MIME-Version: 1.0 To: Dave Crooke CC: Kevin Grittner , Michael Glaesemann , pgsql-performance@postgresql.org Subject: Re: Picking out the most recent row using a time stamp column References: <08B3368E-E623-4E9C-83F5-AB05B471AF4C@seespotcode.net> <4D666A0F020000250003AFCE@gw.wicourts.gov> In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Server-Quench: 005bcfbe-4075-11e0-80b9-0022640b883e X-AuthReport-Spam: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-AuthRoute: OCdyZgscClZXSx8a IioLCC5HRQ8+YBZL BAkGMA9GIUINWEQN c1AAcx19PVdbHwkA AnYLW15WUFdxUS1y axRSaxtca01QXQdv T0pMQFdNFEsoAHwB fRhIJRlzcgRGfDB2 bU5qEHBbWRYpIxQo X0pUEmgbZGY1aH0X UBJRagNUcgFMeh8X Ylh8AT1vNG8XDRoV JSEUBRUEdQ1eIydU RA5FKk8OCVoLE3Y5 QAseVS8iFkADW20p KANuN0UdGklZOw0u MF4sEUkEOhoKaEVV GEVQBSkx X-Authentic-SMTP: 61633136333939.1015:706 X-AuthFastPath: 0 (Was 255) X-AuthVirus-Status: No virus detected - but ensure you scan with your own anti-virus system. X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.6 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7 X-Spam-Level: X-Archive-Number: 201102/487 X-Sequence-Number: 42625 On 2/24/11 3:38 PM, Dave Crooke wrote: > Thanks to all .... I had a tickling feeling at the back of my mind that > there was a neater answer here. For the record, times (all from > in-memory cached data, averaged over a bunch of runs): > > Dependent subquery = 117.9 seconds > Join to temp table = 2.7 sec > DISTINCT ON = 2.7 sec But wait, there's more! You haven't tested the Windowing Function solution. I'll bet it's even faster. SELECT id_key, time_stamp, value FROM ( SELECT id_key, time_stamp, value, row_number() OVER ( PARTITION BY id_key ORDER BY time_stamp DESC) as ranking FROM thetable ) as filtered_table WHERE ranking = 1 -- -- Josh Berkus PostgreSQL Experts Inc. http://www.pgexperts.com