Re: Performance issues - Mailing list pgsql-performance

From Tomas Vondra
Subject Re: Performance issues
Date
Msg-id 55084890.6000907@2ndquadrant.com
Whole thread Raw
In response to Re: Performance issues  (Thomas Kellerer <spam_eater@gmx.net>)
Responses Re: Performance issues
List pgsql-performance
On 17.3.2015 16:24, Thomas Kellerer wrote:
> 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

Nice, thanks!

>
> Btw: your row_number() usage wouldn't return the "latest" row either.
> It would return the "oldest" row.

Oh, right. I forgot the DESC in the window.


--
Tomas Vondra                http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


pgsql-performance by date:

Previous
From: Tomas Vondra
Date:
Subject: Re: Performance issues
Next
From: Steven Jones
Date:
Subject: Very slow checkpoints