การระบุคอขวดด้านประสิทธิภาพ
ใช้แผนคำสั่งค้นหาเพื่อระบุการดำเนินการที่ช้า การรับส่งข้อมูลเข้าออกที่มากเกินไป และปัจจัยอื่นที่ลดทอนประสิทธิภาพ
การระบุคอขวดด้านประสิทธิภาพ เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน PostgreSQL Performance & Query Optimization และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What's a Query Bottleneck?
Imagine your database query as a car race. A bottleneck is like a slow section of the track that causes all cars to slow down, holding up the entire race.
In PostgreSQL, a bottleneck is any operation within a query execution plan that consumes a disproportionate amount of resources (time, CPU, I/O) and thus slows down the entire query.
Why Find Bottlenecks?
Identifying bottlenecks is the first step towards improving query performance. By pinpointing the slowest parts, you can focus your optimization efforts where they'll have the biggest impact.
- Faster Queries: Your applications respond quicker.
- Less Resource Usage: Database server runs more efficiently.
- Better User Experience: Happier users!
Key EXPLAIN ANALYZE Metrics
When using EXPLAIN ANALYZE, several metrics help us spot bottlenecks:
actual time: The real time (in milliseconds) spent executing an operation. This is crucial!rows: Number of rows processed or returned by an operation.loops: How many times an operation was executed.Buffers: Details on disk I/O, indicating how much data was read from or written to memory/disk.
Spotting Slow Operations: `actual time`
The most direct way to find a slow operation is to look for plan nodes with a high actual time value.
Compare the actual time for different nodes. If one node's actual time is significantly higher than others, it's likely a bottleneck.
Remember, actual time has two values: (start_time..end_time). We care about the difference, which is the total time for that node.
Example: High `actual time` Seq Scan
Let's create a table and run a query that will likely result in a slow Seq Scan (sequential scan) because it lacks an index on the filtered column.
Notice the high actual time for the Seq Scan node in the simulated output:
-> Seq Scan on products (cost=0.00..15.50 rows=1000 width=36) (actual time=50.231..120.567 rows=100 loops=1)
Filter: (price > 90)
Rows Removed by Filter: 900
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
price INT
);
INSERT INTO products (name, price)
SELECT 'Product ' || i, (i % 100) + 1
FROM generate_series(1, 1000) s(i);
EXPLAIN ANALYZE SELECT * FROM products WHERE price > 90;Excessive I/O: `Buffers`
High disk I/O is a common bottleneck. The Buffers section in EXPLAIN ANALYZE helps identify this.
shared hit: Data found in shared buffers (memory). Good!shared read: Data had to be read from disk. This is what we want to minimize!shared dirtied/written: Data modified/written to disk.
Many shared read buffers often indicate a need for better indexing or a more selective query.
Example: High `Buffers: shared read`
If a query needs to scan a large portion of a table that isn't cached, you'll see many shared read buffers.
Here, even with an index, scanning a large range might still involve reading many blocks from disk:
-> Index Scan using products_price_idx on products (cost=0.43..15.50 rows=500 width=36) (actual time=5.123..25.456 rows=500 loops=1)
Index Cond: (price BETWEEN 10 AND 60)
Buffers: shared hit=100 read=400
The read=400 indicates 400 data blocks were fetched from disk. This can be a bottleneck.
CREATE INDEX products_price_idx ON products (price);
EXPLAIN ANALYZE SELECT * FROM products WHERE price BETWEEN 10 AND 60;Inefficient Filtering: Many Rows, Few Results
Sometimes an operation processes many rows but discards most of them with a filter. This is inefficient.
Look for a plan node where rows is very high, but the number of Rows Removed by Filter is also high, meaning a lot of work was done only to throw data away.
This often suggests that the filter could be applied earlier, perhaps with a more specific index.
Other Bottleneck Indicators
Beyond time and I/O, keep an eye on these:
Sortoperations: If a sort takes a long time or reportsSort Method: external merge Disk: XkB, it means it couldn't fit inwork_memand spilled to disk, which is slow.- High
work_memusage: Some operations (like hashes or sorts) need memory. If they use a lot, it can impact other queries or cause disk spills. - Expensive Join Types: Nested Loop, Hash Join, or Merge Join can be bottlenecks if they process huge intermediate result sets.
Identify the Bottleneck
Consider the following simplified EXPLAIN ANALYZE output:
-> Hash Join (cost=10.00..200.00 rows=1000 width=64) (actual time=10.000..1500.000 rows=1000 loops=1)
Hash Cond: (a.id = b.id)
Buffers: shared hit=50 read=1000
-> Seq Scan on table_a a (cost=0.00..100.00 rows=10000 width=32) (actual time=0.100..50.000 rows=10000 loops=1)
Buffers: shared hit=10 read=90
-> Hash (cost=9.00..9.00 rows=1000 width=32) (actual time=9.000..9.000 rows=1000 loops=1)
-> Seq Scan on table_b b (cost=0.00..9.00 rows=1000 width=32) (actual time=0.050..8.000 rows=1000 loops=1)Which part of this plan is the most likely bottleneck?
Recap: Pinpointing Bottlenecks
You've learned to identify performance bottlenecks in PostgreSQL query plans!
Key takeaways:
- Look for high
actual timeto find slow operations. - Monitor
Buffers: shared readfor excessive disk I/O. - Be wary of operations processing many rows but returning few.
Sortoperations spilling to disk are also red flags.
With these skills, you can now effectively diagnose why your queries are running slow!
คำถามที่พบบ่อย
บทเรียน “การระบุคอขวดด้านประสิทธิภาพ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การระบุคอขวดด้านประสิทธิภาพ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การระบุคอขวดด้านประสิทธิภาพ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม
ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ EXPLAIN และ ANALYZE
- การตีความโหนดในแผน
- การระบุคอขวดด้านประสิทธิภาพ
- การอ่านค่าประมาณต้นทุนและจำนวนแถวจาก EXPLAIN