Download - Komplex und schnell_20_min

Transcript
Page 1: Komplex und schnell_20_min

Komplex und schnell?Machen Sie Ihrer PostgreSQL Beine!

Susanne EbrechtWestfalen 2013

Page 2: Komplex und schnell_20_min

Referentin

Doktorandin

Datenbankexpertin

OpenSource Aktivistin

Mehr als 25 Jahre Erfahrung in der Wirtschaft

Seit vielen Jahren international tätig

Page 3: Komplex und schnell_20_min

Spielregeln

Twitter @miraceesusanneZwischenfragen sind WillkommenKeine IndividualberatungFolien haben keine alleinstehende Aussagekraft

Page 4: Komplex und schnell_20_min

SQL

Data Definition LanguageCREATE, ALTER, DROP

Data Modification LanguageINSERT, UPDATE, DELETE

Data Query LanguageSELECT

Data Control LanguageGRANT, REVOKE

Transaction Control LanguageSTART TRANSACTION, SAVEPOINT, COMMIT, ROLLBACK

Page 5: Komplex und schnell_20_min

Indizierung

Gezielte Indizierung... WHERE col1 = x AND col2 = y

ein Index für col1, einer für col2... WHERE (col1, col2) = (x, y)

ein Index für (col1, col2)

Page 6: Komplex und schnell_20_min

Joins

B

B

A B A

A B A

A B

INNER JOINOUTER JOINS

LEFT JOIN RIGHT JOIN

SELECT * FROM A JOIN B ON A.id=B.id;SELECT * FROM A, B WHERE A.id=B.id;SELECT A.* FROM A WHERE A.id IN (SELECT B.id FROM B);

SELECT * FROM A LEFT JOIN B ON A.id=B.id

WHERE B.id IS NULL

SELECT * FROM A RIGHT JOIN B ON B.id=A.id

WHERE A.id IS NULL

B

A B

FULL JOIN

A

SELECT * FROM A FULL JOIN B ON A.id=B.id

WHERE A.id IS NULL OR B.id is NULL

Page 7: Komplex und schnell_20_min

Schritt für Schritt

A B C A B C A B C

AB AC CB

ABC ACB CBA

Page 8: Komplex und schnell_20_min

Planer

EXPLAINPlanung

EXPLAIN ANALYZEPlanung + Ausführung

Page 9: Komplex und schnell_20_min

EXPLAINknolle=# EXPLAIN SELECT s.stadt, k.verstoss, SUM(kv.betrag) AS gesamt FROM stadt AS s JOIN knoellchenvergabe AS kv ON s.kennzeichen=kv.stadt JOIN knoellchen AS k ON kv.verstoss=k.verstoss GROUP BY s.stadt, k.verstoss ORDER BY gesamt DESC LIMIT 10;

QUERY PLAN ------------------------------------------------------------------------------------------------------------------------- Limit (cost=4878.07..4878.10 rows=10 width=69) -> Sort (cost=4878.07..4941.18 rows=25245 width=69) Sort Key: (sum(kv.betrag)) -> GroupAggregate (cost=3827.64..4332.54 rows=25245 width=69) -> Sort (cost=3827.64..3890.75 rows=25245 width=69) Sort Key: s.stadt, k.verstoss -> Merge Join (cost=561.98..945.76 rows=25245 width=69) Merge Cond: (k.verstoss = kv.verstoss) -> Sort (cost=71.17..73.72 rows=1020 width=32) Sort Key: k.verstoss -> Seq Scan on knoellchen k (cost=0.00..20.20 rows=1020 width=32) -> Sort (cost=490.81..503.19 rows=4950 width=67) Sort Key: kv.verstoss -> Hash Join (cost=33.50..187.05 rows=4950 width=67) Hash Cond: ((s.kennzeichen)::text = (kv.stadt)::text) -> Seq Scan on stadt s (cost=0.00..19.90 rows=990 width=48) -> Hash (cost=21.00..21.00 rows=1000 width=37) -> Seq Scan on knoellchenvergabe kv (cost=0.00..21.00 rows=1000 width=37)

Page 10: Komplex und schnell_20_min

EXPLAIN ANALYZEknolle=# EXPLAIN ANALYZE SELECT ... QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4878.07..4878.10 rows=10 width=69) (actual time=26.814..26.815 rows=10 loops=1) -> Sort (cost=4878.07..4941.18 rows=25245 width=69) (actual time=26.812..26.812 rows=10 loops=1) Sort Key: (sum(kv.betrag)) Sort Method: top-N heapsort Memory: 25kB -> GroupAggregate (cost=3827.64..4332.54 rows=25245 width=69) (actual time=25.631..26.597 rows=256 loops=1) -> Sort (cost=3827.64..3890.75 rows=25245 width=69) (actual time=25.617..25.712 rows=1000 loops=1) Sort Key: s.stadt, k.verstoss Sort Method: quicksort Memory: 125kB -> Merge Join (cost=561.98..945.76 rows=25245 width=69) (actual time=10.094..12.171 rows=1000 loops=1) Merge Cond: (k.verstoss = kv.verstoss) -> Sort (cost=71.17..73.72 rows=1020 width=32) (actual time=0.102..0.103 rows=13 loops=1) Sort Key: k.verstoss Sort Method: quicksort Memory: 25kB -> Seq Scan on knoellchen k (cost=0.00..20.20 rows=1020 width=32) (actual time=0.009..0.014 rows=13 loops=1) -> Sort (cost=490.81..503.19 rows=4950 width=67) (actual time=9.986..10.061 rows=1000 loops=1) Sort Key: kv.verstoss Sort Method: quicksort Memory: 125kB -> Hash Join (cost=33.50..187.05 rows=4950 width=67) (actual time=1.684..2.487 rows=1000 loops=1) Hash Cond: ((s.kennzeichen)::text = (kv.stadt)::text) -> Seq Scan on stadt s (cost=0.00..19.90 rows=990 width=48) (actual time=0.003..0.011 rows=21 loops=1) -> Hash (cost=21.00..21.00 rows=1000 width=37) (actual time=1.659..1.659 rows=1000 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 69kB -> Seq Scan on knoellchenvergabe kv (cost=0.00..21.00 rows=1000 width=37) (actual time=0.007..0.674 rows=1000 loops=1) Total runtime: 26.920 ms

Page 11: Komplex und schnell_20_min

ANALYZE

knolle=# ANALYZE;

knolle=# EXPLAIN ANALYZE SELECT s.stadt, k.verstoss, sum(kv.betrag) as gesamtFROM stadt as s JOIN knoellchenvergabe as kv ON s.kennzeichen=kv.stadt JOIN knoellchen as k ON kv.verstoss=k.verstossGROUP BY s.stadt, k.verstoss ORDER BY gesamt desc LIMIT 10;

QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------Limit (cost=67.39..67.42 rows=10 width=44) (actual time=5.586..5.590 rows=10 loops=1) -> Sort (cost=67.39..68.08 rows=273 width=44) (actual time=5.584..5.586 rows=10 loops=1) Sort Key: (sum(kv.betrag)) Sort Method: top-N heapsort Memory: 25kB -> HashAggregate (cost=58.77..61.49 rows=273 width=44) (actual time=5.080..5.240 rows=256 loops=1) -> Hash Join (cost=2.77..51.27 rows=1000 width=44) (actual time=0.084..2.812 rows=1000 loops=1) Hash Cond: (kv.verstoss = k.verstoss) -> Hash Join (cost=1.47..36.22 rows=1000 width=44) (actual time=0.048..1.716 rows=1000 loops=1) Hash Cond: ((kv.stadt)::text = (s.kennzeichen)::text) -> Seq Scan on knoellchenvergabe kv (cost=0.00..21.00 rows=1000 width=37) (actual time=0.008..0.326 rows=1000 loops=1) -> Hash (cost=1.21..1.21 rows=21 width=12) (actual time=0.028..0.028 rows=21 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 1kB -> Seq Scan on stadt s (cost=0.00..1.21 rows=21 width=12) (actual time=0.003..0.014 rows=21 loops=1) -> Hash (cost=1.13..1.13 rows=13 width=30) (actual time=0.027..0.027 rows=13 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 1kB -> Seq Scan on knoellchen k (cost=0.00..1.13 rows=13 width=30) (actual time=0.008..0.015 rows=13 loops=1) Total runtime: 5.686 ms

Page 12: Komplex und schnell_20_min

PGAdminIII

Page 13: Komplex und schnell_20_min

explain.depesz.com

Hubert Lubaczewski, Nickname: depesz

Page 14: Komplex und schnell_20_min

explain.depesz.com

Hubert Lubaczewski, Nickname: depesz

Page 15: Komplex und schnell_20_min

Analyse

• (cost=0.00..19.90 rows=990 width=48) (actual time=0.003..0.011 rows=21 loops=1)

• ANALYZE oder STATISTIC TARGET

• (actual time=10.081..15.764 rows=1000 loops=651)

• Logik überdenken, Umgestaltung der Anfrage, ggf. CTE (Common Table Expression)

• (actual time=25.617..12425.712 rows=1000 loops=1)

• Logik überdenken, Umgestaltung der Anfrage, ggf. Indizierung

Page 16: Komplex und schnell_20_min

Seq Scan

Tabelle

Page 1

Page 2

Page 3

...

Page n

Sychronize SeqscanPage 1

Page 2

Page 3

...

Page n

Page 17: Komplex und schnell_20_min

Index Scan

B-Baum

Blatt 1

Blatt 2

Blatt 3

...

Blatt n

TabellePage 1

Page 2

Page 3

...

Page n

Wurzel

Page 18: Komplex und schnell_20_min

Bitmap-Index-Scan

B-Baum

Blatt 1

Blatt 2

Blatt 3

...

Blatt n

TabellePage 1

Page 2

Page 3

...

Page n

Wurzel

01100001001100000110101100010

Page 19: Komplex und schnell_20_min

Geschwindigkeit

Tabellengröße

Antw

ortze

it

Seqsca

n

Indexscan

Bitmapscan

Page 20: Komplex und schnell_20_min

Nested Loop

Index A

Blatt 1

Blatt 2

Blatt 3

...

Blatt n

Tabelle APage 1

Page 2

Page 3

...

Page n

Wurzel

Index B

Blatt 1

Blatt 2

Blatt 3

...

Blatt n

Tabelle BPage 1

Page 2

Page 3

...

Page n

Wurzel

Page 21: Komplex und schnell_20_min

Merge Join

1. Datensatz 2. Datensatz

Voraussetzung: Sortierte Datensätze

Page 22: Komplex und schnell_20_min

Hash Join

1. Datensatz 2. DatensatzHash Lookup

•Hash wird erzeugt und zum Joinen genutzt•Verknüpfung von großer und kleiner Tabelle•Hoher work_mem•Notfall-Mechanismus schützt vor Speicherüberlauf

Page 23: Komplex und schnell_20_min

Langsam

SELECT COUNTAggregate MIN(), MAX(), ...DISTINCT

SELECT COUNT (DISTINCT ...)Correlated SubselectsINNER JOINS schneller als OUTER

Page 24: Komplex und schnell_20_min

Zusammenfassung

Gezielt DenormalisierenGezielt IndizierenPrüfen ob ANALYZE gelaufen istEXPLAIN ANALYZE zur AnalyseINNER schneller als OUTERAggregate und DISTINCT sind langsam