Database.Middle.How does database partitioning work?

Database partitioning is a technique used to split a large table into smaller, more manageable pieces, called partitions, while keeping them logically part of the same table. This improves performance, scalability, and sometimes maintenance.


🧠 Why Partition?

  • Query performance: Only scan relevant partition(s)
  • Manageability: Easier to archive, purge, or back up
  • Parallelism: Different partitions can be read/written in parallel
  • Maintenance: Indexes and constraints apply per partition

📦 Types of Partitioning

TypeHow it worksExample
RangeSplit by a range of valuesDate ranges, ID ranges
ListSplit by discrete valuesCountry = US, UK, FR
HashSplit using a hash functionEven data distribution
CompositeCombines two types (e.g., range + hash)Date → then hash within each month

🔍 Example: Range Partitioning (PostgreSQL)

CREATE TABLE sales (
  id SERIAL,
  sale_date DATE NOT NULL,
  amount NUMERIC
) PARTITION BY RANGE (sale_date);

Then define partitions:

CREATE TABLE sales_2023_q1 PARTITION OF sales
  FOR VALUES FROM ('2023-01-01') TO ('2023-04-01');

CREATE TABLE sales_2023_q2 PARTITION OF sales
  FOR VALUES FROM ('2023-04-01') TO ('2023-07-01');

Now, when you query by date:

SELECT * FROM sales WHERE sale_date = '2023-02-10';

PostgreSQL automatically routes the query to sales_2023_q1 only → faster.

🧪 Other Partitioning Examples

✅ List Partitioning (MySQL, PostgreSQL 11+)

CREATE TABLE users (
  id INT,
  country_code TEXT
) PARTITION BY LIST (country_code);

CREATE TABLE users_us PARTITION OF users
  FOR VALUES IN ('US');

CREATE TABLE users_uk PARTITION OF users
  FOR VALUES IN ('UK');

🧪 Other Partitioning Examples

✅ List Partitioning (MySQL, PostgreSQL 11+)

CREATE TABLE users (
  id INT,
  country_code TEXT
) PARTITION BY LIST (country_code);

CREATE TABLE users_us PARTITION OF users
  FOR VALUES IN ('US');

CREATE TABLE users_uk PARTITION OF users
  FOR VALUES IN ('UK');

✅ Hash Partitioning (PostgreSQL / MySQL)

Useful for spreading rows evenly when ranges/lists aren’t practical.

CREATE TABLE logs (
  id INT,
  message TEXT
) PARTITION BY HASH (id);

CREATE TABLE logs_p0 PARTITION OF logs FOR VALUES WITH (modulus 4, remainder 0);
CREATE TABLE logs_p1 PARTITION OF logs FOR VALUES WITH (modulus 4, remainder 1);
-- etc.

🚦 Partition Pruning

Modern databases (PostgreSQL, MySQL, Oracle, etc.) prune unneeded partitions automatically during query execution, so only relevant partitions are scanned.

This boosts performance dramatically for filtered queries like:

SELECT * FROM sales WHERE sale_date BETWEEN '2023-01-01' AND '2023-01-31';

⚠️ Downsides / Gotchas

ConcernExplanation
❌ ComplexityMore complex DDL and indexing
❌ Not always fasterEspecially if queries span many partitions
❌ Insert routing overheadDatabase must decide where to insert
❌ Feature limitationsSome constraints or foreign keys may be restricted

✅ Summary

ConceptExplanation
PartitioningBreak large table into smaller parts
BenefitBetter performance, easier maintenance
TypesRange, List, Hash, Composite
Use CasesTime-series, multi-region, log data, huge tables
This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.