473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

7.3.4 and 7.4 ORDER in queries

Hi,
I'm not an expert, not by far.
I've just installed postgres7.4 and have realized tat the order of rows in
queries in different to that in 7.3.4. It seems that previously it was more
logic, ordering by the first column, then by the second,... by default.
At least this is so in a couple of queries I have. And now I have to
explicitly say the order I want.

Could someone just tell me why is this so?

Thanks and best wishes,

Javier
................

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #1
5 1518
javier garcia - CEBAS <rn***@cebas.csic.es> writes:
Hi,
I'm not an expert, not by far.
I've just installed postgres7.4 and have realized tat the order of rows in
queries in different to that in 7.3.4. It seems that previously it was more
logic, ordering by the first column, then by the second,... by default.
At least this is so in a couple of queries I have. And now I have to
explicitly say the order I want.

Could someone just tell me why is this so?


Unless you specify ORDER BY, the order of matching rows returned by a
query is completely up to the server. So it's not suprising that the
ordering changed.

If you depend on order, always use ORDER BY.

-Doug

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #2
javier garcia - CEBAS writes:
I've just installed postgres7.4 and have realized tat the order of rows in
queries in different to that in 7.3.4. It seems that previously it was more
logic, ordering by the first column, then by the second,... by default.
At least this is so in a couple of queries I have. And now I have to
explicitly say the order I want.


Unless you specify ORDER BY explicitly, the order is completely random.
In practice, it depends on the order of the rows on disk and the query
execution plan, but those are implementation details that may change over
time. If you need a particular order, you need to say so.

--
Peter Eisentraut pe*****@gmx.net
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 12 '05 #3
rn***@cebas.csic.es (javier garcia - CEBAS) writes:
I'm not an expert, not by far.
I've just installed postgres7.4 and have realized tat the order of rows in
queries in different to that in 7.3.4. It seems that previously it was more
logic, ordering by the first column, then by the second,... by default.
At least this is so in a couple of queries I have. And now I have to
explicitly say the order I want.

Could someone just tell me why is this so?


You always had to explicitly state ORDER BY if you wanted to get a
specific ordering; that's how SQL is designed.

There are a number of cases where earlier versions of PostgreSQL had
to perform specific sort procedures in order to perform GROUP BY
queries, whereas 7.4 is able to use unordered hash tables to collect
the data, thereby eliminating the sorts.

That is quite likely to be the sort of thing that you are observing.

If you want the data to be returned in a particular order, then you
MUST state that order, otherwise you'll get whatever the database
gives you, and yes, indeed, the ordering that is imposed implicitly
can and does change.
--
let name="cbbrowne" and tld="libertyrms.info" in String.concat "@" [name;tld];;
<http://dev6.int.libertyrms.com/>
Christopher Browne
(416) 646 3304 x124 (land)
Nov 12 '05 #4
pe*****@gmx.net (Peter Eisentraut) writes:
javier garcia - CEBAS writes:
I've just installed postgres7.4 and have realized tat the order of rows in
queries in different to that in 7.3.4. It seems that previously it was more
logic, ordering by the first column, then by the second,... by default.
At least this is so in a couple of queries I have. And now I have to
explicitly say the order I want.
Unless you specify ORDER BY explicitly, the order is completely random.


That's not _quite_ true. It is somewhat unusual for the ordering of
query results to be "random;" there is typically some underlying logic
that is NOT random behind it.

What is a bit fairer is to say that, absent an ORDER BY clause, the
order is "totally unspecified," and that any given ordering is as
legitimate as any other ordering.

"Random order" is what you'd generally observe if the data is being
accessed via hashing (which has become a lot more popular in 7.4 than
it used to be).

Other 'surprising' orders might include:

- Data might be returned in the order in which it was inserted, where
if an old record was overwritten by a new one, that might draw in new
records sooner;

- Data returns might start with the set of pages that were already in
the page cache.

[The point being that it is reasonable, absent of ORDER BY, to return
results based on the physical location of the data.]

I am quite surprised that the original poster got to expect an
implicit "ORDER BY 1, 2". That seems _quite_ surprising.
In practice, it depends on the order of the rows on disk and the query
execution plan, but those are implementation details that may change over
time. If you need a particular order, you need to say so.


Very true.
--
let name="cbbrowne" and tld="libertyrms.info" in String.concat "@" [name;tld];;
<http://dev6.int.libertyrms.com/>
Christopher Browne
(416) 646 3304 x124 (land)
Nov 12 '05 #5
Christopher Browne <cb******@libertyrms.info> writes:
I am quite surprised that the original poster got to expect an
implicit "ORDER BY 1, 2". That seems _quite_ surprising.


Pre-7.4, GROUP BY queries would tend to work like that, but it was
always an implementation artifact, never a documented much less
guaranteed behavior ...

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: One's Too Many | last post by:
Ran into a strange problem today: 8.1.7 on AIX 4.3.3 Database and applications had been working fine for two years and all of a sudden a couple of regularly-run queries are now no longer...
4
by: Tryfon Gavriel | last post by:
Hi all I recently noticed when trying to optimise a major query of a chess website I am the webmaster of, that adding an order by for "gamenumber" which is a clustered index field as in for...
3
by: becoolmun | last post by:
It seems with V7 of DB2 I can now use Fetch First 1 Rows Only in a Select stmt, whic is great, because I don't have to use a cursor. Unfortunately, it doesn't allow ORDER BY in the same Select and...
12
by: Irene | last post by:
Hi all again, Well, I have my Athletics database with Athletes, Competitions, Scores tables. I have a ranking query where I get back the list of the competitions-athletes and scores...
2
by: rooster575 | last post by:
Im running a querystring in vb.net and it seems as though SQL server decides on the order in which to run the 3 queries in the string.. Is this possible? I would think that SQL server starts with...
104
by: Beowulf | last post by:
I have the view below and if I use vwRouteReference as the rowsource for a combo box in an MS Access form or run "SELECT * FROM vwRouteReference" in SQL Query Analyzer, the rows don't come through...
2
by: Tim Marshall | last post by:
Have worked for many years with the above both toegther and separately (as in Jet and reports), I was always under the impression that the order by clause of a query had no real bearing on the...
13
by: bevanward | last post by:
Hi All I am finding unexpected results when inserted into a newly created table that has a field of datatype int identity (1,1). Basically the order I sort on when inserting into the table is...
24
by: Hurricane | last post by:
When I create a view in SQL and include an ORDER BY clause i can see it in Management Studio. However, when I call the same view from an ASP page the order goes completely haywire. Any ideas?
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.