Performance Tuning  «Prev  Next»

Lesson 3Creating an Index-Organized Table
ObjectiveUnderstand and Apply the Syntax for Creating an Index-Organized Table in Oracle

Creating an Index-Organized Table in Oracle

An index-organized table (IOT) is a specialized table structure in Oracle where data is stored in a B-tree index structure, sorted by the primary key. Unlike a standard heap-organized table, which stores data in an unordered heap, an IOT combines the table and its primary key index into a single structure. This provides faster access to data when queried by the primary key but may impact performance for other operations.

Why Use an Index-Organized Table?

  • Performance Benefits: IOTs offer faster primary key-based lookups because the data is stored directly in the B-tree index, reducing the need for separate index lookups. This is ideal for queries that frequently filter or join on the primary key.
  • Storage Efficiency: Since the primary key index and table data are unified, IOTs can save storage space compared to a heap table with a separate index.
  • Use Cases: IOTs are best suited for:
    • Lookup tables or mapping tables (e.g., key-value stores).
    • Applications where most queries use the primary key (e.g., dictionary tables, document indexing).
    • Scenarios requiring sorted access to data by the primary key.
  • Drawbacks:
    • Slower DML operations (INSERT, UPDATE, DELETE) due to the need to maintain the B-tree structure.
    • Limited support for secondary indexes, which may perform less efficiently.
    • Increased complexity for managing overflow segments (explained below).

Syntax for Creating an Index-Organized Table

The syntax for creating an IOT is similar to that of a standard table, with the addition of the ORGANIZATION INDEX keywords. An IOT requires a primary key, as the data is physically stored in the order of the primary key. Below is the syntax:


Diagram: Creating an Index-Organized Table
The diagram illustrates the syntax, starting with CREATE TABLE, followed by the table name, column definitions, a primary key constraint, and the ORGANIZATION INDEX clause. Optional clauses like TABLESPACE, PCTTHRESHOLD, and OVERFLOW can be included for advanced configurations.

CREATE TABLE table_name (
  column_list 
  CONSTRAINT pk_name PRIMARY KEY (pk_column_list)
)
ORGANIZATION INDEX
[TABLESPACE tablespace_name]
[PCTTHRESHOLD percentage]
[OVERFLOW TABLESPACE overflow_tablespace_name];

CREATE TABLERequired keywords to initiate table creation.
table_nameUnique name for the table.
column_listList of columns with their data types.
CONSTRAINTIntroduces the primary key constraint, required for an IOT.
pk_nameName of the primary key constraint.
PRIMARY KEYSpecifies the primary key columns.
pk_column_listList of columns forming the primary key.
ORGANIZATION INDEXSpecifies that the table is an IOT, stored as a B-tree index.
TABLESPACEOptional: Specifies the tablespace for the IOT.
PCTTHRESHOLDOptional: Defines the percentage of an index block that a row can occupy before overflow is triggered.
OVERFLOW TABLESPACEOptional: Specifies a tablespace for storing row data that exceeds the PCTTHRESHOLD.

Example 1: Creating a Simple Index-Organized Table

This example creates a basic IOT for tracking sales figures, using a composite primary key:

CREATE TABLE sales_figures (
  store_id NUMBER,
  quarter INTEGER,
  month INTEGER,
  amount NUMBER,
  CONSTRAINT pk_sales_figures PRIMARY KEY (store_id, quarter, month)
)
ORGANIZATION INDEX;

This table is ideal for queries that retrieve sales data by store_id, quarter, and month, as the data is stored in sorted order by these columns.

Example 2: Creating an Advanced Index-Organized Table

This example demonstrates an IOT with overflow storage for a document indexing system:

CREATE TABLE admin_docindex (
  token CHAR(20),
  doc_id NUMBER,
  token_frequency NUMBER,
  token_offsets VARCHAR2(2000),
  CONSTRAINT pk_admin_docindex PRIMARY KEY (token, doc_id)
)
ORGANIZATION INDEX
TABLESPACE admin_tbs
PCTTHRESHOLD 20
OVERFLOW TABLESPACE admin_tbs2;

In this example:
  • The table admin_docindex stores document indexing data with a composite primary key (token, doc_id).
  • PCTTHRESHOLD 20 means that if a row exceeds 20% of the index block size, the excess columns (starting with token_offsets) are moved to an overflow segment.
  • The overflow segment is stored in the admin_tbs2 tablespace, improving performance for large rows.

Key Differences from Standard Tables

Compared to a standard heap-organized table:
  • An IOT requires a primary key constraint and the ORGANIZATION INDEX clause.
  • Data is stored in a B-tree index, sorted by the primary key, unlike the unordered storage of a heap-organized table (which uses ORGANIZATION HEAP, the default in Oracle).
  • The ORGANIZATION HEAP keyword is rarely used explicitly, as it is the default table structure, but it can be included for clarity in documentation.

Review Primary Key Concepts

When to Choose an IOT

Use an IOT when:

  • Queries primarily access data via the primary key (e.g., lookup tables).
  • You need sorted data access or compact storage.

Avoid IOTs when:

  • Frequent inserts, updates, or deletes are expected, as these are slower due to B-tree maintenance.
  • Secondary indexes are heavily accessed or non-unique key access is common.

The next lesson covers how to rebuild an index-organized table.

SEMrush Software 3 SEMrush Banner 3