Meningkatkan Performa Kueri di PostgreSQL
Amy McCarty
Instructor
| Sistem | Langkah front end | Proses back end | |
|---|---|---|---|
| 1 | Parser | Kirim kueri ke basis data | Memeriksa sintaks. Menerjemahkan SQL ke sintaks yang lebih ramah komputer berdasarkan aturan tersimpan sistem. |
| 2 | Planner & Optimizer | Menilai dan mengoptimalkan tugas kueri | Menggunakan statistik basis data untuk membuat rencana kueri. Menghitung biaya dan memilih rencana terbaik. |
| 3 | Executor | Mengembalikan hasil kueri | Mengikuti rencana kueri untuk mengeksekusi kueri. |
Responsif terhadap perubahan struktur SQL
SELECT * FROM pg_class
WHERE relname = 'mytable'
-- contoh kolom keluaran
| relname | relhasindex |
SELECT * FROM pg_stats
WHERE tablename = 'mytable'
-- contoh kolom keluaran
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 pada cheeses (cost=0.00..10.50 rows=5725 width=296)
Seq Scan pada cheeses (cost=0.00..10.50 rows=5725 width=296)
..10.50 : waktu total
waktu total = waktu mulai + waktu jalan
Seq Scan pada 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[]))
Meningkatkan Performa Kueri di PostgreSQL