क्वेरी संरचना और निष्पादन

PostgreSQL में क्वेरी प्रदर्शन सुधारना

Amy McCarty

Instructor

सबक्वेरी और जॉइन

-- SUBQUERY
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 में क्वेरी प्रदर्शन सुधारना

क्वेरी प्लान

Aggregate  ()
  ->  Hash Join  ()
        Hash Cond: (athletes.country = climate.country)
        ->  Seq Scan on athletes  ()
        ->  Hash  ()
              ->  Seq Scan on climate  ()
                    Filter: (temp_annual > '22'::numeric)
PostgreSQL में क्वेरी प्रदर्शन सुधारना

Common table expressions और टेम्पररी टेबल्स

-- CTE
WITH celsius AS 
(
  SELECT country 
  FROM climate 
  WHERE temp_annual > 22 -- Celsius
)
SELECT count(athlete_id)
FROM athletes a
INNER JOIN celsius c
  ON a.country = c.country
-- TEMP TABLE
CREATE TEMPORARY TABLE celsius AS 
  SELECT country 
  FROM climate 
  WHERE temp_annual > 22; -- Celsius

SELECT count(athlete_id)
FROM athletes a
INNER JOIN celsius c
  ON a.country = c.country
PostgreSQL में क्वेरी प्रदर्शन सुधारना

क्वेरी प्लान

  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 में क्वेरी प्रदर्शन सुधारना

डेटा सीमित करना

  SELECT country_code
  , COUNT(athlete_id) as athletes
  FROM athletes
  WHERE year in (2014, 2010) -- Indexed column
  GROUP BY country_code
PostgreSQL में क्वेरी प्रदर्शन सुधारना

डेटा सीमित करना

  SELECT country_code
  , COUNT(athlete_id) as athletes
  FROM athletes
  WHERE year in (2014, 2010) -- Indexed column
  GROUP BY country_code
No Index Index
Planning Time: 3.370 ms Planning Time: 0.163 ms
Execution Time: 0.143 ms Execution Time: 0.062 ms
PostgreSQL में क्वेरी प्रदर्शन सुधारना

एग्रीगेशन - अलग ग्रैन्युलैरिटी

 

SELECT r.country
  , COUNT(a.athlete_id) as athletes
FROM regions r -- country level
INNER JOIN athletes a -- athletes level
  ON r.country = a.country
GROUP BY r.country

 

 

  • Execution Time : 0.267 ms
PostgreSQL में क्वेरी प्रदर्शन सुधारना

एग्रीगेशन - ग्रैन्युलैरिटी बदलना

WITH olympians AS ( -- country level
  SELECT country
  , COUNT(athlete_id) as athletes
  FROM athletes -- athletes level
  GROUP BY country
)
SELECT country, athletes
FROM regions r -- country level
INNER JOIN olympians o
  ON r.country = o.country
Execution Time
Join 1st 0.267 ms
Aggregate 1st 0.192 ms
PostgreSQL में क्वेरी प्रदर्शन सुधारना

अभ्यास करते हैं!

PostgreSQL में क्वेरी प्रदर्शन सुधारना

Preparing Video For Download...