การตีความโหนดในแผน
เรียนรู้การแปลความหมายของโหนดในแผนที่พบบ่อย เช่น การสแกนตามลำดับ การสแกนดัชนี ประเภทการเชื่อมตาราง และการเรียงลำดับ
การตีความโหนดในแผน เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน PostgreSQL Performance & Query Optimization และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Decoding Query Plans
Welcome back! In the previous lesson, you learned how to use EXPLAIN to view a query's execution plan. Now, let's dive into interpreting the different 'nodes' within these plans.
Each node represents a specific operation PostgreSQL performs. Understanding them is key to identifying performance bottlenecks.
What are Plan Nodes?
Think of a query plan as a tree, where each branch and leaf is a 'node'. These nodes tell you:
- What operation is being done (e.g., scanning, sorting, joining).
- How it's being done (e.g., sequentially, using an index).
- Cost estimates: How much time and resources PostgreSQL *expects* the operation to take.
We'll look at the most common and important node types.
Sequential Scan: The Full Read
A Sequential Scan (often called a 'Seq Scan') means PostgreSQL reads every single row in a table from start to finish to find the data it needs.
- When it happens: For small tables, or when querying a large portion of a table with no suitable index.
- Performance impact: Can be slow for large tables, especially if only a few rows are needed.
It's like looking through every page of a book to find one sentence.
Seq Scan Example
Let's see a sequential scan in action. We'll create a simple table and then query it without an index.
CREATE TABLE products (
product_id SERIAL PRIMARY KEY,
name VARCHAR(100),
price DECIMAL(10, 2)
);
INSERT INTO products (name, price) VALUES
('Laptop', 1200.00),
('Mouse', 25.00),
('Keyboard', 75.00),
('Monitor', 300.00);
EXPLAIN SELECT * FROM products WHERE price > 100;Index Scan: Targeted Search
An Index Scan is much more efficient. PostgreSQL uses an index to quickly locate the specific rows it needs, much like using an index in a book.
- When it happens: When a query uses a
WHEREclause on an indexed column, and the index is selective enough. - Performance impact: Generally much faster than a sequential scan for selective queries on large tables.
It allows PostgreSQL to jump directly to the relevant data pages.
Index Scan Example
Now, let's add an index to our products table and observe the change in the query plan.
CREATE INDEX idx_products_price ON products (price);
EXPLAIN SELECT * FROM products WHERE price > 100;Sort Node: Ordering Data
The Sort node appears when PostgreSQL needs to order data, typically for an ORDER BY or GROUP BY clause, and there isn't an index that can provide the data in the required order.
- When it happens: Explicit
ORDER BY, or implicitly for some operations likeGROUP BYor unique constraints. - Performance impact: Sorting can be CPU and I/O intensive, especially for large datasets.
If the sort happens 'on disk' (meaning it can't fit in memory), it becomes even slower.
Sort Node Example
Here's an example where PostgreSQL has to sort the results because no index exists for the ordering column.
EXPLAIN SELECT name, price FROM products ORDER BY name DESC;Join Nodes: Combining Tables
When you join two or more tables, PostgreSQL uses specific Join Nodes to combine the data. There are three primary types:
- Nested Loop Join: Often good for small inner tables or when an index is available.
- Hash Join: Efficient for larger tables where no useful index is present on the join key.
- Merge Join: Requires both inputs to be sorted on the join key, then merges them.
The choice depends on table sizes, available indexes, and data distribution.
Nested Loop Join Example
Let's create another table and then join it with products to see a Nested Loop Join. This often happens when one side of the join is small.
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
product_id INT,
quantity INT
);
INSERT INTO orders (product_id, quantity) VALUES
(1, 1),
(2, 2),
(1, 3);
EXPLAIN SELECT p.name, o.quantity
FROM products p JOIN orders o ON p.product_id = o.product_id
WHERE o.order_id = 2;Identify the Scan Type
Consider the following query and its execution plan snippet. What kind of scan is most likely being performed on the customers table?
EXPLAIN SELECT * FROM customers WHERE age > 30;
Partial Plan Output (assume no index on age):
-> Seq Scan on customers (cost=0.00..10.50 rows=3 width=...)Recap: Decoding Plan Nodes
You've taken a big step in understanding PostgreSQL performance by learning to interpret key plan nodes!
- Sequential Scan: Full table read, can be slow for large tables.
- Index Scan: Uses an index for targeted row access, faster for selective queries.
- Sort: Occurs when data needs ordering and no suitable index exists.
- Join Nodes: (Nested Loop, Hash, Merge) combine data from multiple tables, chosen based on data size and indexes.
In the next lesson, we'll put this knowledge to use to identify actual performance bottlenecks!
คำถามที่พบบ่อย
บทเรียน “การตีความโหนดในแผน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การตีความโหนดในแผน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส PostgreSQL Performance & Query Optimization ให้อัปเกรดเป็น CoddyKit PRO คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การตีความโหนดในแผน”
เรียนรู้การแปลความหมายของโหนดในแผนที่พบบ่อย เช่น การสแกนตามลำดับ การสแกนดัชนี ประเภทการเชื่อมตาราง และการเรียงลำดับ คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน PostgreSQL Performance & Query Optimization บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การตีความโหนดในแผน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม
ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ EXPLAIN และ ANALYZE
- การตีความโหนดในแผน
- การระบุคอขวดด้านประสิทธิภาพ
- การอ่านค่าประมาณต้นทุนและจำนวนแถวจาก EXPLAIN