In Oracle, Local and Global indexes are two types of indexes used on partitioned tables.
The main difference is:
- Local Index: Index partitioning follows the table partitioning structure.
- Global Index: Index partitioning is independent of the table partitioning structure, or the index can be nonpartitioned.
Let's understand both indexes with practical examples and diagrams.
1. First, 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')
);
Our table has four partitions:
- P_JAN
- P_FEB
- P_MAR
- P_APR
Table partition key = ORDER_DATE
Table partitioning method = RANGE
2. Local Index Example
Suppose we frequently search orders using CUSTOMER_ID.
Create a local index:
CREATE INDEX idx_orders_local
ON orders (customer_id)
LOCAL;
What Happens Internally?
Oracle creates four corresponding index partitions because our table contains four partitions.
Local Index Structure
Table partitions and index partitions follow the same structure.
↓
↓
↓
↓
Each local index partition contains index entries for rows in its corresponding table partition.
Local indexes do not necessarily need to use the table's partition key as their index column.
Here, the index is on CUSTOMER_ID, while the table is partitioned by ORDER_DATE.
3. Global Index Example
Now create a global partitioned index on CUSTOMER_ID.
CREATE INDEX idx_orders_global
ON orders (customer_id)
GLOBAL PARTITION BY HASH (customer_id)
(
PARTITION idx_p1,
PARTITION idx_p2,
PARTITION idx_p3,
PARTITION idx_p4
);
Here:
- Table partition key =
ORDER_DATE - Global index partition key =
CUSTOMER_ID - Table partitioning method = RANGE
- Index partitioning method = HASH
See the Structure
Global Partitioned Index Structure
ORDERS Table
Range partition
Range partition
Range partition
Range partition
GLOBAL INDEX
Hash partition
Hash partition
Hash partition
Hash partition
A global index partition can contain entries pointing to rows in multiple table partitions.
Unlike local indexes, there is no one-to-one relationship between table and index partitions.
Local indexes follow the table partitioning structure. Global partitioned indexes have their own independent partitioning structure.
4. Global Nonpartitioned Index Example
There is another important type of global index.
A global nonpartitioned index is one index covering the entire partitioned table without dividing the index into partitions.
CREATE INDEX idx_orders_global_nonpart
ON orders (customer_id);
Notice that we didn't specify LOCAL or GLOBAL PARTITION BY.
Because the underlying table is partitioned, this creates a global nonpartitioned index.
5. Main Differences Between Local and Global Indexes
| Feature | Local Index | Global Index |
|---|---|---|
| Partition structure | Follows table | Independent or nonpartitioned |
| Partition relationship | Corresponding partitions | No one-to-one relationship |
| Index partition key | Follows table partitioning | Can be different |
| Partition maintenance | Generally easier | May need additional maintenance |
| Partition drop | Corresponding local index partition is handled | Global index may become unusable without maintenance |
| Best suited for | Partition-level operations | Queries across multiple table partitions |
6. Real-Time Example: Banking Application
Imagine a banking application with 500 million transaction records.
The table is partitioned monthly using TRANSACTION_DATE.
PARTITION BY RANGE (transaction_date)
Now consider two queries.
Query 1: Search Transactions for February
SELECT *
FROM transactions
WHERE transaction_date >= DATE '2025-02-01'
AND transaction_date < DATE '2025-03-01';
A local index can be useful because Oracle can prune the table to February and access the corresponding local index partition if appropriate.
Query 2: Search a Customer's Transactions Across All Months
SELECT *
FROM transactions
WHERE customer_id = 1001;
A global index on CUSTOMER_ID may be useful because it can locate matching entries across different table partitions.
The optimizer chooses the access path based on estimated cost.
A global index is not automatically faster than a local index for cross-partition queries.
7. What Happens When We Drop a Partition?
This is a very important Oracle DBA interview question.
Suppose we execute:
ALTER TABLE orders
DROP PARTITION p_jan;
With Local Index
- The corresponding local index partition is automatically removed along with the table partition.
- Other local index partitions remain available.
With Global Index
- The global index may become unusable if you don't maintain it.
- You can maintain it using
UPDATE GLOBAL INDEXES.
ALTER TABLE orders
DROP PARTITION p_jan
UPDATE GLOBAL INDEXES;
This maintains the global indexes during the partition drop operation.
Using UPDATE GLOBAL INDEXES maintains global indexes during supported partition maintenance operations instead of leaving them unusable.
8. How to Identify Local and Global Indexes?
Run this query:
SELECT
INDEX_NAME,
TABLE_NAME,
LOCALITY
FROM USER_PART_INDEXES
WHERE TABLE_NAME = 'ORDERS';
For a more complete view, check both partitioned and nonpartitioned indexes:
SELECT
INDEX_NAME,
TABLE_NAME,
PARTITIONED
FROM USER_INDEXES
WHERE TABLE_NAME = 'ORDERS';
Interpretation:
LOCALITY = LOCAL: Local partitioned index.LOCALITY = GLOBAL: Global partitioned index.PARTITIONED = NO: Nonpartitioned index, which is global if its table is partitioned.
9. Interview-Ready Answer
What is the difference between Local and Global Indexes in Oracle?
Answer:"A local index follows the partitioning structure of its underlying table. Each table partition has a corresponding local index partition.
A global index has an independent partitioning structure or can be nonpartitioned.
Local indexes are easier to maintain during partition-level operations, whereas global indexes are useful for queries that search across multiple table partitions.
Global indexes may require additional maintenance when table partitions are dropped or truncated."
10. Test Your Understanding
Interview Question
Your ORDERS table has 12 monthly partitions. You create a local index on CUSTOMER_ID.
How many corresponding index partitions will Oracle create?
Conclusion
Final Summary
Local and Global indexes are both important in Oracle partitioned tables.
- Local indexes follow the partitioning structure of the underlying table.
- Global partitioned indexes have an independent partitioning structure.
- Global nonpartitioned indexes cover the entire partitioned table without separate index partitions.
- Local indexes are generally easier to maintain during partition-level operations.
- Global indexes may require additional maintenance when table partitions are dropped or truncated.
GLOBAL = Independent or Nonpartitioned
Local Prefixed vs Local Non-Prefixed Indexes in Oracle.
No comments:
Post a Comment