การตัดและการยกเว้นพาร์ทิชัน
เจาะลึกวิธีที่ตัวเพิ่มประสิทธิภาพใช้คีย์พาร์ทิชันเพื่อยกเว้นพาร์ทิชันที่ไม่เกี่ยวข้อง ซึ่งช่วยลดปริมาณข้อมูลที่ต้องสแกนลงอย่างมาก
การตัดและการยกเว้นพาร์ทิชัน เป็นบทเรียน 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 Partition Pruning?
Welcome to a key optimization technique in PostgreSQL: Partition Pruning. This is where the database intelligently skips scanning partitions that cannot possibly contain the data a query is looking for.
Think of it as filtering bookshelves: if you're looking for a book published in 2023, you wouldn't check shelves marked '1990-1999' or '2000-2010'.
How the Optimizer Works
When you execute a query on a partitioned table, PostgreSQL's query planner examines the WHERE clause. It compares the conditions in your query to the definitions of your table's partitions.
If the query's conditions guarantee that certain partitions cannot possibly hold any matching rows, the optimizer simply excludes those partitions from the scan plan. This significantly reduces the amount of data that needs to be read from disk.
Pruning with Range Partitions
Partition pruning is most evident with range-partitioned tables, especially those partitioned by date or timestamp. For example, if you have a table partitioned by month, and you query for data from a specific week, only the relevant month partition(s) will be scanned.
This is incredibly powerful for time-series data, as queries often target specific timeframes.
Demo: Range Pruning in Action
Let's create a simple range-partitioned table and see how EXPLAIN shows pruning. We'll partition by sale_date.
Notice how the EXPLAIN output will only show a scan on the relevant partition, not the others.
CREATE TABLE sales (
sale_id INT,
sale_date DATE,
amount NUMERIC
) PARTITION BY RANGE (sale_date);
CREATE TABLE sales_2023_q1 PARTITION OF sales
FOR VALUES FROM ('2023-01-01') TO ('2023-04-01');
CREATE TABLE sales_2023_q2 PARTITION OF sales
FOR VALUES FROM ('2023-04-01') TO ('2023-07-01');
INSERT INTO sales VALUES (1, '2023-01-15', 100);
INSERT INTO sales VALUES (2, '2023-04-20', 200);
EXPLAIN SELECT * FROM sales WHERE sale_date = '2023-01-15';Pruning with List Partitions
Partition pruning also works effectively with list-partitioned tables. If your table is partitioned by a discrete value, like a region or a status code, and your query filters on that specific value, only the corresponding partition will be scanned.
This is useful when you often query data specific to certain categories or groups.
Demo: List Pruning Example
Here's an example using a list-partitioned table based on a region column. Observe the EXPLAIN output to see only the 'North' partition being scanned.
CREATE TABLE products (
product_id INT,
region TEXT,
price NUMERIC
) PARTITION BY LIST (region);
CREATE TABLE products_north PARTITION OF products
FOR VALUES IN ('North');
CREATE TABLE products_south PARTITION OF products
FOR VALUES IN ('South');
INSERT INTO products VALUES (101, 'North', 50.00);
INSERT INTO products VALUES (102, 'South', 75.00);
EXPLAIN SELECT * FROM products WHERE region = 'North';Static vs. Dynamic Pruning
PostgreSQL employs two main types of pruning:
- Static Pruning: Occurs at query planning time. The planner can see the explicit values in your
WHEREclause and immediately exclude partitions. - Dynamic Pruning: Happens during query execution. This is for more complex cases, like when the partition key is filtered by the result of a subquery or a parameter from a join. The database determines which partitions to scan as it runs.
When Pruning Might Not Occur
While powerful, partition pruning isn't always possible:
- Complex Expressions: If your
WHEREclause uses a function or complex expression on the partition key (e.g.,EXTRACT(MONTH FROM sale_date) = 1). - Non-Partition Key Filters: Queries filtering only on columns not part of the partition key will scan all partitions.
- Joins: Pruning with joins can be trickier, especially if the join condition doesn't directly involve the partition key or if the values are not known until runtime.
Verifying Pruning with EXPLAIN
To confirm that partition pruning is working, always use EXPLAIN (or EXPLAIN ANALYZE). Look for lines like:
-> Partition Selector (Dyanmic Partition Pruning)-> Append (partitions: 1)-> Result (partitions: 1)
The key is seeing a limited number of partitions selected, rather than scanning the entire partitioned table or all its child tables.
Quick Check: Pruning Benefits
Understanding partition pruning is crucial for optimizing queries on large partitioned tables. Let's test your knowledge!
Pruning Power-Up!
You've mastered partition pruning! You now understand that it's a vital PostgreSQL optimization that:
- Significantly reduces the amount of data scanned.
- Works by comparing
WHEREclauses with partition definitions. - Is especially effective with range and list partitions.
- Can be static (planning time) or dynamic (execution time).
- Can be verified using
EXPLAIN.
By leveraging partition pruning, you ensure your queries run as efficiently as possible on large datasets!
คำถามที่พบบ่อย
บทเรียน “การตัดและการยกเว้นพาร์ทิชัน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การตัดและการยกเว้นพาร์ทิชัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเพิ่มประสิทธิภาพการสอบถามด้วยการแบ่งพาร์ทิชัน
- การแนบและถอดพาร์ทิชัน
- การตัดและการยกเว้นพาร์ทิชัน
- การเชื่อมตารางและการรวมผลแบบแยกตามพาร์ทิชัน