0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · บทเรียน

การตั้งค่าการแบ่งพาร์ทิชันตามช่วง

เรียนรู้การใช้งานการแบ่งพาร์ทิชันตามช่วง โดยแบ่งตารางตามช่วงของคีย์ ซึ่งมักใช้กับข้อมูลอนุกรมเวลา

การตั้งค่าการแบ่งพาร์ทิชันตามช่วง เป็นบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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_at timestamp).
  • 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.

คำถามที่พบบ่อย

บทเรียน “การตั้งค่าการแบ่งพาร์ทิชันตามช่วง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การตั้งค่าการแบ่งพาร์ทิชันตามช่วง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การตั้งค่าการแบ่งพาร์ทิชันตามช่วง”

เรียนรู้การใช้งานการแบ่งพาร์ทิชันตามช่วง โดยแบ่งตารางตามช่วงของคีย์ ซึ่งมักใช้กับข้อมูลอนุกรมเวลา คุณปฏิบัติ Advanced PostgreSQL: Indexing, Partitioning, Replication ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced PostgreSQL: Indexing, Partitioning, Replication บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การตั้งค่าการแบ่งพาร์ทิชันตามช่วง” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication นี้ได้ไหม

ได้ บทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เหตุใดจึงต้องแบ่งพาร์ทิชัน
  2. การตั้งค่าการแบ่งพาร์ทิชันตามช่วง
  3. การใช้งานการแบ่งพาร์ทิชันแบบรายการ
  4. การนำการแบ่งพาร์ทิชันแบบแฮชไปใช้
← กลับไปที่ Advanced PostgreSQL: Indexing, Partitioning, Replication