Table partitioning

Projeto de banco de dados

Lis Sulmont

Curriculum Manager

Why partition?

Tables grow (100s Gb / Tb)

Problem: queries/updates become slower

Because: e.g., indices don't fit memory

Solution: split table into smaller parts (= partitioning)

Image of file archives by date

Projeto de banco de dados

Data modeling refresher

 

1. Conceptual data model

2. Logical data model

For partitioning, logical data model is the same

3. Physical data model

Partitioning is part of physical data model

Projeto de banco de dados

Vertical partitioning

Vertical partitioning

Split table even when fully normalized

Projeto de banco de dados

Vertical partitioning: an example

Vertical partitioning example

E.g., store long_description on slower medium

Projeto de banco de dados

Horizontal partitioning

Horizontal partitioning

Projeto de banco de dados

Horizontal partitioning: an example

Horizontal partitioning: example table

Projeto de banco de dados

Horizontal partitioning: an example

 

Horizontal partitioning: example

 

CREATE TABLE sales (
    ...
    timestamp DATE NOT NULL
)
PARTITION BY RANGE (timestamp);

CREATE TABLE sales_2019_q1 PARTITION OF sales FOR VALUES FROM ('2019-01-01') TO ('2019-03-31'); ... CREATE TABLE sales_2019_q4 PARTITION OF sales FOR VALUES FROM ('2019-10-01') TO ('2020-01-31');
CREATE INDEX ON sales ('timestamp');
Projeto de banco de dados

Pros/cons of horizontal partitioning

Pros

  • Indices of heavily-used partitions fit in memory
  • Move to specific medium: slower vs. faster
  • Used for both OLAP and OLTP

Cons

  • Partitioning existing table can be a hassle
  • Some constraints can not be set
Projeto de banco de dados

Relation to sharding

Sharding

Projeto de banco de dados

Let's practice!

Projeto de banco de dados

Preparing Video For Download...