범위 파티셔닝 설정
키의 범위에 따라 테이블을 나누는 범위 파티셔닝을 구현하는 방법을 배웁니다. 범위 파티셔닝은 시계열 데이터에 자주 사용됩니다.
범위 파티셔닝 설정은(는) CoddyKit의 무료 Advanced PostgreSQL: Indexing, Partitioning, Replication 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Advanced PostgreSQL: Indexing, Partitioning, Replication 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Advanced PostgreSQL: Indexing, Partitioning, Replication 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Intro to Range Partitioning
Welcome to range partitioning! This technique divides a large table into smaller, more manageable pieces called partitions.
Range partitioning is especially useful for data that has a natural order, like time-series data (e.g., logs, sensor readings) or data with sequential IDs.
How Range Partitioning Works
With range partitioning, rows are distributed into partitions based on a 'partition key' column's value falling within a specified range.
- Partition Key: The column used to determine which partition a row belongs to (e.g.,
created_attimestamp). - Ranges: Defined boundaries (e.g., 'January to March', 'IDs 1-1000').
Creating the Master Table
First, you create a master (or parent) table. This table defines the schema for all its partitions and specifies the partitioning strategy.
Notice the PARTITION BY RANGE clause and the partition key (event_date) in the example:
CREATE TABLE sensor_data (
id SERIAL,
event_date DATE NOT NULL,
temperature NUMERIC,
humidity NUMERIC
) PARTITION BY RANGE (event_date);Defining Partition Bounds
After the master table, you create individual child tables, which are the actual partitions. Each child table is linked to the master table and defines its specific range.
The FOR VALUES FROM (...) TO (...) clause sets the lower (inclusive) and upper (exclusive) bounds for the event_date column.
Creating the First Partition
Let's create a partition for data from January 2023. The TO value is exclusive, so '2023-02-01' means up to, but not including, February 1st.
CREATE TABLE sensor_data_2023_01
PARTITION OF sensor_data
FOR VALUES FROM ('2023-01-01') TO ('2023-02-01');Adding More Partitions
You can create as many partitions as needed, covering different time periods or ranges. It's common to create partitions for months, quarters, or years.
Here's a partition for February 2023 data:
CREATE TABLE sensor_data_2023_02
PARTITION OF sensor_data
FOR VALUES FROM ('2023-02-01') TO ('2023-03-01');Inserting Data
When you insert data into the master sensor_data table, PostgreSQL automatically directs each row to the correct child partition based on its event_date.
You insert data into the parent table, not directly into the child partitions:
INSERT INTO sensor_data (event_date, temperature, humidity)
VALUES
('2023-01-15', 22.5, 60),
('2023-02-10', 20.1, 65),
('2023-01-28', 23.0, 58);Verifying Data Distribution
You can query the master table as usual, and PostgreSQL will scan only the relevant partitions (a process called 'partition pruning').
Or, you can directly query a child partition to see its contents:
SELECT * FROM sensor_data_2023_01;
SELECT * FROM sensor_data_2023_02;Adding Future Partitions
As new data arrives, you'll need to create new partitions. It's a good practice to pre-create future partitions to ensure seamless data ingestion.
For example, to add a partition for March 2023:
CREATE TABLE sensor_data_2023_03
PARTITION OF sensor_data
FOR VALUES FROM ('2023-03-01') TO ('2023-04-01');Quick Check
You've set up a range-partitioned table for sales_data based on sale_date. The master table is sales_data, and you've created a partition sales_q1_2023 for '2023-01-01' to '2023-04-01'.
Which of the following INSERT statements into the master sales_data table would correctly route data into the sales_q1_2023 partition?
Recap & Next Steps
You've learned how to implement range partitioning in PostgreSQL!
- Range partitioning divides tables by a key's value range.
- You define a master table with
PARTITION BY RANGE. - Child tables are created using
PARTITION OF ... FOR VALUES FROM ... TO .... - Data is inserted into the master table and automatically routed.
This method is excellent for managing large, time-ordered datasets, improving both query performance and data maintenance.
자주 묻는 질문
“범위 파티셔닝 설정” 강의는 무료인가요?
네 — “범위 파티셔닝 설정” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Advanced PostgreSQL: Indexing, Partitioning, Replication 강의 전체를 잠금 해제할 수 있습니다. Advanced PostgreSQL: Indexing, Partitioning, Replication 강의에는 총 4개의 강의가 포함되어 있습니다.
“범위 파티셔닝 설정”에서 뭘 배우나요?
키의 범위에 따라 테이블을 나누는 범위 파티셔닝을 구현하는 방법을 배웁니다. 범위 파티셔닝은 시계열 데이터에 자주 사용됩니다. 브라우저에서 직접 실행하는 실습 코드로 Advanced PostgreSQL: Indexing, Partitioning, Replication을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Advanced PostgreSQL: Indexing, Partitioning, Replication을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Advanced PostgreSQL: Indexing, Partitioning, Replication은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“범위 파티셔닝 설정” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Advanced PostgreSQL: Indexing, Partitioning, Replication 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Advanced PostgreSQL: Indexing, Partitioning, Replication 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 파티셔닝이 필요한 이유
- 범위 파티셔닝 설정
- 목록 파티셔닝 구현
- 해시 파티셔닝 구현