Filteren in de WHERE-clausule

Queryprestaties verbeteren in PostgreSQL

Amy McCarty

Instructor

Beperk de data

Order Clause Purpose
1 FROM provides directions to the table(s)
2 WHERE filters or limits the records

 

  • Vindt vroeg plaats
  • Minder records
Queryprestaties verbeteren in PostgreSQL

EXPLAIN

EXPLAIN
SELECT * FROM phones

 

Seq Scan on phones (cost = 0.00..22.7
                    ,rows=1270
                    ,width=36)
  • Aantal uitvoeringstappen

Queryplanner

Meerdere koks rond een grote kookpot

Queryprestaties verbeteren in PostgreSQL

EXPLAIN met WHERE

EXPLAIN
SELECT * FROM phones
WHERE phone_code = 235

 

Seq Scan on phones (cost = 0.00..25.8
                    ,rows=6,width=636)
  Filter: (phone_code=235)

Queryplanner

Meerdere koks rond een grote kookpot

Queryprestaties verbeteren in PostgreSQL

Goed - Vergelijkbare waarden filteren met LIKE OR

country phone_code reliability
Chad 235 medium
China 86 high
Costa Rica 506 high
India 91 medium
Indonesia 62 medium
Iraq 964 low
EXPLAIN
SELECT * FROM phones
WHERE country LIKE 'Ch%' 
  OR country LIKE 'In%'

 

Seq Scan on phones (cost = 0.00..29.05
                    ,rows=13,width=36)
  Filter: ((country~~'Ch%'::text)
           OR(country~~'In%'::text))
Queryprestaties verbeteren in PostgreSQL

Beter - Vergelijkbare waarden filteren met LIKE ANY

country phone_code reliability
Chad 235 medium
China 86 high
Costa Rica 506 high
India 91 medium
Indonesia 62 medium
Iraq 964 low
EXPLAIN
SELECT * FROM phones
WHERE country 
  LIKE ANY(ARRAY['Ch%','In%'])

 

Seq Scan on phones (cost = 0.00..25.88
                    ,rows=13,width=36)
  Filter: ((country~~ANY('{Ch%,In%}'
                          ::text[]))
Queryprestaties verbeteren in PostgreSQL

Goed - Exacte waarden filteren met OR

country phone_code reliability
Chad 235 medium
China 86 high
Costa Rica 506 high
India 91 medium
Indonesia 62 medium
Iraq 964 low
EXPLAIN
SELECT * FROM phones
WHERE country = 'Chad' 
  OR country = 'China'

 

Seq Scan on phones (cost = 0.00..29.05
                    ,rows=13,width=36)
  Filter: ((country='Chad'::text)
           OR(country='China'::text))
Queryprestaties verbeteren in PostgreSQL

Beter - Exacte waarden filteren met IN

country phone_code reliability
Chad 235 medium
China 86 high
Costa Rica 506 high
India 91 medium
Indonesia 62 medium
Iraq 964 low
EXPLAIN
SELECT * FROM phones
WHERE country IN ('Chad','China')

 

Seq Scan on phones (cost = 0.00..25.88
                    ,rows=13,width=36)
  Filter: ((country=ANY('{Chad,China}'
                          ::text[]))
Queryprestaties verbeteren in PostgreSQL

Best - Getallen filteren

country phone_code reliability
Chad 235 medium
China 86 high
Costa Rica 506 high
India 91 medium
Indonesia 62 medium
Iraq 964 low
EXPLAIN
SELECT *
FROM phones
WHERE phone_code IN (235,86) 

 

Seq Scan on phones (cost = 0.00..25.88
                    ,rows=13,width=36)
  Filter: (phone_code=ANY('{235,86}'
                          ::integer[]))
Queryprestaties verbeteren in PostgreSQL

Samenvatting: beste WHERE-filters

 

Voordelen van numeriek

  • Korter
  • Minder opslag
  • Snellere performance
Queryprestaties verbeteren in PostgreSQL

Samenvatting: beste WHERE-filters

 

Voordelen van numeriek

  • Korter
  • Minder opslag
  • Snellere performance

 

Goed Beter
Tekst Numeriek
OR IN, ARRAY
Queryprestaties verbeteren in PostgreSQL

Laten we oefenen!

Queryprestaties verbeteren in PostgreSQL

Preparing Video For Download...