การปรับการรวมตารางแบบ LATERAL และการค้นหาที่สัมพันธ์กัน
เรียนรู้ว่าการรวมตารางแบบ LATERAL ช่วยให้คำสืบค้นย่อยอ้างอิงคอลัมน์จากตารางก่อนหน้าได้อย่างไร และใช้แทนคำสืบค้นย่อยที่สัมพันธ์กันซึ่งทำงานช้า ด้วยการค้นหาแต่ละแถวที่มีประสิทธิภาพได้อย่างไร
การปรับการรวมตารางแบบ LATERAL และการค้นหาที่สัมพันธ์กัน เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน PostgreSQL Performance & Query Optimization และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is a LATERAL Join?
A LATERAL join lets a subquery in the FROM clause reference columns from tables listed before it. Without LATERAL, a subquery in FROM cannot see the outer rows.
The Problem It Solves
Suppose you want, for each customer, their three most recent orders. A plain join cannot easily limit rows per customer. LATERAL evaluates the subquery once per outer row, so a per-row LIMIT works.
Basic Syntax
Place LATERAL before the subquery and reference the outer table inside it.
SELECT c.name, o.id, o.total
FROM customers c
CROSS JOIN LATERAL (
SELECT id, total
FROM orders
WHERE orders.customer_id = c.id
ORDER BY created_at DESC
LIMIT 3
) o;LEFT JOIN LATERAL
Use LEFT JOIN LATERAL ... ON true when you still want outer rows that have no matching subquery results, such as customers with no orders.
SELECT c.name, o.id
FROM customers c
LEFT JOIN LATERAL (
SELECT id FROM orders
WHERE orders.customer_id = c.id
ORDER BY created_at DESC LIMIT 1
) o ON true;LATERAL vs Correlated Subquery
A correlated subquery in the SELECT list can only return one column per row. LATERAL can return multiple columns and multiple rows, making it far more flexible.
Indexing for LATERAL
Because the subquery runs once per outer row, the inner filter and sort must be index-backed. Create a composite index matching the WHERE and ORDER BY columns.
CREATE INDEX idx_orders_cust_created
ON orders (customer_id, created_at DESC);Reading the Plan
A well-optimized LATERAL shows a Nested Loop with an Index Scan on the inner side. If you see a Seq Scan inside the loop, the supporting index is missing.
EXPLAIN ANALYZE
SELECT c.name, o.id
FROM customers c
CROSS JOIN LATERAL (
SELECT id FROM orders
WHERE orders.customer_id = c.id
ORDER BY created_at DESC LIMIT 3
) o;LATERAL with Set-Returning Functions
LATERAL also works with functions like unnest or jsonb_array_elements, expanding array columns per row.
SELECT p.id, tag
FROM products p
CROSS JOIN LATERAL unnest(p.tags) AS tag;Top-N Per Group Pattern
The most common use of LATERAL is the top-N-per-group query. It is usually faster than window-function approaches when N is small and an index supports the order.
When to Avoid LATERAL
If the outer table is huge and the inner subquery has no supporting index, running it millions of times is slow. In that case a window function or a single aggregated join may win. Always measure both.
Passing Computed Values Forward
LATERAL can also compute an intermediate value and reuse it in later expressions, avoiding repeating the same calculation. Each LATERAL block sees the columns produced before it.
SELECT o.id, m.margin
FROM orders o
CROSS JOIN LATERAL (
SELECT o.total - o.cost AS margin
) m
WHERE m.margin > 0;Quick Check
Test your LATERAL knowledge.
Recap
You learned LATERAL joins:
- They let a FROM-clause subquery see earlier tables' columns
- Perfect for top-N-per-group and per-row lookups
- Use
LEFT JOIN LATERAL ... ON trueto keep unmatched outer rows - Back the inner filter and sort with a composite index
- Confirm a Nested Loop + Index Scan in the plan
เรียนรู้ SQL ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 22
- บทเรียน
- 88
คำถามที่พบบ่อย
บทเรียน “การปรับการรวมตารางแบบ LATERAL และการค้นหาที่สัมพันธ์กัน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การปรับการรวมตารางแบบ LATERAL และการค้นหาที่สัมพันธ์กัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส PostgreSQL Performance & Query Optimization ให้อัปเกรดเป็น CoddyKit PRO คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การปรับการรวมตารางแบบ LATERAL และการค้นหาที่สัมพันธ์กัน”
เรียนรู้ว่าการรวมตารางแบบ LATERAL ช่วยให้คำสืบค้นย่อยอ้างอิงคอลัมน์จากตารางก่อนหน้าได้อย่างไร และใช้แทนคำสืบค้นย่อยที่สัมพันธ์กันซึ่งทำงานช้า ด้วยการค้นหาแต่ละแถวที่มีประสิทธิภาพได้อย่างไร คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน PostgreSQL Performance & Query Optimization บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การปรับการรวมตารางแบบ LATERAL และการค้นหาที่สัมพันธ์กัน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม
ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำความเข้าใจอัลกอริทึมการเชื่อมตาราง
- การเขียนการเชื่อมตารางที่ซับซ้อนใหม่
- คำสั่งค้นหาย่อย เทียบกับ CTE และการเชื่อมตาราง
- การปรับการรวมตารางแบบ LATERAL และการค้นหาที่สัมพันธ์กัน