0% found this document useful (0 votes)
12 views4 pages

Lab 04

Uploaded by

Maria Gherzouli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Lab 04

Uploaded by

Maria Gherzouli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Advanced Database

Lab: 04
Topic: Data partitioning (Range Partitioning vs List Partitioning)

Part 01: Run some queries

Using the same table created in TP 03, run the following queries:

Query 01:
EXPLAIN ANALYZE SELECT * FROM students WHERE enrollment_date BETWEEN
'2023-01-01' AND '2023-12-31';

Query 02:
EXPLAIN ANALYZE
SELECT * FROM students
WHERE major = 'Computer Science'
AND enrollment_date BETWEEN '2022-01-01' AND '2022-12-31';

Query 03:
EXPLAIN ANALYZE
SELECT * FROM students
WHERE enrollment_date >= '2024-01-01'
AND city IN ('New York', 'Los Angeles', 'Chicago');

Record the time for each query make sure that all indexes are deleted:

Query Time
Query 01

Query 02

Query 03
Part 02: Convert the student's table (TP 03) to a range partitioned table

Partitioning can be done using several strategies, such as range partitioning, list
partitioning, and hash partitioning. For our example, we will use range partitioning based
on the enrollment_date column.

1- Create a New Partitioned Table (with the same schema of students (TP 03)):
Create a new table with the same schema as the existing table, and partition it. This will
serve as the base partitioned table.

CREATE TABLE students (


id SERIAL,
name VARCHAR(100),
g INT, -- grade
age INT,
major VARCHAR(50),
enrollment_date DATE,
city VARCHAR(100),
PRIMARY KEY (id, enrollment_date)
) PARTITION BY RANGE (enrollment_date);

2- Create Partitions for the New Partitioned Table:


Define partitions based on the column you want to partition by (e.g., enrollment_date in
this case).

CREATE TABLE students_2022 PARTITION OF students_partitioned


FOR VALUES FROM ('2022-01-01') TO ('2023-01-01');

CREATE TABLE students_2023 PARTITION OF students_partitioned


FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');

CREATE TABLE students_2024 PARTITION OF students_partitioned


FOR VALUES FROM ('2024-01-01') TO ('2025-01-01');

3- Insert Data from the Existing Table into the Partitioned Table:

INSERT INTO students_partitioned (id, name, g, age, major,


enrollment_date, city)
SELECT id, name, g, age, major, enrollment_date, city FROM students;
Part 3: Query the Partitioned Table

Run the same queries (Part 01) on the partitioned table to see the differences in
performance (table name has changed: students_partitioned):

Query Time

Query 01

Query 02

Query 03

Part 4: Category partitioning

Run the following queries on student table, and save the result on this table:

Query Time

Query 01

Query 02

Query 03

Query 01:
EXPLAIN ANALYZE
SELECT * FROM students
WHERE city = 'New York';

Query 02:
EXPLAIN ANALYZE
SELECT * FROM students
WHERE city = 'Los Angeles'
AND major = 'Computer Science';

Query 03:
EXPLAIN ANALYZE
SELECT * FROM students
WHERE city NOT IN ('New York', 'Los Angeles', 'Chicago');

Questions:

1. Create a new partitioned version of the students table, partitioned by the city
column, name it “students_partitioned_by_city”.
2. Create Partitions for Specific Cities: 'New York', 'Los Angeles', 'Chicago',
3. Migrate Data into the Partitioned Table
4. Run the same 3 queries from part 4, and record the time:

Query Time

Query 01

Query 02

Query 03

Note:

● To view partition in postgress you can use:


\d+ students_partitioned_by_city

You might also like