DEV Community

Pranav Bakare
Pranav Bakare

Posted on

Partitioning types in ORACLE SQL

Partitioning is a database design strategy that divides a large table into smaller, manageable pieces, or partitions, to improve performance, ease of management, and availability. Here are the main types of partitioning in SQL, particularly as implemented in Oracle SQL, along with detailed explanations and examples:

  1. Range Partitioning

Description: Range partitioning divides data based on a specified range of values. Each partition holds a specific range of values from a partitioning key (e.g., dates, numerical ranges).

Use Case: Ideal for time-series data, where records are frequently queried by date ranges.

Example: Creating a table to store sales data partitioned by year:

CREATE TABLE sales (
id NUMBER,
amount NUMBER,
sale_date DATE
)
PARTITION BY RANGE (sale_date) (
PARTITION p2021 VALUES LESS THAN (TO_DATE('01-JAN-2022', 'DD-MON-YYYY')),
PARTITION p2022 VALUES LESS THAN (TO_DATE('01-JAN-2023', 'DD-MON-YYYY')),
PARTITION p2023 VALUES LESS THAN (TO_DATE('01-JAN-2024', 'DD-MON-YYYY'))
);

In this example:

p2021 contains all sales records from before January 1, 2022.

p2022 contains sales records from 2022.

p2023 contains sales records from 2023.

  1. List Partitioning

Description: List partitioning divides data based on a set of discrete values defined in a list. Each partition corresponds to a specific value or group of values.

Use Case: Useful for categorical data, such as geographical regions, departments, etc.

Example: Creating a table partitioned by product categories:

CREATE TABLE products (
product_id NUMBER,
product_name VARCHAR2(100),
category VARCHAR2(50)
)
PARTITION BY LIST (category) (
PARTITION electronics VALUES ('Mobile', 'Laptop', 'Tablet'),
PARTITION clothing VALUES ('Shirt', 'Pants', 'Dress'),
PARTITION grocery VALUES ('Fruits', 'Vegetables', 'Snacks')
);

In this example:

The electronics partition contains products related to electronics.

The clothing partition contains apparel items.

The grocery partition contains food items.

  1. Hash Partitioning

Description: Hash partitioning distributes data evenly across a specified number of partitions using a hash function. This method is particularly useful for achieving load balancing.

Use Case: Ideal for evenly distributing data when there is no natural range or list to partition by.

Example: Creating a table partitioned by a hash of the user ID:

CREATE TABLE users (
user_id NUMBER,
username VARCHAR2(50)
)
PARTITION BY HASH (user_id)
PARTITIONS 4; -- Specifies 4 partitions

In this example:

The users table will be divided into 4 partitions based on the hash of the user_id.

Each user is assigned to one of the 4 partitions based on the hash value of their ID, ensuring an even distribution.

  1. Composite Partitioning

Description: Composite partitioning combines two or more partitioning methods. For example, a table can be partitioned by range and then further sub-partitioned by hash.

Use Case: Useful for complex data distributions requiring both ranges and balances.

Example: Creating a table partitioned by range and sub-partitioned by hash:

CREATE TABLE orders (
order_id NUMBER,
order_amount NUMBER,
order_date DATE
)
PARTITION BY RANGE (order_date)
SUBPARTITION BY HASH (order_id)
(
PARTITION p2022 VALUES LESS THAN (TO_DATE('01-JAN-2023', 'DD-MON-YYYY'))
(SUBPARTITION sp1, SUBPARTITION sp2),
PARTITION p2023 VALUES LESS THAN (TO_DATE('01-JAN-2024', 'DD-MON-YYYY'))
(SUBPARTITION sp3, SUBPARTITION sp4)
);

In this example:

The orders table is partitioned by the order_date into partitions for the years 2022 and 2023.

Each partition is further sub-partitioned by the hash of order_id, resulting in 2 sub-partitions for each year.

  1. Reference Partitioning

Description: Reference partitioning allows a child table to inherit the partitioning scheme of its parent table, creating a relationship between them. This is particularly useful for maintaining referential integrity in partitioned tables.

Use Case: Useful for scenarios where there is a parent-child relationship between tables, such as orders and order items.

Example: Creating a parent table and a child table with reference partitioning:

CREATE TABLE orders (
order_id NUMBER,
customer_id NUMBER,
order_date DATE
)
PARTITION BY RANGE (order_date) (
PARTITION p2022 VALUES LESS THAN (TO_DATE('01-JAN-2023', 'DD-MON-YYYY')),
PARTITION p2023 VALUES LESS THAN (TO_DATE('01-JAN-2024', 'DD-MON-YYYY'))
);

CREATE TABLE order_items (
item_id NUMBER,
order_id NUMBER,
product_id NUMBER,
quantity NUMBER
)
PARTITION BY REFERENCE (orders);

In this example:

The order_items table is partitioned based on the partitioning scheme of the orders table.

Each order_items partition corresponds to the relevant partition in the orders table.

Summary

Range Partitioning: Divides data based on ranges, useful for time-series data.

List Partitioning: Divides data based on a list of discrete values, ideal for categorical data.

Hash Partitioning: Distributes data evenly across partitions using a hash function, useful for load balancing.

Composite Partitioning: Combines multiple partitioning strategies, useful for complex data distributions.

Reference Partitioning: Allows child tables to inherit the partitioning scheme of parent tables, maintaining referential integrity.

Each partitioning strategy has its advantages and is suitable for specific use cases. Properly implemented, partitioning can significantly enhance database performance and manageability.

Top comments (0)