Is an index clustered or unclustered in Oracle?

13,785

Solution 1

By default all indexes in Oracle are unclustered. The only clustered indexes in Oracle are the Index-Organized tables (IOT) primary key indexes.

You can determine if a table is an IOT by looking at the IOT_TYPE column in the ALL_TABLES view (its primary key could be determined by querying the ALL_CONSTRAINTS and ALL_CONS_COLUMNS views).

Here are some reasons why your query might return ordered rows:

  1. Your table is index-organized and FIELD is the leading part of its primary key.
  2. Your table is heap-organized but the rows are by chance ordered by FIELD, this happens sometimes on an incrementing identity column.

Case 2 will return sorted rows only by chance. The order of the inserts is not guaranteed, furthermore Oracle is free to reuse old blocks if some happen to have available space in the future, disrupting the fragile ordering.

Case 1 will most of the time return ordered rows, however you shouldn't rely on it since the order of the rows returned depends upon the algorithm of the access path which may change in the future (or if you change DB parameter, especially parallelism).

In both case if you want ordered rows you should supply an ORDER BY clause:

SELECT field 
  FROM (SELECT field 
          FROM TABLE 
         ORDER BY field) 
 WHERE rownum <= 100;

Solution 2

There is no concept of a "clustered index" in Oracle as in SQL Server and Sybase. There is an Index-Organized Table, which is similar but not the same.

Solution 3

"Clustered" indices, as implemented in Sybase, MS SQL Server and possibly others, where rows are physically stored in the order of the indexed column(s) don't exist as such in Oracle. "Cluster" has a different meaning in Oracle, relating, I believe, to the way blocks and tables are organized.

Oracle does have "Index Organized Tables", which are physically equivalent, but they're used much less frequently because the query optimizer works differently.

The closest I can get to an answer to the identification question is to try something like this:

SELECT IOT_TYPE FROM user_tables
WHERE table_name = '<your table name>'

My 10g instance reports IOT or null accordingly.

Solution 4

Index Organized Tables have to be organized on the primary key. Where the primary key is a sequence generated value this is often useless or even counter-productive (because simultaneous inserts get into conflict for the same block).

Single table clusters can be used to group data with the same column value in the same database block(s). But they are not ordered.

Share:
13,785
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin over 1 year

    How can I determine if an Oracle index is clustered or unclustered?

    I've done

    select FIELD from TABLE where rownum <100
    

    where FIELD is the field on which is built the index. I have ordered tuples, but the result is wrong because the index is unclustered.