Oracle gem of the day: DISTINCT is not ORDER BY

jdavidb on 2006-11-02T20:21:25

I'm in a training class and just heard the following:

Right now, when you do a SELECT DISTINCT, Oracle performs a SORT internally in order to accomplish the DISTINCT. This means your data happens to come out ordered. I say happens to, because Oracle (and, I presume, any other RDBMS vendor) has always made it clear that unless you use ORDER BY, the order that your query results are returned in is not guaranteed. Of course, people always seem to respond to such warnings by ignoring them. Heh.

So, word on the street now is that Oracle is going to start using some kind of hash join internally instead of a SORT to accomplish the DISTINCT. Meaning your results will no longer be ordered. If you've followed what you've been told ever since somebody first taught you SQL, you'll have no problem, because you don't assume your results to be ordered unless you use ORDER BY. But apparently not everybody does that.

I'm honestly a little entertained at the thought of people who don't listen suddenly being bitten by this. It's like 19100 all over again.


that could explain

TeeJay on 2006-11-02T21:09:59

..why postgres requires that anything distinct in a distinct clause must also be in the order which is bloody annoying when you want a whole bunch of columns.

Re:that could explain

jdavidb on 2006-11-03T16:03:30

Ew, that's wrong. A definite strike against PostgreSQL.