Queryprestaties verbeteren in PostgreSQL
Amy McCarty
Instructor
| Systeem | Front-end stappen | Back-end processen | |
|---|---|---|---|
| 1 | Parser | Stuur query naar database | Controleert syntax. Vertaalt SQL naar computer-vriendelijkere syntax op basis van systeemsregels. |
| 2 | Planner & Optimizer | Beoordeel en optimaliseer taken | Gebruikt databasestatistieken om een queryplan te maken. Berekent kosten en kiest het beste plan. |
| 3 | Executor | Geef queryresultaten terug | Volgt het queryplan om de query uit te voeren. |
Reageert op wijzigingen in SQL-structuur
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 op cheeses (cost=0.00..10.50 rows=5725 width=296)
Seq Scan op cheeses (cost=0.00..10.50 rows=5725 width=296)
..10.50 : totale tijd
totale tijd = opstart + looptijd
Seq Scan op 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[]))
Queryprestaties verbeteren in PostgreSQL