We can have sub partitions created with different permutations and combinations. Sub Partitioning is also known as nested partitioning.
List - List
List - Range and others.
Let us understand how we can create table using list - list sub partitioning. We would like to have main partition per year and then sub partitions per quarter.
Create table users_qtly
with PARTITION BY LIST
with created_year
.
Create tables for yearly partitions with PARTITION BY LIST
with created_month
.
Create tables for quarterly partitions with list of values using FOR VALUES IN
.
%%sql
CREATE TABLE users_qtly (
user_id SERIAL,
user_first_name VARCHAR(30) NOT NULL,
user_last_name VARCHAR(30) NOT NULL,
user_email_id VARCHAR(50) NOT NULL,
user_email_validated BOOLEAN DEFAULT FALSE,
user_password VARCHAR(200),
user_role VARCHAR(1) NOT NULL DEFAULT 'U', --U and A
is_active BOOLEAN DEFAULT FALSE,
created_dt DATE DEFAULT CURRENT_DATE,
created_year INT,
created_mnth INT,
last_updated_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (created_year, created_mnth, user_id)
) PARTITION BY LIST(created_year)
Let us understand how we can create table using list - Range sub partitioning using same example as before (partitioning by year and then by quarter).
Create table with PARTITION BY LIST
with created_year
.
Create tables for yearly partitions with PARTITION BY RANGE
with created_month
.
Create tables for quarterly partitions with the range of values using FOR VALUES FROM (lower_bound) TO (upper_bound)
.
%%sql
CREATE TABLE users_qtly (
user_id SERIAL,
user_first_name VARCHAR(30) NOT NULL,
user_last_name VARCHAR(30) NOT NULL,
user_email_id VARCHAR(50) NOT NULL,
user_email_validated BOOLEAN DEFAULT FALSE,
user_password VARCHAR(200),
user_role VARCHAR(1) NOT NULL DEFAULT 'U', --U and A
is_active BOOLEAN DEFAULT FALSE,
created_dt DATE DEFAULT CURRENT_DATE,
created_year INT,
created_mnth INT,
last_updated_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (created_year, created_mnth, user_id)
) PARTITION BY LIST(created_year)