การขยายดัชนีด้วยการแบ่งพาร์ทิชัน
เรียนรู้ว่าดัชนีทำงานร่วมกับตารางที่แบ่งพาร์ทิชันอย่างไร และเรียนรู้กลยุทธ์การสร้างดัชนีเฉพาะพาร์ทิชันและดัชนีส่วนกลางที่มีประสิทธิภาพ
การขยายดัชนีด้วยการแบ่งพาร์ทิชัน เป็นบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Indexes & Partitioning Synergy
When dealing with truly massive datasets in PostgreSQL, combining indexing and partitioning isn't just an option—it's often a necessity for maintaining performance.
This lesson explores how indexes interact with partitioned tables, and the strategies for creating indexes that scale effectively alongside your data divisions.
Understanding Local Indexes
A local index is an index that is created on an individual partition of a partitioned table. When you create an index on the parent partitioned table, PostgreSQL automatically creates a separate index for each of its partitions.
- Each local index only covers the data within its specific partition.
- They are implicitly managed as you add or remove partitions.
- This is the most common and often most efficient type of index for partitioned tables.
Creating Local Indexes
Creating a local index is straightforward. You simply create the index on the parent partitioned table. PostgreSQL handles the creation of individual indexes for each child partition automatically.
Try running this example:
CREATE TABLE sensor_data (
id SERIAL,
log_time TIMESTAMPTZ NOT NULL,
value NUMERIC
) PARTITION BY RANGE (log_time);
CREATE TABLE sensor_data_y2023 PARTITION OF sensor_data
FOR VALUES FROM ('2023-01-01 00:00:00') TO ('2024-01-01 00:00:00');
-- This creates local indexes on all current and future partitions
CREATE INDEX idx_sensor_logtime ON sensor_data (log_time);Local Index Advantages
Local indexes shine when queries involve the partition key. Here's why:
- Partition Pruning: PostgreSQL's query planner can quickly identify and scan only the relevant partitions based on your query's `WHERE` clause.
- Smaller Indexes: Each local index is smaller, containing only a subset of the data, leading to faster lookups within a partition.
- Reduced I/O: Less data needs to be read from disk, improving query speed.
Understanding Global Indexes
A global index, unlike a local index, spans across all partitions of a partitioned table. It's a single, monolithic index that acts much like an index on a regular, non-partitioned table.
- It's not tied to any specific partition.
- Updates to any partition affect the single global index.
- They are less common for general query optimization on partitioned tables.
Creating Global Indexes
To create a global index, you typically use the ONLY keyword when specifying the parent table. This tells PostgreSQL to create the index directly on the parent, not implicitly on each child.
Global indexes are often used for enforcing unique constraints across the entire partitioned table on columns that are not the partition key.
Try running this example:
-- Assuming sensor_data is already partitioned
-- Create a unique global index across all partitions
CREATE UNIQUE INDEX idx_sensor_id_global ON ONLY sensor_data (id);Global Index Considerations
While global indexes have their uses, they come with trade-offs:
- Maintenance Overhead: Inserts, updates, or deletes to any partition require updates to the single global index, which can be slower.
- No Partition Pruning Benefit: Queries using a global index don't benefit from partition pruning on the index itself, as it spans all data.
- Use Cases: Best for unique constraints on non-partition key columns, or queries that frequently access non-partition key columns across the entire dataset.
Local vs. Global: When?
Choosing between local and global indexes depends on your workload:
- Local Indexes: Ideal when queries frequently filter data using the partition key (e.g., date ranges, specific categories). They leverage partition pruning for speed.
- Global Indexes: Consider for enforcing unique constraints on columns that are not the partition key, or for queries that frequently access non-partition key columns across the entire table.
Most often, local indexes are the preferred choice for performance on partitioned tables.
Practical Example Scenario
Imagine an orders table partitioned by order_date.
- A local index on
order_datewould be highly efficient for queries likeWHERE order_date BETWEEN X AND Ybecause of partition pruning. - A global index on
customer_idwould be efficient forWHERE customer_id = Zacross all orders, especially if you need to find all orders for a customer regardless of date.
The key is aligning your index strategy with your most common query patterns.
Indexing Partitioned Tables
Consider a large sales table partitioned by sale_date. Which index type is generally more efficient for queries like SELECT * FROM sales WHERE sale_date BETWEEN '2023-01-01' AND '2023-01-31' AND product_id = 123;?
Scaling Indexes Summary
In this lesson, we explored how indexes interact with partitioned tables. You learned about:
- Local indexes: Created per partition, ideal for queries using the partition key, benefiting from partition pruning.
- Global indexes: Span all partitions, useful for unique constraints or queries on non-partition key columns across the whole table, but with more maintenance overhead.
Mastering the interplay between partitioning and indexing is key to unlocking high performance in large-scale PostgreSQL databases.
คำถามที่พบบ่อย
บทเรียน “การขยายดัชนีด้วยการแบ่งพาร์ทิชัน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การขยายดัชนีด้วยการแบ่งพาร์ทิชัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การขยายดัชนีด้วยการแบ่งพาร์ทิชัน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication นี้ได้ไหม
ได้ บทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การขยายดัชนีด้วยการแบ่งพาร์ทิชัน
- การเลือกกลยุทธ์ดัชนีและพาร์ทิชัน
- กรณีศึกษาจากการใช้งานจริง
- ดัชนี BRIN สำหรับตารางขนาดใหญ่ที่แบ่งพาร์ทิชัน