A LOCAL PARTITIONED INDEX in Oracle is an index that is partitioned in the same way as its associated table.
In simple words:
For every table partition, Oracle maintains a corresponding index partition.
If a table has 4 partitions, its local index will also have 4 corresponding index partitions.
Let's understand with a real-time example.
1. Example: Create a Partitioned Table
Suppose we have an ORDERS table containing millions of records.
We partition the table based on 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')
);
Now our table contains four partitions:
- P_JAN
- P_FEB
- P_MAR
- P_APR
2. Create a Local Partitioned Index
Suppose we frequently search orders using CUSTOMER_ID.
We can create a local index:
CREATE INDEX idx_orders_customer
ON orders (customer_id)
LOCAL;
Here:
idx_orders_customer= Index name.customer_id= Index column.LOCAL= Creates a local partitioned index.
What Happens Internally?
Oracle automatically creates four index partitions corresponding to the four table partitions.
| Table Partition | Corresponding Index Partition |
|---|---|
| P_JAN | SYS-generated local index partition |
| P_FEB | SYS-generated local index partition |
| P_MAR | SYS-generated local index partition |
| P_APR | SYS-generated local index partition |
Oracle automatically creates and maintains the local index partitions. You do not need to manually create an index partition for every table partition.
3. How Does a Local Index Work?
Let's understand the relationship between table partitions and local index partitions.
Table Partitions
January table records
February table records
March table records
April table records
Corresponding Local Index Partitions
Index entries for January table records
Index entries for February table records
Index entries for March table records
Index entries for April table records
Each local index partition contains index entries corresponding to rows in its associated table partition.
For example:
- P_JAN has its own index partition.
- P_FEB has its own index partition.
- P_MAR has its own index partition.
- P_APR has its own index partition.
Oracle maintains this relationship automatically.
4. Example: Insert Data and Query
Let's insert some records into the ORDERS table.
INSERT INTO orders VALUES
(101, DATE '2025-01-15', 1001, 5000);
INSERT INTO orders VALUES
(102, DATE '2025-02-15', 1002, 7000);
INSERT INTO orders VALUES
(103, DATE '2025-03-15', 1001, 9000);
COMMIT;
Now execute the following query:
SELECT *
FROM orders
WHERE order_date >= DATE '2025-02-01'
AND order_date < DATE '2025-03-01'
AND customer_id = 1002;
What Happens Internally?
- Oracle identifies the February partition using ORDER_DATE.
- Oracle can eliminate the other table partitions through partition pruning.
- Oracle can use the corresponding local index partition on CUSTOMER_ID to find the matching row.
This can improve query performance by reducing the amount of data scanned.
The optimizer decides whether using the index is more efficient than a full partition scan. Index usage is not guaranteed for every query.
5. What Happens When We Add a New Table Partition?
Suppose we add a May partition.
ALTER TABLE orders
ADD PARTITION p_may
VALUES LESS THAN (DATE '2025-06-01');
Oracle automatically creates the corresponding local index partition.
You don't need to manually create another index partition.
Local indexes simplify partition maintenance because Oracle automatically manages the corresponding index partition when new table partitions are added.
6. How to Check Local Index Partitions?
Use the following query to check local index partitions:
SELECT
INDEX_NAME,
PARTITION_NAME,
STATUS
FROM USER_IND_PARTITIONS
WHERE INDEX_NAME = 'IDX_ORDERS_CUSTOMER';
To check whether the index is local:
SELECT
INDEX_NAME,
TABLE_NAME,
LOCALITY
FROM USER_PART_INDEXES
WHERE TABLE_NAME = 'ORDERS';
You should see LOCAL as the locality.
7. Advantages of Local Partitioned Indexes
Local partitioned indexes provide several advantages:
- Easy maintenance: When adding or dropping table partitions, corresponding local index partitions are managed automatically.
- Partition independence: Each index partition is associated with one table partition.
- Partition pruning: Oracle can limit index access to relevant partitions when the query filters on the table partition key.
- Manageability: Individual index partitions can be rebuilt independently.
- Useful for large tables: Especially transaction, sales, and historical tables.
8. Local Index vs Global Index
This is a very important Oracle Database interview topic.
| Feature | Local Index | Global Index |
|---|---|---|
| Partitioning | Follows table partitioning | Independent partitioning or nonpartitioned |
| Relationship | Corresponding partitions for table partitions | Not necessarily one-to-one |
| Maintenance | Generally easier | Can require additional maintenance |
| Partition independence | Strong | Less independent |
| Best suited for | Partition-wise maintenance | Queries across multiple table partitions |
Local indexes are generally easier to maintain when table partitions are added, dropped, or truncated. Global indexes can require additional maintenance depending on the partition operation and Oracle options used.
9. Interview-Ready Answer
Answer:
"A local partitioned index is an index that follows the partitioning structure of its associated table.
Each table partition has a corresponding index partition. Oracle automatically maintains this relationship when table partitions are added or dropped.
Local indexes are particularly useful for large partitioned tables because they simplify index maintenance and support efficient partition-level operations."
10. Remember These Three Concepts
Divides table data into multiple partitions.
Index partitions follow the table partitions.
Index partitioning is independent of the table's partitioning structure.
11. Conclusion
Final Summary
A Local Partitioned Index in Oracle is an index that follows the partitioning structure of its associated table.
For every table partition, Oracle maintains a corresponding local index partition.
For example, if an ORDERS table contains four partitions, its local index also contains four corresponding index partitions.
Local indexes are useful for large tables because they simplify index maintenance and support efficient partition-level operations.
No comments:
Post a Comment