never trust an "implicit" sort in any version
if you want your data sorted a certain way, then force it with order by
Main Topics
Browse All TopicsI have two database environments. One if oracle 8i and the other is 10g
when i run the following query in 8i it returns as follows (see below)
8i
SELECT rownum, TYPEID, name
FROM TYPE
WHERE deptid = 51
ORDER BY name ASC
Row# ROWNUM TYPEID NAME
1 1 2000001 TEST
2 2 716000001 TEST
10g
SELECT rownum, TYPEID, name
FROM TYPE
WHERE deptid = 51
ORDER BY name ASC
Row# ROWNUM TYPEID NAME
1 2 716000001 TEST
2 1 2000001 TEST
As you can see in both environments the sequence order is different. Eventhough I am sorting by name and the column is exactly equivalent , the 10g returns rownum 2 then 1.
I need it such that resultset in 10g is equivalent to 8i.
I checked the indices and couldn't find anything out of the ordinary.
Please advise what I would need to do in 10g so that the query returns typeid = 2000001 first just like in 8i.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Those are still using "implicit" sorting and hence are unreliable.
I just ran the following query...
select /*+ PARALLEL(wordlist, 4) */ word from wordlist (word list is an IOT)
here are the first 9 words returned...
Row# WORD
1 COMPLAINANT
2 COMPLAINING
3 COMPLAINT
4 COMPLAISANCE
5 COMPLAISANT
6 COMPLEMENT
7 COMPLEMENTARITY
8 COMPLEMENTARY
9 COMPLEMENTATION
vs
select /*+ PARALLEL(wordlist, 4) */ word from wordlist order by word
Row# WORD
1 A
2 AAA
3 AARDVARK
4 AARHUS
5 AARON
6 ABA
7 ABABA
8 ABACK
9 ABACUS
Since I'm tangenting, at least I can add some useful info, so let me say: I would consider an IOT or cluster where I had a predominant ORDER BY case. Meaning, if I typically ordered by col1, col2 90% of the time, using the IOT or cluster will speed up things because Oracle will not have to sort the results before returning.
Still, keep the ORDER BY, as Sean already said, or there are no guarantees.
I never consider the "default", I think that's a dangerous practice, like trusting default date formats. :)
In my example, I picked an easy illustration
If my table is partitioned oracle may parallelize the query for me
and, even if I run the same query multiple times I'm not guaranteed to get the same results.
I just ran this again... select /*+ PARALLEL(wordlist, 4) */ word from wordlist
and got different results
Row# WORD
1 C
2 CA
3 CAB
4 CABAL
5 CABANA
6 CABARET
7 CABBAGE
8 CABDRIVER
9 CABIN
so, it is obviously scanning in order through the index, but without ORDER BY to ensure the final results, the Parallel threads can return in effectively random order even though each will scan in "natural" or "default" order, the overall result isn't reliable without the explicit order by
as long as we're trying to make the most of default sorting. An order by operation on already sorted data is almost free.
So, even if I was going to do
select * from my_index_organized_table
I would still add an order by.
interestingly, I just did an autotrace on select word from wordlist with and without order by
I was not expecting this... NOT using an order by produced many, many more gets but did eliminate one sort.
That's really getting tangential though. I'll investigate that more off line and start a separate discussion about what I find.
Once again my poor choice of words has made me appear to give bad advice.
The only guarantee is to use ORDER BY, and I never meant to imply that I recommended not to.
I should have said more than "OR use a cluster or IOT" because by saying what I said I was offering it as an alternative.
I meant to communicate that there are physical storage structures (indexes and clusters) that can pretty much guarantee the ordering of your data at the physical layer and the side effect is that the "implicit" behaviour will be more consistent. The other side effect is performance; you'll benefit from an index or cluster that coincides with the predominant ordering that you request, because in most cases, the rows will already be sorted, so Oracle will skip the sort step if the SQL "order by" clause coincides with the physical ordering.
Please read my suggestion as supplementary information, not as an alternative, as I shouldn't have worded it as such.
unfortunately, i cannot modify the query as this was a query i retrieved after debugging the application itself. so i cannot explicitly alter the order by portion.
therefore, i need to resovle at the database level.
i haven't tried yet, but if i were to delete the records and re-insert them in the 10g environment would it sort them like in the 8i environment. ?
No. As we've been discussing, by default, Oracle can order the rows however it wants. The only way to guarantee physical ordering is by recreating the object as one that provides ordered storage (IOT or cluster), or by adding an index that includes all of the columns so Oracle can use a INDEX FULL SCAN or INDEX FAST FULL SCAN.
A view might also help. Its possible you could rename the table, and create a view with the original table name and add an order by to the view.
rename a to t_a;
create view a as select * from t_a order by id;
Not that I'm recommend this approach, but given your restrictions, it might be worth a try.
mrjoltcola is correct about creating physically ordered data.
BUT!!!
as discussed above, the physical ordering of the data is NOT indicative of the order the data will be returned to you from a query
I made an easy example above where I forced parallel query. But even if I don't do that. The physical order is still not guaranteed to be reflected in the output of a query.
Using the same index organized table as before
select * from wordlist
(note, I haven't done ANYTHING to alter the results or forced funny execution through hints)
Here's is an excerpt of the returned data....
Row# WORD PHONE_NUMBER
1 A 2
2 AAA 222
3 AARDVARK 22738275
4 AARHUS 227487
5 AARON 22766
6 ABA 222
7 ABABA 22222
8 ABACK 22225
9 ABACUS 222287
-----
----- started out good but when I scrolled down further....
-----
975 FRIEDRICH 374337424
976 FRIEND 374363
977 FRIENDLESS 3743635377
978 D 3
979 D'ART 3278
980 D'ETAT 33828
981 D'ETRE 33873
982 D'OEUVRE 3638873
983 DAB 322
now some of my D's came before my F's
the plan even shows the query is executed via the index which is sorted.
Row# PLAN_TABLE_OUTPUT
1 Plan hash value: 888072002
2
3 --------------------------
4 | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
5 --------------------------
6 | 0 | SELECT STATEMENT | | 35369 | 587K| 56 (0)| 00:00:01 |
7 | 1 | INDEX FAST FULL SCAN| PK_WORDLIST | 35369 | 587K| 56 (0)| 00:00:01 |
8 --------------------------
I understand that the only way to alter the order in which results display is by explicitly changing the order by condition. but since i cannot modify the query and have to deal with resolving at db level i've just decided to alter the record of the duplicate so that the query will always return 1 record since the 2nd record won't satisfy condition.
thanks all for your help and guidance.
Business Accounts
Answer for Membership
by: sdstuberPosted on 2009-10-01 at 08:36:23ID: 25469843
if you want typeid to be sorted, you must include it in your order by
SELECT rownum, TYPEID, name
FROM TYPE
WHERE deptid = 51
ORDER BY name ASC , typeid asc