Re: Weird query sort

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Jeffrey Melloy" <jmelloy(at)gmail(dot)com>
Cc: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: Weird query sort
Date: 2008-12-28 00:08:36
Message-ID: 4187.1230422916@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Jeffrey Melloy" <jmelloy(at)gmail(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message plu tard 2008-12-28 07:00:02 Finding foreign keys that are missing indexes
Previous Message Jeffrey Melloy 2008-12-27 22:02:37 Weird query sort