การใช้งานการแบ่งพาร์ทิชันแบบรายการ
ทำความเข้าใจวิธีตั้งค่าการแบ่งพาร์ทิชันแบบรายการ โดยแบ่งข้อมูลตามค่าแบบไม่ต่อเนื่องที่ระบุในคอลัมน์
การใช้งานการแบ่งพาร์ทิชันแบบรายการ เป็นบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is List Partitioning?
Welcome! In this lesson, we'll dive into List Partitioning, a powerful way to organize your data in PostgreSQL.
List partitioning divides a table based on discrete values in a specified column. Think of it as categorizing your data into distinct groups.
List vs. Range: A Quick Look
You might recall Range Partitioning from a previous lesson, which divides data based on a *range* of values (e.g., dates, IDs).
List partitioning is different: it uses specific, distinct values. For example, you could partition by country_code ('US', 'CA', 'MX') or order_status ('PENDING', 'SHIPPED', 'CANCELLED').
Choosing Your Partition Key
The column you choose to partition by is called the partition key. For list partitioning, this column should have a finite and manageable set of distinct values.
- Good candidates: Country codes, product categories, order statuses.
- Poor candidates: Timestamps, large text fields, unique IDs (too many distinct values).
Setting Up the Parent Table
First, we create the main table, known as the parent table. We specify PARTITION BY LIST and indicate the column that will serve as our partition key.
Let's create a sales table partitioned by country_code:
CREATE TABLE sales (
sale_id SERIAL,
product_id INT,
country_code CHAR(2),
amount NUMERIC(10, 2),
sale_date DATE
) PARTITION BY LIST (country_code);Defining Your Partitions
Once the parent table is ready, we create individual child partitions. Each child partition is a separate table that stores rows for specific values of the partition key.
Here, we create partitions for sales in the 'US' and 'CA':
CREATE TABLE sales_us PARTITION OF sales
FOR VALUES IN ('US');
CREATE TABLE sales_ca PARTITION OF sales
FOR VALUES IN ('CA');Data Insertion in Action
When you insert data into the parent sales table, PostgreSQL automatically directs each row to the correct child partition based on its country_code value.
Let's add some sales data and then check the individual partitions:
INSERT INTO sales (product_id, country_code, amount, sale_date) VALUES
(101, 'US', 150.75, '2023-01-05'),
(102, 'CA', 200.00, '2023-01-06'),
(103, 'US', 75.20, '2023-01-07');
SELECT * FROM sales_us;
SELECT * FROM sales_ca;Inspecting Your Partitions
It's good practice to verify that your partitions are correctly linked to the parent table. You can query PostgreSQL's catalog tables to see the inheritance structure.
This query lists child tables for 'sales':
SELECT relnamespace::regnamespace AS parent_schema,
relname AS parent_table,
c.relname AS child_table
FROM pg_class p
JOIN pg_inherits i ON p.oid = i.inhparent
JOIN pg_class c ON i.inhrelid = c.oid
WHERE p.relname = 'sales';Expanding Your Partition Scheme
As your business expands or new data categories emerge, you can easily add new partitions to your existing scheme without downtime.
Let's add a partition for Mexico ('MX') sales and insert some data:
CREATE TABLE sales_mx PARTITION OF sales
FOR VALUES IN ('MX');
INSERT INTO sales (product_id, country_code, amount, sale_date) VALUES
(104, 'MX', 120.50, '2023-01-08');
SELECT * FROM sales_mx;Handling Unknown Values with DEFAULT
What happens if you insert a country_code that isn't explicitly covered by a partition (e.g., 'GB')?
You can create a default partition to catch all values that don't match any other defined list. This prevents errors for unexpected data.
CREATE TABLE sales_default PARTITION OF sales
DEFAULT;
INSERT INTO sales (product_id, country_code, amount, sale_date) VALUES
(105, 'GB', 99.99, '2023-01-09');
SELECT * FROM sales_default;List Partitioning Quiz
Consider a table orders partitioned by list on the status column, which can be 'NEW', 'PROCESSING', 'SHIPPED', or 'CANCELLED'.
List Partitioning Summary
We've covered how to implement list partitioning in PostgreSQL. You learned to:
- Define a parent table with
PARTITION BY LIST. - Create child partitions using
FOR VALUES IN. - Route data automatically upon insertion.
- Add new partitions and handle unspecified values with a
DEFAULTpartition.
List partitioning is ideal for data with discrete, categorical values, helping organize and query your data more efficiently.
คำถามที่พบบ่อย
บทเรียน “การใช้งานการแบ่งพาร์ทิชันแบบรายการ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การใช้งานการแบ่งพาร์ทิชันแบบรายการ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การใช้งานการแบ่งพาร์ทิชันแบบรายการ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication นี้ได้ไหม
ได้ บทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใดจึงต้องแบ่งพาร์ทิชัน
- การตั้งค่าการแบ่งพาร์ทิชันตามช่วง
- การใช้งานการแบ่งพาร์ทิชันแบบรายการ
- การนำการแบ่งพาร์ทิชันแบบแฮชไปใช้