Sorgu yapısı ve sorgu yürütme

PostgreSQL'de Sorgu Performansını İyileştirme

Amy McCarty

Instructor

Alt sorgular ve join’ler

-- ALT SORGU
SELECT COUNT(athlete_id)
FROM athletes
WHERE country IN 
  (SELECT country FROM climate 
    WHERE temp_annual > 22)
-- JOIN
SELECT COUNT(athlete_id)
FROM athletes a
INNER JOIN climate c
  ON a.country = c.country
  AND c.temp_annual > 22
PostgreSQL'de Sorgu Performansını İyileştirme

Sorgu planı

Aggregate  ()
  ->  Hash Join  ()
        Hash Cond: (athletes.country = climate.country)
        ->  Seq Scan on athletes  ()
        ->  Hash  ()
              ->  Seq Scan on climate  ()
                    Filter: (temp_annual > '22'::numeric)
PostgreSQL'de Sorgu Performansını İyileştirme

CTE’ler ve geçici tablolar

-- CTE
WITH celsius AS 
(
  SELECT country 
  FROM climate 
  WHERE temp_annual > 22 -- Santigrat
)
SELECT count(athlete_id)
FROM athletes a
INNER JOIN celsius c
  ON a.country = c.country
-- GEÇİCİ TABLO
CREATE TEMPORARY TABLE celsius AS 
  SELECT country 
  FROM climate 
  WHERE temp_annual > 22; -- Santigrat

SELECT count(athlete_id)
FROM athletes a
INNER JOIN celsius c
  ON a.country = c.country
PostgreSQL'de Sorgu Performansını İyileştirme

Sorgu planı

  Aggregate  ()
  CTE celsius
    ->  Seq Scan on climate  ()
          Filter: (temp_annual > '22'::numeric)
  ->  Hash Join  ()
        Hash Cond: (a.country_code = c.country_code)
        ->  Seq Scan on athletes a  ()
        ->  Hash  ()
              ->  CTE Scan on celsius c  ()
PostgreSQL'de Sorgu Performansını İyileştirme

Veriyi sınırlama

  SELECT country_code
  , COUNT(athlete_id) as athletes
  FROM athletes
  WHERE year in (2014, 2010) -- Indexed column
  GROUP BY country_code
PostgreSQL'de Sorgu Performansını İyileştirme

Veriyi sınırlama

  SELECT country_code
  , COUNT(athlete_id) as athletes
  FROM athletes
  WHERE year in (2014, 2010) -- Indexed column
  GROUP BY country_code
Dizin yok Dizin var
Planlama Süresi: 3.370 ms Planlama Süresi: 0.163 ms
Yürütme Süresi: 0.143 ms Yürütme Süresi: 0.062 ms
PostgreSQL'de Sorgu Performansını İyileştirme

Toplamalar - farklı ayrıntı düzeyleri

 

SELECT r.country
  , COUNT(a.athlete_id) as athletes
FROM regions r -- ülke düzeyi
INNER JOIN athletes a -- sporcu düzeyi
  ON r.country = a.country
GROUP BY r.country

 

 

  • Yürütme Süresi: 0.267 ms
PostgreSQL'de Sorgu Performansını İyileştirme

Toplamalar - ayrıntıyı değiştirme

WITH olympians AS ( -- ülke düzeyi
  SELECT country
  , COUNT(athlete_id) as athletes
  FROM athletes -- sporcu düzeyi
  GROUP BY country
)
SELECT country, athletes
FROM regions r -- ülke düzeyi
INNER JOIN olympians o
  ON r.country = o.country
Yürütme Süresi
Önce Birleştir 0.267 ms
Önce Toplam 0.192 ms
PostgreSQL'de Sorgu Performansını İyileştirme

Haydi pratik yapalım!

PostgreSQL'de Sorgu Performansını İyileştirme

Preparing Video For Download...