Partition in Oracle | Revision49

Pranav Bakare - Oct 22 - - Dev Community

What is Partition in Oracle SQL?

Partitioning in Oracle SQL refers to the process of dividing a large database table into smaller, more manageable pieces, called partitions. Each partition can be managed and queried independently while still being treated as part of the larger table. This approach enhances performance, manageability, and availability.

Why is Partitioning Required?

  1. Performance: Partitioning can improve query performance by allowing the database to scan only the relevant partitions instead of the entire table. This can be particularly beneficial for large tables with millions of rows.

  2. Manageability: Smaller partitions are easier to manage. Tasks like loading data, backing up, and archiving can be done on individual partitions without affecting the entire table.

  3. Availability: Partitioning can increase the availability of the database. For example, if one partition needs maintenance, the others can still be operational.

  4. Data Locality: Partitioning allows for data to be stored in a way that optimizes access patterns. For instance, frequently accessed data can be stored in fast storage, while less frequently accessed data can be archived.

Different Types of Partitioning

  1. Range Partitioning: Divides data based on a specified range of values. Commonly used for date values.

Example:

CREATE TABLE sales (
sale_id NUMBER,
sale_date DATE,
amount NUMBER
)
PARTITION BY RANGE (sale_date) (
PARTITION p1 VALUES LESS THAN (TO_DATE('2023-01-01', 'YYYY-MM-DD')),
PARTITION p2 VALUES LESS THAN (TO_DATE('2024-01-01', 'YYYY-MM-DD')),
PARTITION p3 VALUES LESS THAN (MAXVALUE)
);

  1. List Partitioning: Divides data based on a predefined list of values. Useful for categorical data.

Example:

CREATE TABLE employees (
emp_id NUMBER,
dept_id NUMBER,
name VARCHAR2(100)
)
PARTITION BY LIST (dept_id) (
PARTITION p_sales VALUES (1, 2),
PARTITION p_hr VALUES (3),
PARTITION p_eng VALUES (4, 5)
);

  1. Hash Partitioning: Divides data based on a hash function, distributing rows evenly across partitions. Useful when there’s no clear range or list.

Example:

CREATE TABLE customer_orders (
order_id NUMBER,
customer_id NUMBER,
order_date DATE
)
PARTITION BY HASH (customer_id)
PARTITIONS 4;

  1. Composite Partitioning: Combines multiple partitioning strategies. For example, a table can be first range partitioned and then sub-partitioned by hash.

Example:

CREATE TABLE transactions (
transaction_id NUMBER,
transaction_date DATE,
account_id NUMBER
)
PARTITION BY RANGE (transaction_date)
SUBPARTITION BY HASH (account_id)
PARTITIONS 4
(
PARTITION p_jan VALUES LESS THAN (TO_DATE('2024-02-01', 'YYYY-MM-DD')),
PARTITION p_feb VALUES LESS THAN (TO_DATE('2024-03-01', 'YYYY-MM-DD'))
);

Summary

Partitioning is a powerful feature in Oracle SQL that enhances performance, manageability, and availability by breaking down large tables into smaller, more manageable pieces. Understanding the different types of partitioning—range, list, hash, and composite—allows database administrators to design tables that optimize data access and maintenance based on specific use cases.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .