การเพิ่มประสิทธิภาพคิวรีใน PostgreSQL
Amy McCarty
Instructor
| ระบบ | ขั้นตอน Front end | กระบวนการ Back end | |
|---|---|---|---|
| 1 | Parser | ส่งคิวรีไปยังฐานข้อมูล | ตรวจสอบ syntax และแปลง SQL ให้อยู่ในรูปแบบที่คอมพิวเตอร์เข้าใจได้ง่ายขึ้น โดยอิงจากกฎที่เก็บไว้ในระบบ |
| 2 | Planner & Optimizer | ประเมินและปรับคิวรีให้เหมาะสม | ใช้สถิติของฐานข้อมูลเพื่อสร้าง query plan คำนวณต้นทุนและเลือกแผนที่ดีที่สุด |
| 3 | Executor | ส่งคืนผลลัพธ์ของคิวรี | ดำเนินการคิวรีตาม query plan |
ตอบสนองต่อการเปลี่ยนแปลงโครงสร้าง SQL
SELECT * FROM pg_class
WHERE relname = 'mytable'
-- sample of output columns
| relname | relhasindex |
SELECT * FROM pg_stats
WHERE tablename = 'mytable'
-- sample of output columns
null_frac | avg_width | n_distinct |
EXPLAIN
SELECT * FROM cheeses
Seq Scan on cheeses
(cost=0.00..10.50 rows=5725 width=296)
Seq Scan on cheeses (cost=0.00..10.50 rows=5725 width=296)
Seq Scan on cheeses (cost=0.00..10.50 rows=5725 width=296)
..10.50 : เวลาทั้งหมด
เวลาทั้งหมด = เวลาเริ่มต้น + เวลารัน
Seq Scan on cheeses (cost=0.00..10.50 rows=5725 width=296)
EXPLAIN
SELECT * FROM cheeses WHERE species IN ('goat','sheep')
Seq Scan on cheeses (cost=0.00..378.90 rows=3 width=118)
-> Filter: (species = ANY ('{"goat","sheep"}'::text[]))
EXPLAIN
SELECT * FROM cheeses WHERE species IN ('goat','sheep') -- index on species column
Bitmap Index Scan using species_idx on cheeses (cost=0.29..12.66 rows=3 width=118)
Index Cond: (species = ANY ('{"goat","sheep"}'::text[]))
การเพิ่มประสิทธิภาพคิวรีใน PostgreSQL