Re: Possible to improve query plan? - Mailing list pgsql-performance

From Tom Lane
Subject Re: Possible to improve query plan?
Date
Msg-id 20065.1295295866@sss.pgh.pa.us
Whole thread Raw
In response to Possible to improve query plan?  (Jeremy Palmer <JPalmer@linz.govt.nz>)
Responses Re: Possible to improve query plan?  (Jeremy Palmer <JPalmer@linz.govt.nz>)
Re: Possible to improve query plan?  (Mladen Gogala <mladen.gogala@vmsinfo.com>)
List pgsql-performance
Jeremy Palmer <JPalmer@linz.govt.nz> writes:
> I've come to a dead end in trying to get a commonly used query to
> perform better.

> EXPLAIN
> SELECT * FROM (
>     SELECT
>         row_number() OVER (PARTITION BY id ORDER BY _revision_created DESC) as row_number,
>         *
>     FROM
>         version_crs_coordinate_revision
>     WHERE (
>         (_revision_created <= 16 AND _revision_expired > 16 AND _revision_expired <= 40) OR
>         (_revision_created > 16 AND _revision_created <= 40)
>     )
> ) AS T
> WHERE row_number = 1;

If I'm not mistaken, that's a DB2-ish locution for a query with DISTINCT
ON, ie, you're looking for the row with highest _revision_created for
each value of id.  It might perform well on DB2, but it's going to
mostly suck on Postgres --- we don't optimize window-function queries
very much at all at the moment.  Try writing it with DISTINCT ON instead
of a window function, like so:

SELECT DISTINCT ON (id)
        *
    FROM
        version_crs_coordinate_revision
    WHERE (
        (_revision_created <= 16 AND _revision_expired > 16 AND _revision_expired <= 40) OR
        (_revision_created > 16 AND _revision_created <= 40)
    )
ORDER BY id, _revision_created DESC;

You could also experiment with various forms of GROUP BY if you're loath
to use any Postgres-specific syntax.

            regards, tom lane

pgsql-performance by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: Bad plan when join on function
Next
From: "Kevin Grittner"
Date:
Subject: Re: Bad plan when join on function