Partition Pruning is an important Oracle Database optimization technique that improves query performance by reducing the number of partitions Oracle needs to scan to retrieve the required data.

Instead of scanning the entire partitioned table, Oracle accesses only the relevant partitions based on the query's filter conditions.

This helps reduce:

  • Disk I/O operations.
  • Data blocks read from storage.
  • CPU usage.
  • Query execution time.
  • Overall resource consumption.

Let's understand how partition pruning works with a practical Oracle SQL example.

1. Create a Partitioned Table

Suppose we have an employee table containing 10 million records, partitioned by year using the JOIN_DATE column.

CREATE TABLE employee (
    emp_id     NUMBER,
    emp_name   VARCHAR2(100),
    salary     NUMBER,
    join_date  DATE
)
PARTITION BY RANGE (join_date)
(
    PARTITION p2021 VALUES LESS THAN (DATE '2022-01-01'),
    PARTITION p2022 VALUES LESS THAN (DATE '2023-01-01'),
    PARTITION p2023 VALUES LESS THAN (DATE '2024-01-01'),
    PARTITION p2024 VALUES LESS THAN (DATE '2025-01-01'),
    PARTITION p2025 VALUES LESS THAN (DATE '2026-01-01')
);

Assume the data is distributed equally across all five partitions.

Partition Records
P202120 lakh
P202220 lakh
P202320 lakh
P202420 lakh
P202520 lakh
Total1 crore

2. Query Without Partition Pruning

Consider the following SQL query:

SELECT *
FROM employee
WHERE TO_CHAR(join_date, 'YYYY') = '2024';

Here, we are applying the TO_CHAR() function to the partition key JOIN_DATE.

Important: Because the partition key is wrapped inside a function, Oracle may not be able to derive the required partition range from this predicate. Consequently, it may scan all five partitions.

In this scenario, Oracle may examine up to 10 million records.

5 Partitions All partitions may be scanned.

10 million records

3. Query With Partition Pruning

Now rewrite the query using a date range condition.

SELECT *
FROM employee
WHERE join_date >= DATE '2024-01-01'
AND join_date < DATE '2025-01-01';

Now Oracle can identify that the required records belong to the P2024 partition.

Partition Pruning: Oracle can eliminate the unnecessary partitions and access only P2024.

Instead of scanning all five partitions, Oracle accesses only one relevant partition.

10 Million Without partition pruning

5 partitions

2 Million With partition pruning

1 partition

In this example, the number of records to examine is reduced by approximately 80%, assuming equal data distribution and a full scan of the selected partitions.

Note: This is an illustrative comparison. Actual disk I/O, blocks read, and execution time depend on the execution plan, indexes, data distribution, and storage characteristics.

4. How Does Oracle Achieve Partition Pruning Internally?

When Oracle receives a query, it performs the following steps:

  1. Checks the WHERE clause conditions.
  2. Compares the conditions with the table's partition key.
  3. Identifies the relevant partition or partitions.
  4. Eliminates unnecessary partitions from the scan.
  5. Reads data from the selected partitions.

For example, consider this query condition:

WHERE join_date >= DATE '2024-01-01'
  AND join_date < DATE '2025-01-01'

Oracle identifies the partition selection as follows:

P2021SKIP
P2022SKIP
P2023SKIP
P2024READ ✓
P2025SKIP
Result: Oracle reads only P2024 and skips the other four partitions.

5. How to Verify Partition Pruning?

We can verify partition pruning using Oracle's EXPLAIN PLAN and DBMS_XPLAN.DISPLAY.

Execute the following SQL statements:

EXPLAIN PLAN FOR
SELECT *
FROM employee
WHERE join_date >= DATE '2024-01-01'
AND join_date < DATE '2025-01-01';

SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);

Look for the following operations in the execution plan:

Operation Meaning
PARTITION RANGE SINGLE One partition accessed.
PARTITION RANGE ITERATOR Multiple selected partitions accessed.
PARTITION RANGE ALL All partitions accessed.

Also check the PSTART and PSTOP columns in the execution plan.

  • PSTART: Starting partition selected by Oracle.
  • PSTOP: Ending partition selected by Oracle.
Interview Tip: For a single partition access, you may see PARTITION RANGE SINGLE. For multiple partitions, you may see PARTITION RANGE ITERATOR. The actual operation depends on the query and partitioning strategy.

6. Important Oracle Partition Pruning Interview Questions

Q1. Does Partition Pruning Always Improve Performance?

Answer: No, not always.

Partition pruning may provide little or no benefit in the following scenarios:

  • If the query needs data from all partitions, pruning cannot eliminate partitions.
  • If the query filters on a non-partitioning column, Oracle may not be able to prune partitions.
  • If the query retrieves only a small number of rows, an appropriate index may be more efficient than a full partition scan.

Q2. Do We Need an Index for Partition Pruning?

Answer: No.

An index is not mandatory for partition pruning. Oracle can perform partition pruning even when using a full table scan.

Partition pruning identifies which partitions need to be accessed, while indexes help locate rows within the accessed data.

Q3. What Is the Difference Between Partition Pruning and Index Range Scan?

Partition pruning eliminates unnecessary partitions, whereas an index range scan retrieves qualifying rows through an index.

Both techniques can work together to improve query performance.

Final Takeaway

Remember This Simple Example

  • Without partition pruning: Oracle may scan 10 million records.
  • With partition pruning: Oracle scans only the relevant 2 million records.
  • Result: Less I/O, fewer blocks read, and potentially faster query execution.

Partitioning + Partition Pruning = Reduced Data Scanning + Potentially Better Query Performance.

Understanding partition pruning is essential for every Oracle DBA and SQL developer because it helps in identifying unnecessary partition scans and optimizing SQL queries.

Next Topic: Partition Pruning vs Index Range Scan in Oracle.

What is partition pruning

Partition Pruning in Oracle is an optimization technique in which Oracle accesses only the required partitions instead of scanning the entire partitioned table.

In simple words, Oracle skips unnecessary partitions and scans only the partitions that contain the required data.

This improves query performance and reduces I/O operations.

1. Example: Create a Partitioned Table

Let's create an employee table partitioned by JOIN_DATE.

SQL - Create Partitioned Table
CREATE TABLE employee (
    emp_id     NUMBER,
    emp_name   VARCHAR2(100),
    salary     NUMBER,
    join_date  DATE
)
PARTITION BY RANGE (join_date)
(
    PARTITION p2022 VALUES LESS THAN (DATE '2023-01-01'),
    PARTITION p2023 VALUES LESS THAN (DATE '2024-01-01'),
    PARTITION p2024 VALUES LESS THAN (DATE '2025-01-01'),
    PARTITION p2025 VALUES LESS THAN (DATE '2026-01-01')
);

Here, data is divided into four partitions:

Partition Data Range
P2022 Before 2023-01-01
P2023 2023 calendar year
P2024 2024 calendar year
P2025 2025 calendar year

2. Without Partition Pruning

Consider this query:

SQL Query
SELECT *
FROM employee;

Oracle needs to access all four partitions because there is no filter condition to eliminate any partition.

All partitions scanned
P2022 2022 Data SCANNED
P2023 2023 Data SCANNED
P2024 2024 Data SCANNED
P2025 2025 Data SCANNED

3. With Partition Pruning

Now consider this query:

SQL Query
SELECT *
FROM employee
WHERE join_date >= DATE '2024-01-01'
AND join_date < DATE '2025-01-01';

What happens?

Oracle identifies that the required data exists in the P2024 partition.

Therefore, Oracle accesses only P2024 and skips the other partitions.

Partition Pruning Visualization
P2022 2022 Data ✕ SKIPPED
P2023 2023 Data ✕ SKIPPED
P2024 2024 Data ✓ SCANNED
P2025 2025 Data ✕ SKIPPED
Only 1 out of 4 partitions accessed.
This is called Partition Pruning.

4. Types of Partition Pruning

Oracle supports two major types:

A. Static Partition Pruning

Oracle identifies the required partitions at SQL parse time because the partition key value is known.

Example - Static Partition Pruning
SELECT *
FROM employee
WHERE join_date = DATE '2024-06-15';

Oracle can identify the relevant partition (P2024) before executing the query.

Key Point: The partition key value is known at parse time.

B. Dynamic Partition Pruning

Oracle determines which partitions to access during execution.

This can happen when partition key values are obtained dynamically, for example, from a bind variable or a subquery.

Example - Dynamic Partition Pruning
SELECT *
FROM employee
WHERE join_date = :input_date;

The actual partition is determined using the bind variable value during execution.

Key Point: The partition is determined dynamically during execution.

5. How to Check Partition Pruning in Oracle?

You can use EXPLAIN PLAN to check whether Oracle is accessing the required partitions.

SQL - Explain Plan
EXPLAIN PLAN FOR
SELECT *
FROM employee
WHERE join_date >= DATE '2024-01-01'
AND join_date < DATE '2025-01-01';

SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);

Important Execution Plan Operations

Operation Meaning
PARTITION RANGE SINGLE Only one partition accessed.
PARTITION RANGE ITERATOR A range of partitions accessed.
PARTITION RANGE ALL All partitions accessed.

Also check PSTART and PSTOP columns in the execution plan to identify which partitions Oracle accesses.

Important: PSTART and PSTOP help identify the starting and ending partition boundaries. For dynamic pruning, you may see KEY values in the execution plan.

6. Important Interview Questions

Q1. What is the main benefit of partition pruning?
It reduces unnecessary partition scans, I/O operations and query execution time.
Q2. Is an index mandatory for partition pruning?
No. Partition pruning can happen even without an index.
Q3. Does partition pruning work with full table scans?
Yes. Oracle can perform a full table scan on only the required partition.
Q4. Can partition pruning happen with bind variables?
Yes. Oracle can perform dynamic partition pruning using bind variable values.

Interview Takeaway

Partition Pruning = Oracle scans only the required partitions and skips unnecessary partitions based on the partition key condition.

It is one of the most important performance benefits of partitioning in Oracle.

If you want, I can also demonstrate Static vs Dynamic Partition Pruning with actual Oracle execution plans and sample output.

Can we create a unique index on a partitioned table

Can we insert duplicate values into different partitions of an Oracle partitioned table when using LOCAL and GLOBAL UNIQUE INDEXES?

This is an important Oracle interview question. The behavior is different depending on whether we use a LOCAL or GLOBAL unique index.

Let's understand this concept with practical SQL INSERT examples.

1. Create a Partitioned Employee Table

First, create an EMPLOYEE table partitioned by JOIN_DATE.

CREATE TABLE employee (
    emp_id     NUMBER,
    emp_name   VARCHAR2(100),
    salary     NUMBER,
    join_date  DATE
)
PARTITION BY RANGE (join_date)
(
    PARTITION p2024
    VALUES LESS THAN (DATE '2025-01-01'),

    PARTITION p2025
    VALUES LESS THAN (DATE '2026-01-01'),

    PARTITION pmax
    VALUES LESS THAN (MAXVALUE)
);

Here are the table details:

  • Table name: EMPLOYEE
  • Partitioning column: JOIN_DATE
  • Partitioning method: RANGE
  • Partitions: P2024, P2025 and PMAX

2. Create a GLOBAL UNIQUE INDEX

Suppose we want EMP_ID to be unique across the entire EMPLOYEE table.

CREATE UNIQUE INDEX emp_uidx
ON employee (emp_id)
GLOBAL;
Result: Allowed

Oracle allows this because a GLOBAL unique index can enforce uniqueness across all table partitions without including the partitioning column.

3. Insert Initial Records

Let's insert two employees into different partitions.

INSERT INTO employee
VALUES (101, 'John', 50000,
        DATE '2024-06-15');

INSERT INTO employee
VALUES (102, 'David', 60000,
        DATE '2025-03-10');

COMMIT;

These records are stored in different partitions.

EMP_ID EMP_NAME JOIN_DATE Partition
101 John 15-JUN-2024 P2024
102 David 10-MAR-2025 P2025

4. Try to Insert a Duplicate EMP_ID

Now, try inserting EMP_ID = 101 into another partition.

INSERT INTO employee
VALUES (101, 'Peter', 70000,
        DATE '2025-05-15');
Result: INSERT Fails

Oracle raises a unique constraint violation because EMP_ID = 101 already exists in another partition.

ORA-00001: unique constraint violated

Why Does This Happen?

  • EMP_ID is the unique index column.
  • The index is GLOBAL.
  • Global uniqueness is checked across all partitions.
  • Therefore, duplicate EMP_ID values are not allowed anywhere in the table.
Important:

A GLOBAL UNIQUE INDEX guarantees uniqueness across the complete partitioned table, not just within an individual partition.

5. Create a LOCAL UNIQUE INDEX

Now let's understand the behavior of a LOCAL UNIQUE INDEX.

First, drop the previous global index.

DROP INDEX emp_uidx;

Create a LOCAL unique index on EMP_ID and JOIN_DATE.

CREATE UNIQUE INDEX emp_uidx_local
ON employee (emp_id, join_date)
LOCAL;
Result: Allowed

This index is allowed because JOIN_DATE, the table partitioning column, is included in the unique index key.

6. Insert Duplicate EMP_ID Values with Different JOIN_DATE Values

Let's insert the following records.

INSERT INTO employee
VALUES (101, 'John', 50000,
        DATE '2024-06-15');

INSERT INTO employee
VALUES (101, 'Peter', 70000,
        DATE '2025-05-15');

COMMIT;
Result: Both INSERT Statements Succeed

Why? Because the complete unique key combinations are different.

EMP_ID EMP_NAME JOIN_DATE Partition
101 John 15-JUN-2024 P2024
101 Peter 15-MAY-2025 P2025

Why Are Duplicate EMP_ID Values Allowed?

The LOCAL unique index is defined on:

(EMP_ID, JOIN_DATE)

Therefore, Oracle checks the complete combination rather than EMP_ID alone.

The two combinations are:

  • (101, 15-JUN-2024)
  • (101, 15-MAY-2025)

Since the complete combinations are different, both records are allowed.

Remember:

A composite unique index guarantees uniqueness of the complete combination. It does not guarantee that every individual column is unique.

7. Try to Insert the Same Composite Key Again

Now try inserting exactly the same EMP_ID and JOIN_DATE combination.

INSERT INTO employee
VALUES (101, 'Smith', 80000,
        DATE '2025-05-15');
Result: INSERT Fails

Oracle raises ORA-00001 because the combination (101, 15-MAY-2025) already exists.

Even though the table is partitioned, a LOCAL unique index prevents duplicate complete key combinations within the relevant partition.

8. What Happens If We Create a LOCAL UNIQUE INDEX on EMP_ID Alone?

Consider the following statement:

CREATE UNIQUE INDEX emp_uidx_local
ON employee (emp_id)
LOCAL;
Result: Not Allowed

Oracle rejects this statement because the LOCAL unique index does not include the table partitioning column JOIN_DATE.

A LOCAL unique index must include all partitioning key columns in its unique key.

9. LOCAL vs GLOBAL UNIQUE INDEX: INSERT Behavior

Scenario LOCAL UNIQUE GLOBAL UNIQUE
Duplicate EMP_ID in different partitions Allowed if JOIN_DATE differs Rejected
Duplicate EMP_ID and JOIN_DATE in same partition Rejected Rejected when indexed columns match
Unique key excludes partitioning column Not allowed Allowed
Unique key includes partitioning column Allowed Allowed

10. Important Oracle Interview Questions

Question 1: Can duplicate EMP_ID values exist in different partitions with a GLOBAL UNIQUE INDEX?

Answer: No. A GLOBAL unique index enforces uniqueness across the entire table.


Question 2: Can duplicate EMP_ID values exist in different partitions with a LOCAL UNIQUE INDEX?

Answer: Yes, provided the complete unique key combinations are different. For example, (101, 2024-06-15) and (101, 2025-05-15).


Question 3: Can we create a LOCAL UNIQUE INDEX on EMP_ID alone when the table is partitioned by JOIN_DATE?

Answer: No. The LOCAL unique index must include JOIN_DATE.


Question 4: Does a composite LOCAL UNIQUE INDEX guarantee EMP_ID uniqueness?

Answer: No. It guarantees uniqueness of the complete combination of EMP_ID and JOIN_DATE.


Question 5: Which index should we use if EMP_ID must be globally unique?

Answer: Use a GLOBAL unique index on EMP_ID, or another suitable global uniqueness design.

11. Test Your Understanding

Interview Question:

An EMPLOYEE table is partitioned by JOIN_DATE. A LOCAL UNIQUE INDEX is created on (EMP_ID, JOIN_DATE).

Two records are inserted:

INSERT INTO employee
VALUES (101, 'John', 50000,
        DATE '2024-06-15');

INSERT INTO employee
VALUES (101, 'Peter', 70000,
        DATE '2025-05-15');

What will happen?

12. Final Conclusion

Remember These Important Rules

1. A GLOBAL UNIQUE INDEX enforces uniqueness across all partitions.

2. A LOCAL UNIQUE INDEX must include all table partitioning key columns.

3. Duplicate individual EMP_ID values can exist with a composite LOCAL UNIQUE INDEX if the complete key combinations differ.

4. Duplicate complete key combinations are rejected by both LOCAL and GLOBAL unique indexes.

GLOBAL UNIQUE INDEX = Global Uniqueness
LOCAL UNIQUE INDEX = Partition-Aligned Uniqueness

Understanding these INSERT examples will help you answer Oracle partitioning and indexing interview questions confidently.

Next Topic: What Happens to LOCAL and GLOBAL Indexes When We DROP or TRUNCATE a Partition in Oracle?

Can we create a primary key on a partitioned table

Yes! Oracle supports primary keys on partitioned tables. However, there is an important restriction depending on whether we use a LOCAL or GLOBAL unique index.

The table partitioning column determines whether a local unique index can enforce the primary key.

Let's understand with practical examples.

1. Create a Partitioned Table

Suppose we create an EMPLOYEE table partitioned by JOIN_DATE.

Creating the Table

CREATE TABLE employee (
    emp_id     NUMBER,
    emp_name   VARCHAR2(100),
    salary     NUMBER,
    join_date  DATE
)
PARTITION BY RANGE (join_date)
(
    PARTITION p2024
    VALUES LESS THAN (DATE '2025-01-01'),

    PARTITION p2025
    VALUES LESS THAN (DATE '2026-01-01'),

    PARTITION pmax
    VALUES LESS THAN (MAXVALUE)
);

Here:

  • Table name = EMPLOYEE
  • Partition key = JOIN_DATE
  • Partitioning method = RANGE
  • Number of partitions = 3
Remember:

Our EMPLOYEE table is partitioned using JOIN_DATE.

Now let's create a primary key on EMP_ID.

2. Create a Primary Key on EMP_ID Only

Suppose we want EMP_ID to be the primary key of the EMPLOYEE table.

ALTER TABLE employee
ADD CONSTRAINT emp_pk
PRIMARY KEY (emp_id);

Will This Work?

Yes! Oracle can create a GLOBAL unique index to enforce this primary key.

Why does Oracle need a global index?

  • Table partition key = JOIN_DATE.
  • Primary key column = EMP_ID.
  • EMP_ID does not include the table partitioning column.
  • A LOCAL unique index on EMP_ID alone cannot enforce uniqueness across all table partitions.

Therefore, Oracle can use a global unique index to enforce the primary key.

GLOBAL UNIQUE INDEX

Enforces EMP_ID uniqueness across all table partitions.

Important:

A GLOBAL unique index can enforce uniqueness across the entire partitioned table, even when the primary key does not contain the table partitioning column.

3. Create a Primary Key Including the Partitioning Column

Now consider a different scenario.

We include both EMP_ID and JOIN_DATE in the primary key.

ALTER TABLE employee
ADD CONSTRAINT emp_pk
PRIMARY KEY (emp_id, join_date);

Will This Work?

Yes! In this case, Oracle can use a LOCAL unique index because the primary key includes the table partitioning column.

Let's explicitly create the local unique index.

CREATE UNIQUE INDEX emp_pk_idx
ON employee (emp_id, join_date)
LOCAL;

Now add the primary key constraint using the existing index.

ALTER TABLE employee
ADD CONSTRAINT emp_pk
PRIMARY KEY (emp_id, join_date)
USING INDEX emp_pk_idx;

Why Does This Work?

  • Table partition key = JOIN_DATE.
  • Primary key columns = EMP_ID, JOIN_DATE.
  • The partitioning column is included in the primary key.
  • Each local unique index partition can enforce uniqueness within its corresponding table partition.
Important Rule:

A LOCAL unique index on a partitioned table must include all the table partitioning key columns in its index key.

4. LOCAL vs GLOBAL Index for Primary Keys

Let's compare LOCAL and GLOBAL unique indexes.

Feature LOCAL Unique Index GLOBAL Unique Index
Partitioning Follows table partitions Independent of table partitions or nonpartitioned
Partition key requirement Must include all table partitioning key columns No such requirement
Uniqueness Enforced within each index partition Enforced across the entire table
Primary key without partition key Not supported with a local unique index Supported
Partition maintenance Generally easier May require additional maintenance
Interview Point:

If the primary key does not include the table partitioning column, Oracle needs a GLOBAL unique index to enforce uniqueness across all partitions.

If the primary key includes all partitioning key columns, a LOCAL unique index is possible.

5. What Happens When We Insert Duplicate EMP_ID Values?

This is an important practical example.

Suppose the table has the following records:

EMP_ID EMP_NAME JOIN_DATE
101 John 15-JAN-2024
102 David 10-FEB-2025
103 Smith 20-MAR-2025

Now try inserting the same EMP_ID into another partition.

INSERT INTO employee
VALUES (101, 'Peter', 50000, DATE '2025-05-15');

What Happens?

If the primary key is defined as:

PRIMARY KEY (emp_id)

Oracle rejects the duplicate EMP_ID, even though the new record belongs to a different table partition.

This is because the global unique index enforces uniqueness across the entire table.

However, if the primary key is:

PRIMARY KEY (emp_id, join_date)

The following two values are different composite keys:

  • (101, 15-JAN-2024)
  • (101, 15-MAY-2025)

Therefore, both records can exist because the complete primary key combinations are different.

Remember:

A composite primary key guarantees uniqueness of the complete combination, not necessarily uniqueness of each individual column.

6. Real-Time Example: Banking Application

Imagine a banking application with millions of employee or transaction records.

The table is partitioned using a date column.

PARTITION BY RANGE (transaction_date)

Now suppose the business requires a globally unique transaction ID.

We create a primary key on TRANSACTION_ID only.

ALTER TABLE transactions
ADD CONSTRAINT transactions_pk
PRIMARY KEY (transaction_id);

Oracle can use a GLOBAL unique index because TRANSACTION_ID does not include TRANSACTION_DATE.

Alternatively, if the primary key is defined as:

PRIMARY KEY (transaction_id, transaction_date)

A LOCAL unique index can be used because the partitioning column is included.

Real-Time Understanding:

Use a global unique index when uniqueness must be enforced across all partitions using a key that does not include the partitioning column.

Use a local unique index when the complete unique key includes all partitioning key columns.

7. How to Identify the Index Created for a Primary Key?

First, check the primary key constraint:

SELECT
    CONSTRAINT_NAME,
    TABLE_NAME,
    CONSTRAINT_TYPE,
    INDEX_NAME
FROM USER_CONSTRAINTS
WHERE TABLE_NAME = 'EMPLOYEE'
AND CONSTRAINT_TYPE = 'P';

Next, check the index information:

SELECT
    INDEX_NAME,
    TABLE_NAME,
    UNIQUENESS,
    PARTITIONED
FROM USER_INDEXES
WHERE TABLE_NAME = 'EMPLOYEE';

To check whether the index is LOCAL or GLOBAL:

SELECT
    INDEX_NAME,
    TABLE_NAME,
    LOCALITY,
    ALIGNMENT
FROM USER_PART_INDEXES
WHERE TABLE_NAME = 'EMPLOYEE';

These queries help identify the primary key constraint and its associated index.

8. Important Oracle Interview Questions

Question 1: Can we create a primary key on a partitioned table in Oracle?

Answer: Yes. Oracle supports primary keys on partitioned tables. The unique index used to enforce the constraint can be LOCAL or GLOBAL, subject to the partitioning key restrictions.


Question 2: Can we create a primary key on EMP_ID alone when the table is partitioned by JOIN_DATE?

Answer: Yes. Oracle can use a GLOBAL unique index because EMP_ID does not contain the table partitioning column.


Question 3: Can we create a LOCAL unique index on EMP_ID alone?

Answer: No. A LOCAL unique index must include all partitioning key columns.


Question 4: Can we create a LOCAL unique index on (EMP_ID, JOIN_DATE)?

Answer: Yes. Because JOIN_DATE is included in the unique index key.


Question 5: Which index enforces uniqueness across all partitions?

Answer: A GLOBAL unique index can enforce uniqueness across all partitions.

9. Test Your Understanding

Interview Question:

Your EMPLOYEE table is partitioned by JOIN_DATE.

You want to create a primary key on EMP_ID alone.

Which index can Oracle use to enforce this primary key?

10. Conclusion

Final Summary

Oracle supports primary keys on partitioned tables using LOCAL or GLOBAL unique indexes.

If the primary key does not include the table partitioning column, Oracle can use a GLOBAL unique index.

If the primary key includes all table partitioning key columns, Oracle can use a LOCAL unique index.

LOCAL UNIQUE INDEX = Includes All Table Partitioning Key Columns
GLOBAL UNIQUE INDEX = Can Enforce Uniqueness Across All Partitions

Understanding this restriction is important for Oracle database development, partition maintenance, and technical interviews.

Next Topic: What Happens to Primary Keys and Indexes When We Drop or Truncate a Partition in Oracle?

What is a global non-prefixed index

A GLOBAL NON-PREFIXED INDEX in Oracle is a global partitioned index in which the leading (first) column of the index is different from the index partition key.

In simple words:

If the first column of a global partitioned index is not the same as the index partitioning key, it is called a Global Non-Prefixed Index.

Let's understand with a practical example.

1. Create a Partitioned Table

Suppose we have an ORDERS table partitioned by 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
  • Table partitioning method = RANGE
  • Number of table partitions = 4
Remember:

The ORDERS table is partitioned using ORDER_DATE. Our global index will use a different partitioning key.

2. Create a Global Non-Prefixed Index

Now create a global partitioned index using CUSTOMER_ID as the index partition key.

CREATE INDEX idx_orders_global_np
ON orders (order_id, customer_id)
GLOBAL PARTITION BY RANGE (customer_id)
(
    PARTITION ip1 VALUES LESS THAN (1000),

    PARTITION ip2 VALUES LESS THAN (2000),

    PARTITION ip3 VALUES LESS THAN (3000),

    PARTITION ip4 VALUES LESS THAN (MAXVALUE)
);

Understand the Index Definition

Property Value
Table partition key ORDER_DATE
Index columns ORDER_ID, CUSTOMER_ID
Index partition key CUSTOMER_ID
Index partitioning method RANGE
Index type Global partitioned
Index structure Non-prefixed

Why Is This a Global Non-Prefixed Index?

  • Index partition key = CUSTOMER_ID.
  • First index column = ORDER_ID.
  • These columns are different.
Important:

The first index column does not match the global index partition key. Therefore, this is a Global Non-Prefixed Index.

3. How Does a Global Non-Prefixed Index Work?

ORDERS Table

Range partitioning using ORDER_DATE

P_JAN
January table partition
P_FEB
February table partition
P_MAR
March table partition
P_APR
April table partition

GLOBAL INDEX

Range partitioning using CUSTOMER_ID

IP1
CUSTOMER_ID less than 1000
IP2
CUSTOMER_ID from 1000 to less than 2000
IP3
CUSTOMER_ID from 2000 to less than 3000
IP4
CUSTOMER_ID 3000 and above

Independent partition structures:

The global index is partitioned by CUSTOMER_ID, but its first index column is ORDER_ID.

Notice:

  • Table partitions are based on ORDER_DATE.
  • Index partitions are based on CUSTOMER_ID.
  • Index column order is (ORDER_ID, CUSTOMER_ID).
  • There is no one-to-one relationship between table and index partitions.

A global index partition can contain index entries pointing to rows in multiple table partitions.

4. How Does Oracle Search This Index?

Suppose we execute the following query:

SELECT *
FROM orders
WHERE customer_id = 1500;

What Happens Internally?

  1. Oracle checks the global index partition boundaries.
  2. It can identify IP2 because CUSTOMER_ID 1500 falls between 1000 and 2000.
  3. Oracle can prune the other global index partitions.
  4. However, CUSTOMER_ID is not the leading index column.
  5. The index is ordered first by ORDER_ID and then by CUSTOMER_ID.
  6. Oracle may not be able to perform an efficient ordinary index range scan using CUSTOMER_ID alone.
Important Interview Point:

Even though Oracle can prune to the relevant global index partition, that does not mean it can efficiently search index entries using CUSTOMER_ID alone.

Index column order matters when determining whether an efficient index access path is available.

5. Global Prefixed vs Global Non-Prefixed Index

Suppose the global index partition key is CUSTOMER_ID.

Index Definition Index Type
(CUSTOMER_ID, ORDER_ID) Global Prefixed
(ORDER_ID, CUSTOMER_ID) Global Non-Prefixed
(CUSTOMER_ID) Global Prefixed
(ORDER_ID) Global Non-Prefixed

The difference is the position of the index partition key in the index columns.

Easy Memory Trick:

If the index partition key is the first index column, it is Global Prefixed.

If the index partition key is not the first index column, it is Global Non-Prefixed.

6. Local Non-Prefixed vs Global Non-Prefixed

Feature Local Non-Prefixed Global Non-Prefixed
Index type Local Global
Partition structure Follows table Independent
First index column Not table partition key Not global index partition key
Index partitions Correspond to table partitions Independently defined
Maintenance Generally easier May require additional maintenance

7. How to Identify Global Non-Prefixed Indexes?

First, check the index partitioning information:

SELECT
    INDEX_NAME,
    TABLE_NAME,
    LOCALITY,
    ALIGNMENT
FROM USER_PART_INDEXES
WHERE TABLE_NAME = 'ORDERS';

Next, check the index columns and their positions:

SELECT
    INDEX_NAME,
    COLUMN_NAME,
    COLUMN_POSITION
FROM USER_IND_COLUMNS
WHERE TABLE_NAME = 'ORDERS'
ORDER BY INDEX_NAME, COLUMN_POSITION;

Compare the first index column with the index partition key to determine whether the global index is prefixed or non-prefixed.

Remember:

USER_PART_INDEXES provides index locality and alignment information. USER_IND_COLUMNS provides the index column order. You need the index partitioning definition to identify the partition key.

8. Interview-Ready Answer

Interviewer Question:

What is a Global Non-Prefixed Index in Oracle?

Answer:

"A Global Non-Prefixed Index is a global partitioned index in which the leading column of the index is different from the index partitioning key.

For example, if we create a global range-partitioned index using CUSTOMER_ID as the partition key, but define the index columns as (ORDER_ID, CUSTOMER_ID), it becomes a Global Non-Prefixed Index.

Oracle maintains an independent index partition structure from the underlying table."

9. Test Your Understanding

Interview Question:

Your global index is partitioned by TRANSACTION_DATE.

Which definition represents a Global Non-Prefixed Index?

10. Conclusion

Final Summary

A Global Non-Prefixed Index in Oracle is a global partitioned index in which the first index column is different from the index partitioning key.

For example, if the global index partition key is CUSTOMER_ID and the index columns are (ORDER_ID, CUSTOMER_ID), it is a Global Non-Prefixed Index.

Unlike local indexes, global indexes maintain an independent partitioning structure from the underlying table.

GLOBAL NON-PREFIXED INDEX = First Index Column ≠ Index Partition Key

Next Topic: All Oracle Index Types with a Comparison Diagram.

What is a global prefixed index

A GLOBAL PREFIXED INDEX is a global partitioned index in which the leftmost (first) column of the index is the same as the partition key of the index.

In simple words:

If the first column of a global partitioned index matches its own index partitioning key, it is called a Global Prefixed Index.

Important difference: For a global index, we compare the first index column with the global index's partition key, not necessarily the table's partition key.

Let's understand with a practical example.

1. Create a Partitioned Table

Suppose we have an ORDERS table partitioned by 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
  • Table partitioning method = RANGE
  • Number of table partitions = 4
Remember:

Table partition key = ORDER_DATE

Table partitioning method = RANGE

Number of table partitions = 4

2. Create a Global Prefixed Index

Now create a global partitioned index on CUSTOMER_ID.

CREATE INDEX idx_orders_global
ON orders (customer_id, order_id)
GLOBAL PARTITION BY RANGE (customer_id)
(
    PARTITION ip1 VALUES LESS THAN (1000),
    PARTITION ip2 VALUES LESS THAN (2000),
    PARTITION ip3 VALUES LESS THAN (3000),
    PARTITION ip4 VALUES LESS THAN (MAXVALUE)
);

Understand the Index Definition

Property Value
Table partition key ORDER_DATE
Index columns CUSTOMER_ID, ORDER_ID
Index partition key CUSTOMER_ID
Index partitioning method RANGE
Index type Global partitioned
Index structure Prefixed

Why Is This a Global Prefixed Index?

Because:

  • Global index partition key = CUSTOMER_ID.
  • First index column = CUSTOMER_ID.
  • Both match.
Index Partition Key = CUSTOMER_ID

First Index Column = CUSTOMER_ID

Therefore, it is a Global Prefixed Index.

3. How Does a Global Prefixed Index Work?

ORDERS Table

Range partitioning on ORDER_DATE.

P_JAN

January table partition

P_FEB

February table partition

P_MAR

March table partition

P_APR

April table partition

GLOBAL INDEX

Range partitioning on CUSTOMER_ID.

IP1

CUSTOMER_ID less than 1000

IP2

CUSTOMER_ID from 1000 to less than 2000

IP3

CUSTOMER_ID from 2000 to less than 3000

IP4

CUSTOMER_ID 3000 and above

Global Prefixed Index

The index is partitioned independently from the table.

Each index partition can contain entries pointing to rows in multiple table partitions.

Notice the difference:

  • Table is divided based on ORDER_DATE.
  • Global index is divided based on CUSTOMER_ID.
  • The first index column matches the index partition key.
Important:

A Global Prefixed Index does not need to use the table partition key as its leading column. It must use its own index partition key as the leading column.

4. Example Query

Suppose we execute the following query:

SELECT *
FROM orders
WHERE customer_id = 1500;

What Happens Internally?

  1. Oracle checks the global index partition boundaries.
  2. It identifies IP2 because CUSTOMER_ID 1500 falls between 1000 and 2000.
  3. It searches the relevant global index partition.
  4. The index provides row locations (ROWIDs).
  5. Oracle accesses the corresponding table partitions to retrieve the records.

This can help when searching for customers across multiple monthly table partitions.

Performance Note:

A global prefixed index can support efficient index partition pruning when the query contains suitable predicates on the index partition key.

The optimizer decides whether this index is the most efficient access path.

5. What Is a Global Non-Prefixed Index?

Let's change the index column order.

CREATE INDEX idx_orders_global_np
ON orders (order_id, customer_id)
GLOBAL PARTITION BY RANGE (customer_id)
(
    PARTITION ip1 VALUES LESS THAN (1000),
    PARTITION ip2 VALUES LESS THAN (2000),
    PARTITION ip3 VALUES LESS THAN (3000),
    PARTITION ip4 VALUES LESS THAN (MAXVALUE)
);

Here:

  • Index partition key = CUSTOMER_ID.
  • First index column = ORDER_ID.
  • The first index column does not match the index partition key.

Therefore, this is a Global Non-Prefixed Index.

Index Partition Key = CUSTOMER_ID

First Index Column = ORDER_ID

GLOBAL NON-PREFIXED INDEX

6. Global Prefixed vs Global Non-Prefixed

This is an important Oracle interview comparison.

Feature Global Prefixed Global Non-Prefixed
Index partition key First index column Not the first index column
Global index Yes Yes
Partitioning structure Independent of table Independent of table
Index partition pruning Supported when query filters on index partition key May require scanning multiple index partitions
Main difference Column order matches Column order does not match

7. Local Prefixed vs Global Prefixed

This is particularly important for Oracle interviews.

Feature Local Prefixed Global Prefixed
Index partitioning Follows table partitioning Independent of table
Leading column Table partition key Index partition key
Partition key Same as table's partition key Can differ from table's partition key
Number of index partitions Corresponds to table partitions Independently defined
Maintenance Generally easier May require additional maintenance
Interview Tip:

For a local prefixed index, compare the leading index column with the table partition key.

For a global prefixed index, compare the leading index column with the global index partition key.

8. How to Identify a Global Prefixed Index?

Check the index partitioning information:

SELECT
    INDEX_NAME,
    TABLE_NAME,
    LOCALITY,
    ALIGNMENT
FROM USER_PART_INDEXES
WHERE TABLE_NAME = 'ORDERS';

Check the index columns:

SELECT
    INDEX_NAME,
    COLUMN_NAME,
    COLUMN_POSITION
FROM USER_IND_COLUMNS
WHERE TABLE_NAME = 'ORDERS'
ORDER BY INDEX_NAME, COLUMN_POSITION;

Compare the first index column with the index partition key to determine whether the global index is prefixed.

Remember:

USER_PART_INDEXES provides index locality and alignment information.

USER_IND_COLUMNS provides index column order.

For the exact partition key definition, examine USER_IND_PARTITIONS and the index DDL using DBMS_METADATA.GET_DDL where appropriate.

9. Interview-Ready Answer

Interviewer Question:

What is a Global Prefixed Index in Oracle?

Answer:

"A Global Prefixed Index is a global partitioned index in which the leading column of the index matches the index partitioning key.

For example, if an ORDERS table is partitioned by ORDER_DATE, we can create a global range-partitioned index on CUSTOMER_ID and ORDER_ID, with CUSTOMER_ID as the index partition key.

Since CUSTOMER_ID is also the first index column, it is a Global Prefixed Index.

Unlike a local index, its partitioning structure is independent of the underlying table's partitioning structure."

10. Test Your Understanding

Interview Question

Your global index is partitioned by CUSTOMER_ID.

Which index definition is a Global Prefixed Index?

Conclusion

Final Summary

A Global Prefixed Index in Oracle is a global partitioned index in which the first index column matches the index partition key.

  • The index has its own independent partitioning structure.
  • The index partition key can differ from the table partition key.
  • The first index column must match the global index partition key.
  • It can support index partition pruning when queries filter on the index partition key.
  • It is useful for queries accessing data across multiple table partitions.
GLOBAL PREFIXED INDEX
= First Index Column Matches Index Partition Key
Next Topic:

Global Non-Prefixed Index in Oracle with Practical Example.

What is a local non-prefixed index

A LOCAL NON-PREFIXED INDEX in Oracle is a local partitioned index in which the partition key of the table is NOT the first column in the index definition.

In simple words:

If the table's partition key is not the leading (first) column of the local index, it is called a Local Non-Prefixed Index.

Let's understand with a practical example.

1. Create a Partitioned Table

Suppose we have an ORDERS table partitioned by 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 partitions = 4
Remember:

Table partition key = ORDER_DATE

Partitioning method = RANGE

Number of partitions = 4

2. Create a Local Non-Prefixed Index

Now create an index with CUSTOMER_ID as the first column and ORDER_DATE as the second column.

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

Why Is This a Local Non-Prefixed Index?

Because:

  • Table partition key = ORDER_DATE
  • First index column = CUSTOMER_ID
  • Partition key is the second index column.
Table Partition Key = ORDER_DATE

First Index Column = CUSTOMER_ID

Therefore, it is a Local Non-Prefixed Index.

Local Non-Prefixed Index Structure

ORDERS TABLE

Partition Key: ORDER_DATE

Table partitioning column

LOCAL INDEX

CUSTOMER_ID

First column

ORDER_DATE

Second column - Partition key

Index Column Sequence:
(CUSTOMER_ID, ORDER_DATE)
Local Non-Prefixed Index

Oracle creates corresponding index partitions for the four table partitions.

Each local index partition contains entries for rows in its corresponding table partition.

3. How Does a Local Non-Prefixed Index Work?

Suppose we execute this query:

SELECT *
FROM orders
WHERE customer_id = 1001;

What Happens Internally?

  1. Oracle checks the local index partitions.
  2. Since CUSTOMER_ID is the leading index column, Oracle can use that column to search within each relevant local index partition.
  3. Because the query does not specify ORDER_DATE, Oracle generally needs to consider all table partitions.
  4. Oracle retrieves matching rows from the corresponding table partitions.

This is useful when customers have orders across multiple months.

Important:

A local non-prefixed index does not automatically mean that Oracle must scan every index partition in every query.

Partition pruning is still possible when the query contains suitable partition-key predicates.

4. Another Example: Query Using Both Columns

Now consider a query that uses both CUSTOMER_ID and ORDER_DATE.

SELECT *
FROM orders
WHERE customer_id = 1001
AND order_date >= DATE '2025-02-01'
AND order_date < DATE '2025-03-01';

What Happens Internally?

  1. Oracle identifies the February partition using ORDER_DATE.
  2. Other partitions can be eliminated through partition pruning.
  3. Oracle searches the corresponding local index partition using CUSTOMER_ID and ORDER_DATE.

This can reduce unnecessary index and table access.

Performance Note:

Because the index contains both columns, Oracle can use the leading column and the following column to access matching entries within the selected index partition.

5. Local Prefixed vs Local Non-Prefixed Index

This is a very important Oracle interview question.

Feature Local Prefixed Local Non-Prefixed
First index column Partition key Not partition key
Index partitioning Local Local
Partition pruning Supported Supported
Query by partition key Can be useful Can also be useful
Query by other indexed columns Depends on index order Often useful if those columns lead the index

Compare the SQL Statements

Suppose the table partition key is ORDER_DATE.

Local Prefixed Index:
CREATE INDEX idx1
ON orders (order_date, customer_id)
LOCAL;
Local Non-Prefixed Index:
CREATE INDEX idx2
ON orders (customer_id, order_date)
LOCAL;
The Difference Is Simply the Order of the Index Columns
Important Interview Point:

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

6. Real-Time Example: Banking Application

Imagine a banking application with 500 million transaction records.

Table Partitioning:

PARTITION BY RANGE (transaction_date)

Most application queries search using CUSTOMER_ID.

For example:

SELECT *
FROM transactions
WHERE customer_id = 1001;

A Local Non-Prefixed Index could be:

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

Why Use This Index?

  • Customers can have transactions across multiple months.
  • CUSTOMER_ID is the leading index column.
  • Each monthly partition has its own local index partition.
  • Oracle can search the relevant index partitions for that customer.
Real-Time Performance Note:

A local non-prefixed index may be useful when customer-based searches are common.

Whether this is better than a global index depends on workload, data distribution, partition count, and performance testing.

7. How to Identify Local Non-Prefixed Indexes?

You can check the index locality:

SELECT
    INDEX_NAME,
    TABLE_NAME,
    LOCALITY
FROM USER_PART_INDEXES
WHERE TABLE_NAME = 'ORDERS';

To check the index columns and their order:

SELECT
    INDEX_NAME,
    COLUMN_NAME,
    COLUMN_POSITION
FROM USER_IND_COLUMNS
WHERE TABLE_NAME = 'ORDERS'
ORDER BY INDEX_NAME, COLUMN_POSITION;

You need to compare the first index column with the table's partition key to identify whether the local index is prefixed or non-prefixed.

Remember:

USER_PART_INDEXES identifies index locality, while USER_IND_COLUMNS helps identify the index column order.

8. Interview-Ready Answer

Interviewer Question:

What is a Local Non-Prefixed Index in Oracle?

Answer:

"A Local Non-Prefixed Index is a local partitioned index in which the partition key of the underlying table is not the leading column of the index.

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

It is useful when application queries frequently search using CUSTOMER_ID, even when data is spread across multiple table partitions."

9. Test Your Understanding

Interview Question

Your Oracle table is partitioned by TRANSACTION_DATE.

Which index is a Local Non-Prefixed Index?

Conclusion

Final Summary

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

  • The partition key is not the leading index column.
  • The index follows the table partitioning structure.
  • Oracle creates corresponding local index partitions.
  • It can be useful when application queries frequently search using other indexed columns.
  • Partition pruning is still possible when suitable partition-key predicates are present.
  • Index column order should be selected based on actual query requirements.
LOCAL NON-PREFIXED INDEX
= Partition Key Is Not the First Index Column
Next Topic:

When Should We Use Local Prefixed vs Local Non-Prefixed Indexes in Oracle?