In this method:
- First, Oracle divides the table into partitions based on specific values using List Partitioning.
- Then, each main partition is further divided into smaller subpartitions using a hashing algorithm.
1. Real-time Example
Suppose we are working on an employee management application.
We have an EMPLOYEES table containing 10 lakh records.
We want to divide the data using two conditions:
- First level: Divide employee records department-wise using
DEPARTMENT. - Second level: Distribute each department's records using Hash Partitioning based on
EMP_ID.
Example: Partition Structure
| Main Partition | Subpartitions |
|---|---|
| P_IT | 4 Hash Subpartitions |
| P_HR | 4 Hash Subpartitions |
| P_FINANCE | 4 Hash Subpartitions |
- List Partitioning separates employees department-wise.
- Hash Partitioning distributes employees within each department.
Reference: Oracle Partitioning in Oracle Database – Presentation
2. SQL Example
Let's create a List-Hash partitioned table in Oracle.
CREATE TABLE employees
(
emp_id NUMBER,
emp_name VARCHAR2(100),
department VARCHAR2(30),
salary NUMBER
)
PARTITION BY LIST (department)
SUBPARTITION BY HASH (emp_id)
SUBPARTITIONS 4
(
PARTITION p_it VALUES ('IT'),
PARTITION p_hr VALUES ('HR'),
PARTITION p_finance VALUES ('FINANCE')
);
3. Explanation of the SQL Query
PARTITION BY LIST (department)divides employees based on department values.SUBPARTITION BY HASH (emp_id)further divides each department partition using Employee ID.SUBPARTITIONS 4creates four hash subpartitions inside each main partition.- Oracle automatically distributes records into the appropriate hash subpartitions.
Total Number of Subpartitions
4. Insert Sample Records
Let's insert some sample records into the employees table.
INSERT INTO employees
VALUES (101, 'Ravi', 'IT', 50000);
INSERT INTO employees
VALUES (102, 'Kumar', 'HR', 40000);
INSERT INTO employees
VALUES (103, 'Raj', 'FINANCE', 60000);
INSERT INTO employees
VALUES (104, 'Suresh', 'IT', 45000);
COMMIT;
What Happens Internally?
For each inserted record:
- Oracle checks the department value.
- It identifies the appropriate main partition.
- Oracle applies the hashing algorithm to
EMP_ID. - It stores the record in the appropriate hash subpartition.
Where Will the Records Be Stored?
| Employee ID | Department | Main Partition |
|---|---|---|
| 101 | IT | P_IT |
| 102 | HR | P_HR |
| 103 | FINANCE | P_FINANCE |
| 104 | IT | P_IT |
Each employee is further assigned to one of the four hash subpartitions within that department.
5. How to Check Partitions and Subpartitions?
To Check Main Partitions
SELECT
TABLE_NAME,
PARTITION_NAME
FROM USER_TAB_PARTITIONS
WHERE TABLE_NAME = 'EMPLOYEES';
To Check Subpartitions
SELECT
TABLE_NAME,
PARTITION_NAME,
SUBPARTITION_NAME
FROM USER_TAB_SUBPARTITIONS
WHERE TABLE_NAME = 'EMPLOYEES';
6. Advantages of List-Hash Partitioning
- Organizes data based on specific categories.
- Distributes records across multiple subpartitions.
- Helps manage large tables efficiently.
- Supports partition and subpartition pruning.
- Combines the benefits of List and Hash Partitioning.
7. Interview-Ready Answer
Answer:
"List-Hash Partitioning in Oracle is a composite partitioning technique that combines List Partitioning and Hash Partitioning. First, data is divided into partitions based on specific values, such as department names (IT, HR, and Finance). Then each partition is further divided into subpartitions using a hashing algorithm based on Employee ID. It helps organize large volumes of data and distribute records efficiently."
8. Difference Between Range-Hash, Range-List, and List-Hash
| Type | First-Level Partition | Second-Level Subpartition |
|---|---|---|
| Range-Hash | Range | Hash |
| Range-List | Range | List |
| List-Hash | List | Hash |
- Range-Hash: Year-wise + Hash distribution.
- Range-List: Year-wise + Category-wise.
- List-Hash: Department-wise + Hash distribution.
9. Important Point to Remember
The first partitioning method determines the main partition, and the second partitioning method determines how that partition is further divided.
In List-Hash Partitioning:
- List Partitioning determines the department.
- Hash Partitioning determines the subpartition within that department.
Conclusion
List-Hash Partitioning is useful when we need to divide large Oracle tables based on specific categories and distribute records within those categories.
List Partitioning handles category-wise separation, while Hash Partitioning distributes records across multiple subpartitions.
Understanding this concept is important for Oracle Database Developers and DBAs, especially when working with large tables.