การปรับค่าคงที่ต้นทุนของตัววางแผนการสืบค้น
เรียนรู้ว่าค่าคงที่ต้นทุนของตัววางแผน เช่น random_page_cost และ effective_cache_size มีผลต่อการเลือกแผนอย่างไร และวิธีปรับค่าให้เหมาะกับฮาร์ดแวร์และข้อมูลของคุณ
การปรับค่าคงที่ต้นทุนของตัววางแผนการสืบค้น เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน PostgreSQL Performance & Query Optimization และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Planner Needs Hints About Hardware
The planner estimates the cost of each plan using constants that model your hardware. If those constants do not match reality, it may pick a sequential scan when an index scan would be far faster, or vice versa.
seq_page_cost vs random_page_cost
Two key constants:
seq_page_cost(default 1.0): cost to read a page sequentiallyrandom_page_cost(default 4.0): cost to read a page at random
The 4:1 ratio assumes spinning disks where random reads are slow.
Tuning for SSDs
On SSDs random reads are nearly as cheap as sequential ones. Lowering random_page_cost makes the planner favor index scans appropriately.
SET random_page_cost = 1.1;effective_cache_size
This tells the planner how much memory the OS and PostgreSQL together are likely to use for caching data. It does not allocate memory — it only influences cost estimates for index scans.
SET effective_cache_size = '12GB';Why effective_cache_size Matters
A large value tells the planner that repeated index lookups will likely hit cache, making index scans look cheaper and more attractive for large tables. Set it to roughly 50-75% of total RAM.
CPU Cost Constants
Finer constants weigh CPU work:
cpu_tuple_cost: per-row processingcpu_index_tuple_cost: per index entrycpu_operator_cost: per operator/function call
These are rarely changed but matter for CPU-heavy expressions.
Testing a Change at Session Level
Test constants with SET in your session before making them permanent. Compare plans with EXPLAIN.
SET random_page_cost = 1.1;
EXPLAIN SELECT * FROM orders WHERE customer_id = 42;Making It Permanent
Once satisfied, set the value cluster-wide and reload, or edit postgresql.conf.
ALTER SYSTEM SET random_page_cost = 1.1;
SELECT pg_reload_conf();Beware of Over-Tuning
Do not chase a single slow query by skewing constants globally — you may break thousands of other plans. Fix the root cause (missing index, stale stats) first; adjust constants only for genuine hardware mismatches.
Per-Tablespace Tuning
If some data sits on SSD and some on HDD, you can set random_page_cost per tablespace so the planner reasons about each storage medium correctly.
ALTER TABLESPACE fast_ssd
SET (random_page_cost = 1.1);Verifying the Effect
After changing a constant, confirm the planner actually switched plans. Run EXPLAIN before and after and look for a Seq Scan turning into an Index Scan (or vice versa).
EXPLAIN SELECT * FROM orders WHERE customer_id = 42;
SET random_page_cost = 1.1;
EXPLAIN SELECT * FROM orders WHERE customer_id = 42;Quick Check
Test your planner-tuning knowledge.
Recap
You learned planner cost tuning:
- Cost constants model your hardware for the planner
- Lower
random_page_coston SSDs to favor index scans - Set
effective_cache_sizeto ~50-75% of RAM - Test with
SETand EXPLAIN beforeALTER SYSTEM - Fix indexes/stats before skewing constants globally
เรียนรู้ SQL ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 22
- บทเรียน
- 88
คำถามที่พบบ่อย
บทเรียน “การปรับค่าคงที่ต้นทุนของตัววางแผนการสืบค้น” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การปรับค่าคงที่ต้นทุนของตัววางแผนการสืบค้น” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส PostgreSQL Performance & Query Optimization ให้อัปเกรดเป็น CoddyKit PRO คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การปรับค่าคงที่ต้นทุนของตัววางแผนการสืบค้น”
เรียนรู้ว่าค่าคงที่ต้นทุนของตัววางแผน เช่น random_page_cost และ effective_cache_size มีผลต่อการเลือกแผนอย่างไร และวิธีปรับค่าให้เหมาะกับฮาร์ดแวร์และข้อมูลของคุณ คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน PostgreSQL Performance & Query Optimization บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การปรับค่าคงที่ต้นทุนของตัววางแผนการสืบค้น” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม
ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พารามิเตอร์สำคัญใน postgresql.conf
- การปรับแต่งหน่วยความจำ (shared_buffers, work_mem)
- การปรับแต่งดิสก์ I/O และจุดตรวจสอบ
- การปรับค่าคงที่ต้นทุนของตัววางแผนการสืบค้น