Skip site navigation (1) Skip section navigation (2)

Re: Any better plan for this query?..

From: Matthew Wakeling <matthew(at)flymine(dot)org>
To: Simon Riggs <simon(at)2ndQuadrant(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Dimitri <dimitrik(dot)fr(at)gmail(dot)com>, Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>, Dimitri Fontaine <dfontaine(at)hi-media(dot)com>, Alvaro Herrera <alvherre(at)commandprompt(dot)com>, Aidan Van Dyk <aidan(at)highrise(dot)ca>, Merlin Moncure <mmoncure(at)gmail(dot)com>, PostgreSQL Performance <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Any better plan for this query?..
Date: 2009-05-19 12:01:43
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-performance
On Tue, 19 May 2009, Simon Riggs wrote:
>> Speaking of avoiding large sorts, I'd like to push again for partial
>> sorts. This is the situation where an index provides data sorted by
>> column "a", and the query requests data sorted by "a, b". Currently,
>> Postgres sorts the entire data set, whereas it need only group each
>> set of identical "a" and sort each by "b".
> Partially sorted data takes much less effort to sort (OK, not zero, I
> grant) so this seems like a high complexity, lower value feature. I
> agree it should be on the TODO, just IMHO at a lower priority than some
> other features.

Not arguing with you, however I'd like to point out that partial sorting 
allows the results to be streamed, which would lower the cost to produce 
the first row of results significantly, and reduce the amount of RAM used 
by the query, and prevent temporary tables from being used. That has to be 
a fairly major win. Queries with a LIMIT would see the most benefit.

That leads me on to another topic. Consider the query:


where the column "a" is declared UNIQUE and has an index. Does Postgres 
eliminate "b" from the ORDER BY, and therefore allow fetching without 
sorting from the index?

Or how about this query:

SELECT * FROM table1, table2 WHERE = ORDER BY,

where both "id" columns are UNIQUE with an index. Do we eliminate 
"" from the ORDER BY in this case?


"Programming today is a race between software engineers striving to build
 bigger and better idiot-proof programs, and the Universe trying to produce
 bigger and better idiots. So far, the Universe is winning."  -- Rich Cook

In response to


pgsql-performance by date

Next:From: Tom LaneDate: 2009-05-19 12:58:25
Subject: Re: Any better plan for this query?..
Previous:From: DimitriDate: 2009-05-19 12:00:41
Subject: Re: Any better plan for this query?..

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group