Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YXtMO-000121-In for pgsql-performance@arkaria.postgresql.org; Tue, 17 Mar 2015 15:24:48 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YXtMN-0006FN-Vk for pgsql-performance@arkaria.postgresql.org; Tue, 17 Mar 2015 15:24:48 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YXtMM-0006CF-39 for pgsql-performance@postgresql.org; Tue, 17 Mar 2015 15:24:46 +0000 Received: from plane.gmane.org ([80.91.229.3]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YXtMI-0001KO-5r for pgsql-performance@postgresql.org; Tue, 17 Mar 2015 15:24:44 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1YXtM7-0003hW-Qo for pgsql-performance@postgresql.org; Tue, 17 Mar 2015 16:24:32 +0100 Received: from 185.40.248.10 ([185.40.248.10]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 17 Mar 2015 16:24:31 +0100 Received: from spam_eater by 185.40.248.10 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 17 Mar 2015 16:24:31 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-performance@postgresql.org From: Thomas Kellerer Subject: Re: Performance issues Date: Tue, 17 Mar 2015 16:24:14 +0100 Lines: 32 Message-ID: References: <84a6fe4be04c3ef0a714851977860ac8@mail.gmail.com> <55034A48.4040409@2ndquadrant.com> <849e75d9463938372dd4c516f5c5d89c@mail.gmail.com> <55037CEA.5050906@2ndquadrant.com> <55073272.20209@BlueTreble.com> <55074424.9050506@2ndquadrant.com> <55076FFB.9090105@BlueTreble.com> c5231342781287ff8cbc9ac1f20ff036@mail.gmail.com <61a7 a27428e588411e8ac1817043205c@mail.gmail.com> <550813CD.7040605@2ndqu adrant.com> d6a86358c5e8d7ffe923ff307c3e8c7e@mail.gmail.com <55083236.8070900@2ndquadrant.com> <55083D74.4000601@2ndquadrant.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 185.40.248.10 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 In-Reply-To: <55083D74.4000601@2ndquadrant.com> X-Pg-Spam-Score: 0.5 (/) 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 Tomas Vondra schrieb am 17.03.2015 um 15:43: > On 17.3.2015 15:19, Thomas Kellerer wrote: >> Tomas Vondra schrieb am 17.03.2015 um 14:55: >>> (2) using window functions, e.g. like this: >>> >>> SELECT * FROM ( >>> SELECT *, >>> ROW_NUMBER() OVER (PARTITION BY touchpoint_execution_id >>> ORDER BY FROM max_creation_dt) AS rn >>> FROM s_f_touchpoint_execution_status_history >>> ) foo WHERE rn = 1 >>> >>> But estimating this is also rather difficult ... >> >> >> From my experience rewriting something like the above using DISTINCT >> ON is usually faster. > > How do you get the last record (with respect to a timestamp column) > using a DISTINCT ON? You need to use "order by ... desc". See here: http://sqlfiddle.com/#!15/d4846/2 Btw: your row_number() usage wouldn't return the "latest" row either. It would return the "oldest" row. -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance