Question

Oracle 10g sequence order is different

Asked by: badtz7229

I 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.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-10-01 at 08:28:36ID24777323
Topics

Oracle 8.x

,

Oracle 10.x

Participating Experts
3
Points
500
Comments
16

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

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.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

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.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

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.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. RowNum
    I want to select 100 rows. What is the syntex of the select query (like rownum<=100 in Oracle)? thank,
  2. rownum query in oracle 8.0.5
    I have table tbl_region with follwoing description and 25 records: REG_ID NOT NULL NUMBER(8) REG_DESC NOT NULL VARCHAR2(32) I want to retreive a range of records so that they are ordered in reg_desc asc throughout the range i.e. when the whole data is retreived and wh...
  3. URGENT-Oracle ResultSets
    If I have a table that contains about 1 Million rows that I am trying to retrieve, however the retrieval needs to be chunks without using the primary key, what type of queriy do I need to write. If I use rownum, the values become incorrect. For example if I say select * fro...
  4. Rownum
    Is there an equivalent of 'rownum' (Oracle) when retrieving data from a table in SQL Server?
  5. Rownum
    select * from table where rownum = 4 no rows. select * from table where rownum <= 4 4 rows returned select * from table where rownum = 1 1 row returned select * from table where rownum = 2 no rows returned. ?
  6. behaviour of rownum
    hi, this problem is generated in mysterious situation. some peaple make a table with columns empno and salary. and update it without condition thus there empno becomes 123 and then update salary that is 5000. now the situation arise to differentiate them.they added new column...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

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.

Join the Community

Answers

 

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  

 

by: sdstuberPosted on 2009-10-01 at 08:38:27ID: 25469863

never trust an "implicit" sort in any version
if you want your data sorted a certain way, then force it with order by

 

by: mrjoltcolaPosted on 2009-10-01 at 08:40:56ID: 25469888

OR use a cluster or index organized table.

 

by: sdstuberPosted on 2009-10-01 at 08:46:10ID: 25469957

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




 

by: mrjoltcolaPosted on 2009-10-01 at 08:50:37ID: 25469998

No question, it is implicit, but it will still order the data. A "normal" index or table scan will result in ordering by the index key.

I agree the only guaranteed way is to use explicit ORDER BY, but still consider the default on a normal query will be a old fashioned scan.

 

by: mrjoltcolaPosted on 2009-10-01 at 08:54:22ID: 25470034

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.

 

by: sdstuberPosted on 2009-10-01 at 09:00:40ID: 25470110

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



 

by: sdstuberPosted on 2009-10-01 at 09:08:49ID: 25470181

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.







SQL> select word from wordlist ;
 
35369 rows selected.
 
 
Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
       2594  consistent gets
          0  physical reads
          0  redo size
     553160  bytes sent via SQL*Net to client
      26300  bytes received via SQL*Net from client
       2359  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      35369  rows processed
 
SQL> select word from wordlist order by word;
 
35369 rows selected.
 
 
Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
        250  consistent gets
          0  physical reads
          0  redo size
     553160  bytes sent via SQL*Net to client
      26300  bytes received via SQL*Net from client
       2359  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
      35369  rows processed
 
SQL>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:

Select allOpen in new window

 

by: mrjoltcolaPosted on 2009-10-01 at 09:13:39ID: 25470226

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.

 

by: awking00Posted on 2009-10-01 at 11:09:59ID: 25471459

As an aside to this conversation, I think that, since rownum is a pseudocolumn of the result set of a query and not of the table, it is also unreliable, especially when where clauses and/or order by clauses are applied.

 

by: badtz7229Posted on 2009-10-01 at 11:24:29ID: 25471612

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. ?

 

by: sdstuberPosted on 2009-10-01 at 11:27:04ID: 25471648

I'm sorry but you can't modify anything in the db to make it guaranteed.

Your only guaranteed ordering is putting an order by on the query.

 

by: mrjoltcolaPosted on 2009-10-01 at 11:31:10ID: 25471698

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.

 

by: sdstuberPosted on 2009-10-01 at 11:44:54ID: 25471890

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      ------------------------------------------------------------------------------------

 

by: sdstuberPosted on 2009-10-01 at 11:46:20ID: 25471905

so,  the only way to get your OUTPUT RESULTS ordered is to use an ORDER BY query.

even if you physically order the data in your table,
you can't guarantee it will be returned in that order without use of an order by

 

by: badtz7229Posted on 2009-10-04 at 18:44:14ID: 31635935

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.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...