Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Fazeel.

Asked: March 22, 2011 - 11:43 pm UTC

Last updated: March 24, 2011 - 8:42 am UTC

Version: Any

Viewed 1000+ times

You Asked

We can use "ORDER BY" clause for sorting data when running a SELECT query.

But what if I want a permanent sorting on some columns in a Table ? i.e. By default it will sort on those columns. We should be able to set the criteria at the time of CREATE TABLE.

If there is an "ORDER BY" in the SQL then it should override the permanent criteria.

and Tom said...

Not in a create table.

You can create a view with an order by - any select against the view would return the data sorted..

HOWEVER, a query that joins that view or references another table in fact, might not be 'sorted' anymore.

So, you can create a view, applications would use the view not the table - but you would have to bear in mind that if they use more than just the view - the results might not be sorted, they might be sorted - it depends on the plan we decide to use.

Rating

  (4 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Sorted Hash Clusters

Rajeshwaran, Jeyabal, March 23, 2011 - 4:16 pm UTC

Tom:

By default it will sort on those columns. We should be able to set the criteria at the time of CREATE TABLE

How about using Sorted HASH clusters for this? I made a note about it.

http://rajeshwaranbtech.blogspot.com/2010/10/sorted-hash-clusters.html
Tom Kyte
March 23, 2011 - 4:53 pm UTC

without using an ORDER BY, the data returned from a sorted hash cluster is no more sorted than the data returned by an IOT. Meaning - it ain't sorted without an ORDER BY.

It might accidentally sometimes come back sorted - but there is definitely NO ASSURANCE or even a little hint of a promise that the data will be ordered by anything.

Note that even in your own examples - you have an ORDER BY - the optimizer is free to remove the order by step - but you ARE NOT free to not use the order by if you want the data ordered by something!!!!!

EVER.

IOTs??!!

A reader, March 23, 2011 - 9:01 pm UTC


Tom Kyte
March 24, 2011 - 8:12 am UTC

see the following comment by Duke - short answer is:

if you want data sorted, you better use order by - or it isn't guaranteed to be sorted.

view with order by

Sokrates, March 24, 2011 - 8:16 am UTC

You can create a view with an order by - any select against the view would return the data sorted..


Even if the select does not contain an order-by-clause ?
What's the reason for that ?
Tom Kyte
March 24, 2011 - 8:42 am UTC

because they added support for order by in a view in version 8.1.5.

I am not a fan of it personally. I myself would not rely on it - since if you use the view "in general", it doesn't have to be ordered. As soon as you join - all bets are off.

Proof:



ops$tkyte%ORA11GR2> create table t1 ( x int );

Table created.

ops$tkyte%ORA11GR2> insert into t1 values ( 1 );

1 row created.

ops$tkyte%ORA11GR2> insert into t1 values ( 2 );

1 row created.

ops$tkyte%ORA11GR2> insert into t1 values ( 3 );

1 row created.

ops$tkyte%ORA11GR2> 
ops$tkyte%ORA11GR2> create table t2 ( y int );

Table created.

ops$tkyte%ORA11GR2> insert into t2 values ( 1 );

1 row created.

ops$tkyte%ORA11GR2> insert into t2 values ( 2 );

1 row created.

ops$tkyte%ORA11GR2> insert into t2 values ( 3 );

1 row created.

ops$tkyte%ORA11GR2> 
ops$tkyte%ORA11GR2> create or replace view v1 as select * from t1 order by x;

View created.

ops$tkyte%ORA11GR2> create or replace view v2 as select * from t2 order by y DESC;

View created.

ops$tkyte%ORA11GR2> 
ops$tkyte%ORA11GR2> set autotrace on explain
ops$tkyte%ORA11GR2> select * from v1, v2 where v1.x = v2.y;

         X          Y
---------- ----------
         3          3
         2          2
         1          1


Execution Plan
----------------------------------------------------------
Plan hash value: 2678552646

------------------------------------------------------------------------------
| Id  | Operation             | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |      |     3 |    78 |     9  (34)| 00:00:01 |
|   1 |  SORT ORDER BY        |      |     3 |    78 |     9  (34)| 00:00:01 |
|*  2 |   HASH JOIN           |      |     3 |    78 |     8  (25)| 00:00:01 |
|   3 |    VIEW               | V1   |     3 |    39 |     4  (25)| 00:00:01 |
|   4 |     SORT ORDER BY     |      |     3 |    39 |     4  (25)| 00:00:01 |
|   5 |      TABLE ACCESS FULL| T1   |     3 |    39 |     3   (0)| 00:00:01 |
|   6 |    TABLE ACCESS FULL  | T2   |     3 |    39 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("V1"."X"="Y")



two views with competing order by's....