Re: Weird query sort - Mailing list pgsql-general

From Tom Lane
Subject Re: Weird query sort
Date
Msg-id 4187.1230422916@sss.pgh.pa.us
Whole thread Raw
In response to Weird query sort  ("Jeffrey Melloy" <jmelloy@gmail.com>)
List pgsql-general
"Jeffrey Melloy" <jmelloy@gmail.com> writes:
> It seems like I should be able to order by quer_time desc and then
> query_time asc. Am I missing something?

You'd have to do it *after* the join and GROUP BY if you want it to
control the input to the aggregate reliably.  Either of those operations
will feel free to output rows in whatever order suits its convenience.
Ordering in the sub-select is useful in connection with making sure its
LIMIT produces the tuples you want, but it isn't going to be preserved
through the join and GROUP BY.

Also, I think what you really need here is to have the intermediate
query "ORDER BY queries.query_id, query_time"; otherwise you risk
having the GROUP BY do a sort by query_id, which'll destroy the desired
ordering on query_time within query_id groups.

            regards, tom lane

pgsql-general by date:

Previous
From: "Jeffrey Melloy"
Date:
Subject: Weird query sort
Next
From: plu tard
Date:
Subject: Finding foreign keys that are missing indexes