PostgreSQL'de Sorgu Performansını İyileştirme
Amy McCarty
Instructor
| Sistem | Ön uç adımları | Arka uç işlemleri | |
|---|---|---|---|
| 1 | Parser | Sorguyu veritabanına gönderir | Söz dizimini kontrol eder. Sistemdeki kurallara göre SQL’i daha makine dostu biçime çevirir. |
| 2 | Planner & Optimizer | Sorgu görevlerini değerlendirir ve iyileştirir | Veritabanı istatistiklerini kullanarak sorgu planı oluşturur. Maliyetleri hesaplar ve en iyi planı seçer. |
| 3 | Executor | Sorgu sonuçlarını döndürür | Sorgu planını izleyerek sorguyu çalıştırır. |
SQL yapısındaki değişikliklere duyarlı
SELECT * FROM pg_class
WHERE relname = 'mytable'
-- örnek çıktı sütunları
| relname | relhasindex |
SELECT * FROM pg_stats
WHERE tablename = 'mytable'
-- örnek çıktı sütunları
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 : toplam süre
toplam süre = başlatma + çalışma süresi
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'de Sorgu Performansını İyileştirme