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
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
ORDER_DATE = ORDER_DATE
Local Prefixed Index Structure
ORDERS TABLE
Table partition
Table partition
Table partition
Table partition
LOCAL INDEX
First column
Second column
Index column sequence: (ORDER_DATE, CUSTOMER_ID)
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?
- Oracle checks the
ORDER_DATEpartition key. - It identifies the February partition.
- It eliminates unnecessary partitions using partition pruning.
- It can use the corresponding local index partition to find the matching customer record.
This can improve query performance.
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 |
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 |
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.
The best choice depends on actual query patterns, data distribution, index selectivity, and execution plans.
7. Interview-Ready Answer
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.
= Partition Key as First Index Column
Local Non-Prefixed Index in Oracle with Example.
No comments:
Post a Comment