0Pricing
PostgreSQL Performance & Query Optimization · บทเรียน

การปรับแต่งหน่วยความจำ (shared_buffers, work_mem)

ปรับการใช้หน่วยความจำให้เหมาะสมด้วยการกำหนดค่า `shared_buffers`, `work_mem` และการตั้งค่าอื่นที่เกี่ยวข้องกับหน่วยความจำอย่างถูกต้อง

การปรับแต่งหน่วยความจำ (shared_buffers, work_mem) เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน PostgreSQL Performance & Query Optimization และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Memory Matters for PostgreSQL

Optimizing how PostgreSQL uses memory is crucial for blazing-fast performance. Think of memory as your database's short-term workspace.

Proper memory configuration means less waiting for data from slow disks and quicker query processing.

Shared Buffers Explained

shared_buffers is one of the most important memory settings. It defines the amount of RAM PostgreSQL uses for caching data pages.

  • What it does: Stores frequently accessed data blocks from tables and indexes.
  • Why it matters: Reduces disk I/O, as PostgreSQL can serve data directly from RAM.

Configuring Shared Buffers

A common starting point for shared_buffers is 25% of your total system RAM. For dedicated database servers, you might go up to 40%.

  • Too low? Frequent disk reads.
  • Too high? Less RAM for OS/other processes, potentially causing swapping.

Remember to restart PostgreSQL after changing this setting in postgresql.conf.

work_mem for Sorts & Hashes

work_mem is memory allocated for individual query operations like sorting (ORDER BY, GROUP BY) and hash tables (Hash Joins).

Unlike shared_buffers, this memory is private to each operation and temporary. Multiple operations in a single query, or multiple concurrent queries, can each use their own work_mem.

Fine-tuning work_mem

If a query operation exceeds its allocated work_mem, it 'spills to disk,' using temporary files. This is much slower!

You can set work_mem globally, or even per-session or per-query for specific needs. A good starting point is 4-16MB, but monitor for spills using EXPLAIN ANALYZE.

Maintenance Memory (maintenance_work_mem)

maintenance_work_mem is dedicated memory for maintenance tasks, specifically:

  • VACUUM
  • CREATE INDEX
  • ALTER TABLE (adding columns, etc.)

These operations can be very memory-intensive. Setting this higher than work_mem is common, e.g., 64-512MB, to speed up maintenance.

effective_cache_size: The Planner's Guide

effective_cache_size is not memory allocated by PostgreSQL. Instead, it tells the query planner how much memory it expects to be available for caching data.

This includes shared_buffers AND the OS file system cache. A higher value encourages the planner to use index scans over sequential scans, assuming data is likely in memory.

Modifying Configuration

Most of these settings are found in your postgresql.conf file. You can also change them at runtime using ALTER SYSTEM or ALTER DATABASE/ALTER ROLE for finer control.

Remember to reload the configuration (or restart PostgreSQL for shared_buffers) after making changes to postgresql.conf.

Collective Impact on Performance

When tuned correctly, these memory parameters work together to:

  • Reduce Disk I/O: Data served from RAM is much faster.
  • Speed up Queries: Sorts, joins, and aggregations complete quicker.
  • Optimize Maintenance: VACUUM and index creation run faster.
  • Improve Planner Decisions: Better query plans lead to better execution.

Quick Check: Memory Roles

Which PostgreSQL memory parameter is primarily used by individual query operations like sorting and hashing, and can lead to "spilling to disk" if set too low?

Recap: Memory Tuning Essentials

We've explored key PostgreSQL memory parameters:

  • shared_buffers: Main data cache, ~25% RAM.
  • work_mem: Per-operation memory for sorts/hashes.
  • maintenance_work_mem: For VACUUM, CREATE INDEX.
  • effective_cache_size: A hint for the query planner.

Tuning these carefully can significantly boost your database's performance!

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

บทเรียน “การปรับแต่งหน่วยความจำ (shared_buffers, work_mem)” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การปรับแต่งหน่วยความจำ (shared_buffers, work_mem)” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส PostgreSQL Performance & Query Optimization ให้อัปเกรดเป็น CoddyKit PRO คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การปรับแต่งหน่วยความจำ (shared_buffers, work_mem)”

ปรับการใช้หน่วยความจำให้เหมาะสมด้วยการกำหนดค่า `shared_buffers`, `work_mem` และการตั้งค่าอื่นที่เกี่ยวข้องกับหน่วยความจำอย่างถูกต้อง คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่

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

บทเรียน “การปรับแต่งหน่วยความจำ (shared_buffers, work_mem)” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม

ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. พารามิเตอร์สำคัญใน postgresql.conf
  2. การปรับแต่งหน่วยความจำ (shared_buffers, work_mem)
  3. การปรับแต่งดิสก์ I/O และจุดตรวจสอบ
  4. การปรับค่าคงที่ต้นทุนของตัววางแผนการสืบค้น
← กลับไปที่ PostgreSQL Performance & Query Optimization