DEV Community

Anh Trần Tuấn
Anh Trần Tuấn

Posted on • Originally published at tuanh.net on

Relational Database Partition with Example

1. Partitioning by Range: Managing Time-Series Data Efficiently

Partitioning by range is particularly useful for data that grows over time, such as logs or historical records. This method divides data into ranges based on a specified column, often a date.

Example: Orders Table

CREATE TABLE orders (
    id INT,
    order_date DATE,
    amount DECIMAL(10, 2)
)
PARTITION BY RANGE (YEAR(order_date)) (
    PARTITION p0 VALUES LESS THAN (1991),
    PARTITION p1 VALUES LESS THAN (1992),
    PARTITION p2 VALUES LESS THAN (1993),
    PARTITION p3 VALUES LESS THAN (MAXVALUE)
);
Enter fullscreen mode Exit fullscreen mode

1.1 Inserting Data

To insert data into the orders table, you can use standard INSERT statements:

INSERT INTO orders (id, order_date, amount) VALUES (1, '1990-05-10', 150.00);
INSERT INTO orders (id, order_date, amount) VALUES (2, '1991-07-15', 200.00);
INSERT INTO orders (id, order_date, amount) VALUES (3, '1992-11-20', 250.00);
Enter fullscreen mode Exit fullscreen mode

1.2 Querying Data

To retrieve data and see how partitioning affects the query:

SELECT * FROM orders WHERE order_date BETWEEN '1990-01-01' AND '1991-12-31';
Enter fullscreen mode Exit fullscreen mode

Image

EXPLAIN SELECT * FROM orders WHERE order_date BETWEEN '1990-01-01' AND '1991-12-31';
Enter fullscreen mode Exit fullscreen mode

Image

Explain

In the example above, the orders table is divided into four partitions based on the year of the order_date. Data for the year 1990 falls into p0, while data from 1991 goes into p1, and so forth. This setup allows for efficient querying and management of time-based data.

2. Partitioning by List: Organizing Data by Categories

Partitioning by list is ideal for categorical data where you can group records into specific sets.

Example: Employees Table

CREATE TABLE employees (
    id INT,
    name VARCHAR(50),
    department VARCHAR(20)
)
PARTITION BY LIST COLUMNS (department) (
    PARTITION p0 VALUES IN ('HR', 'Admin'),
    PARTITION p1 VALUES IN ('IT', 'Development'),
    PARTITION p2 VALUES IN ('Sales', 'Marketing')
);
Enter fullscreen mode Exit fullscreen mode

2.1 Inserting Data

Insert data into the employees table:

INSERT INTO employees (id, name, department) VALUES (1, 'Alice', 'HR');
INSERT INTO employees (id, name, department) VALUES (2, 'Bob', 'IT');
INSERT INTO employees (id, name, department) VALUES (3, 'Carol', 'Sales');
Enter fullscreen mode Exit fullscreen mode

2.2 Querying Data

To query data and understand the partitioning impact:

SELECT * FROM employees WHERE department = 'IT';
Enter fullscreen mode Exit fullscreen mode

Image

EXPLAIN SELECT * FROM employees WHERE department = 'IT';
Enter fullscreen mode Exit fullscreen mode

Image

Explain

In this partitioning scheme, the employees table is divided based on the department column. Each partition contains employees from specific departments. This organization simplifies queries and operations that target particular departments.

3. Partitioning by Hash: Distributing Data Evenly

Hash partitioning is beneficial when you need to evenly distribute data across partitions without a natural range or list to use.

Example: Customers Table

CREATE TABLE customers (
    id INT,
    name VARCHAR(50),
    email VARCHAR(50)
)
PARTITION BY HASH (id) PARTITIONS 4;
Enter fullscreen mode Exit fullscreen mode

3.1 Inserting Data

INSERT INTO customers (id, name, email) VALUES (1, 'John Doe', 'john@example.com');
INSERT INTO customers (id, name, email) VALUES (2, 'Jane Smith', 'jane@example.com');
INSERT INTO customers (id, name, email) VALUES (3, 'Emily Johnson', 'emily@example.com');
Enter fullscreen mode Exit fullscreen mode

3.2 Querying Data

To see how hash partitioning works in practice:

SELECT * FROM customers WHERE id = 1;
Enter fullscreen mode Exit fullscreen mode

Image

EXPLAIN SELECT * FROM customers WHERE id = 1;
Enter fullscreen mode Exit fullscreen mode

Image

Explain

The customers table is partitioned into 4 parts based on the hash of the id column. This distribution ensures that data is spread evenly across partitions, enhancing performance for queries and data management.

4. Conclusion

Partitioning is a valuable technique for managing large datasets and optimizing query performance. By using range, list, and hash partitioning methods, you can tailor your database structure to fit your specific needs. If you have any questions or need further clarification on database partitioning, feel free to leave a comment below!

Read posts more at : Relational Database Partition with Example

Top comments (0)