ORDER BY results

From: Howard Cole <howardnews(at)selestial(dot)com>
To: 'PgSql General' <pgsql-general(at)postgresql(dot)org>
Subject: ORDER BY results
Date: 2005-09-22 13:19:00
Message-ID: 4332AF44.7060802@selestial.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

I have a query which presents results sorted using the SQL "ORDER BY...
LIMIT". Now my question is... if the column that the order refers to
has some repeated data, will the order of results always be the same?

For instance, say I have a table as follows:

create table example {id serial primary key, stuff int};

Now suppose I the table is populated as follows:

ID STUFF
1 10
2 11
3 11
4 12

Now if i try SELECT ID,STUFF FROM EXAMPLE ORDER BY STUFF; I might expect

ID STUFF
1 10
2 11
3 11
4 12

but possibly could get

ID STUFF
1 10
3 11
2 11
4 12

... Depending on the implementation. Whichever result I get, can I
always expect the output in the same way? Or should I ensure this by
sorting by the ID aswell

e.g. SELECT ID,STUFF FROM EXAMPLE ORDER BY STUFF, ID; - which would
always give the same order.

In case you are interested, the application for this problem is the
presentation of partial results using LIMIT (as per search results
20,21...30) - for instance could the following present different results
to the same client...

SELECT ID,STUFF FROM EXAMPLE WHERE STUFF=11 ORDER BY STUFF LIMIT 0, 1;

Could give the following on one transaction
2 11
And the following on another transaction
3 11

I am not bothered about which way the results are presented and I am
aware that the order would be implementaion specific, but obviously the
results presented should always be the same for my app.

Regards,

Howard Cole
www.selestial.com

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Yonatan Ben-Nes 2005-09-22 13:31:03 Re: Slow search.. quite clueless
Previous Message Gábor Farkas 2005-09-22 12:49:41 Re: Slow search.. quite clueless