What is a local prefixed index

A LOCAL PREFIXED INDEX in Oracle is a local partitioned index in which the leftmost column of the index is the same as the partition key of the table.

In simple words:

The partition key column must be the first column in the index definition.

Let's understand with a practical example.

1. Create a Partitioned Table

Suppose we have an ORDERS table partitioned using ORDER_DATE.

Creating the Table

CREATE TABLE orders (
    order_id NUMBER,
    order_date DATE,
    customer_id NUMBER,
    amount NUMBER
)
PARTITION BY RANGE (order_date)
(
    PARTITION p_jan
    VALUES LESS THAN (DATE '2025-02-01'),

    PARTITION p_feb
    VALUES LESS THAN (DATE '2025-03-01'),

    PARTITION p_mar
    VALUES LESS THAN (DATE '2025-04-01'),

    PARTITION p_apr
    VALUES LESS THAN (DATE '2025-05-01')
);

Here:

  • Table partition key = ORDER_DATE
  • Partitioning method = RANGE
  • Number of table partitions = 4
Remember:

Table partition key = ORDER_DATE

Partitioning method = RANGE

Number of partitions = 4

2. Create a Local Prefixed Index

Now create an index on ORDER_DATE and CUSTOMER_ID.

CREATE INDEX idx_orders_prefixed
ON orders (order_date, customer_id)
LOCAL;

Why Is This a Local Prefixed Index?

Because:

  • Table partition key = ORDER_DATE
  • First index column = ORDER_DATE
Table Partition Key = First Index Column

ORDER_DATE = ORDER_DATE

Local Prefixed Index Structure

ORDERS TABLE

P_JAN

Table partition

P_FEB

Table partition

P_MAR

Table partition

P_APR

Table partition

LOCAL INDEX

ORDER_DATE

First column

CUSTOMER_ID

Second column

Index column sequence: (ORDER_DATE, CUSTOMER_ID)

Local Prefixed Index

Oracle creates corresponding index partitions for each table partition.

Each index partition contains entries for its corresponding table partition.

3. What Is the Purpose of a Local Prefixed Index?

Suppose we execute this query:

SELECT *
FROM orders
WHERE order_date = DATE '2025-02-15'
AND customer_id = 1001;

What Happens Internally?

  1. Oracle checks the ORDER_DATE partition key.
  2. It identifies the February partition.
  3. It eliminates unnecessary partitions using partition pruning.
  4. It can use the corresponding local index partition to find the matching customer record.

This can improve query performance.

Performance Note:

Partition pruning helps Oracle eliminate unnecessary table partitions. The local index can then provide an efficient access path within the relevant partition.

4. What Is NOT a Local Prefixed Index?

Consider this index:

CREATE INDEX idx_orders_nonprefixed
ON orders (customer_id, order_date)
LOCAL;

Here:

  • Table partition key = ORDER_DATE
  • First index column = CUSTOMER_ID
  • The partition key is not the first column.

Therefore, this is a Local Non-Prefixed Index, not a Local Prefixed Index.

Compare Both

Index Definition Index Type
(ORDER_DATE, CUSTOMER_ID) Local Prefixed
(CUSTOMER_ID, ORDER_DATE) Local Non-Prefixed
(ORDER_DATE) Local Prefixed
(CUSTOMER_ID) Local Non-Prefixed
Important:

The position of the partition key in the index definition determines whether a local index is prefixed or non-prefixed.

5. Local Prefixed vs Local Non-Prefixed Index

Feature Local Prefixed Local Non-Prefixed
First index column Partition key Not partition key
Local index Yes Yes
Partition pruning Supported Supported
Useful for date-based queries Yes Depends on query
Partition key in index First position Can be later or absent
Important Interview Point:

Partition pruning is primarily determined by the query predicates on the table's partition key, not simply by whether the index is prefixed.

6. Real-Time Example: Banking Application

Imagine a banking transaction table.

  • Table partition key = TRANSACTION_DATE
  • Frequently searched columns = TRANSACTION_DATE, CUSTOMER_ID

If your queries commonly look like this:

SELECT *
FROM transactions
WHERE transaction_date = DATE '2025-02-15'
AND customer_id = 1001;

A local prefixed index is a suitable candidate:

CREATE INDEX idx_trans_date_customer
ON transactions (transaction_date, customer_id)
LOCAL;

But if your application frequently searches using only CUSTOMER_ID, an index starting with CUSTOMER_ID may be more appropriate:

CREATE INDEX idx_trans_customer_date
ON transactions (customer_id, transaction_date)
LOCAL;

This is a local non-prefixed index.

Real-Time Performance Note:

The best choice depends on actual query patterns, data distribution, index selectivity, and execution plans.

7. Interview-Ready Answer

Interviewer Question:

What is a Local Prefixed Index in Oracle?

Answer:

"A local prefixed index is a local partitioned index in which the partition key of the table is the leading column of the index.

For example, if an ORDERS table is partitioned by ORDER_DATE, an index on (ORDER_DATE, CUSTOMER_ID) is a local prefixed index.

It maintains the local partition structure and can support efficient access when queries filter using the partition key."

8. Test Your Understanding

Interview Question

Your Oracle table is partitioned by EMPLOYEE_DATE.

Which of the following is a Local Prefixed Index?

Conclusion

Final Summary

A Local Prefixed Index in Oracle is a local partitioned index in which the table partition key is the first column in the index definition.

  • The partition key must be the leading index column.
  • The index follows the table partitioning structure.
  • Oracle creates corresponding local index partitions.
  • It can be useful for queries filtering on the partition key and other indexed columns.
  • Index column order should be selected based on actual query requirements.
LOCAL PREFIXED INDEX
= Partition Key as First Index Column
Next Topic:

Local Non-Prefixed Index in Oracle with Example.

No comments:

Post a Comment