Logo

dev-resources.site

for different kinds of informations.

Relational Database Partition with Example

Published at
12/30/2024
Categories
codeproject
devops
databaseperformance
Author
anh_trntun_4732cf3d299
Author
22 person written this
anh_trntun_4732cf3d299
open
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', '[email protected]');
INSERT INTO customers (id, name, email) VALUES (2, 'Jane Smith', '[email protected]');
INSERT INTO customers (id, name, email) VALUES (3, 'Emily Johnson', '[email protected]');
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

codeproject Article's
30 articles in total
Favicon
Default Methods in Java
Favicon
Secrets of Java's String Pool
Favicon
Reasons Why Docker Runs Natively on Linux but Needs Virtualization on Windows
Favicon
Secrets of URIs, URLs, and URNs: Understanding Their Differences and Uses
Favicon
When Should You Use Threads in Java?
Favicon
Secrets of Distributed Locks
Favicon
Understanding the High Water Mark
Favicon
Ways to Speed Up Spring Boot Application Startup Time
Favicon
Mastering Java Generics: A Comprehensive Guide with Code Examples
Favicon
Understanding and Managing Tablespace in MySQL
Favicon
Handling the "Forwarded" Header in Spring Boot Applications
Favicon
Secrets Behind Deleting or Updating Records in PostgreSQL
Favicon
Strategies to Optimize PostgreSQL Performance Using VACUUM
Favicon
Techniques for Storing JSON Documents in PostgreSQL
Favicon
Methods for Storing Boolean Values in MySQL
Favicon
Relational Database Partition with Example
Favicon
Techniques for Leveraging ReBAC: A Comprehensive Guide
Favicon
How Does CAPTCHA Work?
Favicon
Secure User Passwords in a Database
Favicon
Tips to Avoid NullPointerException in Java
Favicon
Methods for Efficient Large File Processing in Spring Boot
Favicon
Master Local Variable Type Inference in Java: A Guide to Using var Effectively
Favicon
Understanding JWE: Structure, Operations, Advantages, Disadvantages, and How to Create One
Favicon
Reasons Why Rainbow Table Attacks Are Dangerous and How Salting Passwords Protects Against Them
Favicon
OSI Model Overview
Favicon
Understanding Runnable and Callable in Java: Examples and Code Demos
Favicon
Methods to Optimize Database Queries Using Query Hints in Spring Data JPA
Favicon
Techniques for Managing Session, Cookie, JWT, and SSO
Favicon
Using CountDownLatch in Java: A Deep Dive with Code Examples and Demos
Favicon
Tips for Saving Redis Memory: Essential Techniques for Efficient Resource Management

Featured ones: