ดัชนี BRIN สำหรับตารางขนาดใหญ่ที่แบ่งพาร์ทิชัน
เรียนรู้ว่าเมื่อใดดัชนีช่วงบล็อกให้ประสิทธิภาพดีกว่า B-tree บนตารางขนาดมหึมาที่แบ่งพาร์ทิชันและมีการเรียงลำดับตามธรรมชาติ
ดัชนี BRIN สำหรับตารางขนาดใหญ่ที่แบ่งพาร์ทิชัน เป็นบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Scale Problem
On tables with billions of rows, a B-tree index can grow huge and consume a lot of memory. BRIN (Block Range Index) offers a tiny alternative for naturally ordered data.
How BRIN Works
BRIN stores the min and max value per block range instead of one entry per row. A query checks which ranges could contain matching rows and scans only those blocks.
Correlation Is Key
BRIN shines when the column's physical order correlates with its value order, such as an inserted_at timestamp on an append-only table. Random ordering makes BRIN nearly useless.
Creating a BRIN Index
You declare it with USING brin. It is dramatically smaller than a B-tree on the same column.
CREATE INDEX ON measurements USING brin (recorded_at);pages_per_range
The pages_per_range storage parameter controls granularity. Smaller values give more precise (but larger) indexes.
CREATE INDEX ON measurements USING brin (recorded_at)
WITH (pages_per_range = 32);Size Comparison
A B-tree might be many gigabytes on a billion-row table; the equivalent BRIN can be just a few megabytes. The trade-off is BRIN does coarser filtering.
BRIN on Partitions
On a range-partitioned table, create the BRIN on the parent; it propagates to each partition. Within a partition the data is usually well-correlated, so BRIN works great.
CREATE INDEX ON events USING brin (created_at);Keeping BRIN Fresh
New blocks must be summarized for BRIN to cover them. Autovacuum does this, or you can force it.
SELECT brin_summarize_new_values('measurements_recorded_at_idx');Verifying with EXPLAIN
Look for a Bitmap Index Scan on the BRIN index followed by a recheck. The recheck confirms candidate blocks really contain matches.
EXPLAIN SELECT * FROM measurements
WHERE recorded_at > now() - interval '1 day';When Not to Use BRIN
Avoid BRIN for:
- Columns with random physical order
- Point lookups needing exact, fast access (use B-tree)
- Uniqueness enforcement (BRIN cannot)
Combining Strategies
At scale, a common pattern is: range-partition by time, BRIN on the time column for cheap range scans, and a small B-tree on a frequently filtered foreign key.
Quick Check
When is BRIN a strong choice?
Recap
You learned BRIN indexes: tiny block-range summaries ideal for huge, well-correlated partitioned tables. Tune pages_per_range, keep summaries fresh, verify with EXPLAIN, and combine BRIN with selective B-trees.
คำถามที่พบบ่อย
บทเรียน “ดัชนี BRIN สำหรับตารางขนาดใหญ่ที่แบ่งพาร์ทิชัน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ดัชนี BRIN สำหรับตารางขนาดใหญ่ที่แบ่งพาร์ทิชัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ดัชนี BRIN สำหรับตารางขนาดใหญ่ที่แบ่งพาร์ทิชัน”
เรียนรู้ว่าเมื่อใดดัชนีช่วงบล็อกให้ประสิทธิภาพดีกว่า B-tree บนตารางขนาดมหึมาที่แบ่งพาร์ทิชันและมีการเรียงลำดับตามธรรมชาติ คุณปฏิบัติ Advanced PostgreSQL: Indexing, Partitioning, Replication ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced PostgreSQL: Indexing, Partitioning, Replication บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ดัชนี BRIN สำหรับตารางขนาดใหญ่ที่แบ่งพาร์ทิชัน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication นี้ได้ไหม
ได้ บทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การขยายดัชนีด้วยการแบ่งพาร์ทิชัน
- การเลือกกลยุทธ์ดัชนีและพาร์ทิชัน
- กรณีศึกษาจากการใช้งานจริง
- ดัชนี BRIN สำหรับตารางขนาดใหญ่ที่แบ่งพาร์ทิชัน