Lavorare con tabelle temporanee

Migliorare le prestazioni delle query in PostgreSQL

Amy McCarty

Instructor

Info sulle tabelle temporanee

Cosa?

  • Tabella di breve durata

Perché?

  • Archivio temporaneo
  • Sessione del database
  • Più query
  • Specifica per utente
  • Tabelle lente

Come?

  • CREATE TEMP TABLE name AS
Migliorare le prestazioni delle query in PostgreSQL

Struttura della tabella TEMP

holiday holiday_type country_code
Epiphany religious CZE
Epiphany religious FRA
Epiphany religious USA
Thanksgiving secular USA
CREATE TEMP TABLE usa_holidays AS
  SELECT holiday, holiday_type
  FROM world_holidays
  WHERE country_code = 'USA';

 

 

 

Festività USA

holiday holiday_type
Epiphany religious
Thanksgiving secular
Migliorare le prestazioni delle query in PostgreSQL

Tabelle grandi e lente

  • Lenta per via dei molti record

 

Statistiche tabella World Holidays USA Holidays
Tipo table temp table
# righe 591,444 25
Migliorare le prestazioni delle query in PostgreSQL

View complesse e lente

  • Lenta per la logica della view

Diagramma che mostra che le singole tabelle paese alimentano la view World Holidays

Statistiche tabella World Holidays USA Holidays
Tipo view temp_table
# righe 591,444 25
Sorgenti 195 1
  • Le tabelle contengono dati
  • Le view contengono le istruzioni ai dati
Migliorare le prestazioni delle query in PostgreSQL

Join di molte tabelle su una

CREATE TEMP TABLE usa_holidays AS
  SELECT holiday, holiday_type
  FROM world_holidays
  WHERE country_code = 'USA';
WITH religious AS 
(   SELECT usa.holiday, r.initial_yr
     , r.celebration_dt
    FROM religious r
    INNER JOIN usa_holidays usa 
      USING (holiday)  )
, secular AS 
(   SELECT usa.holiday, s.initial_yr
     , s.celebration_dt
    FROM secular s
    INNER JOIN usa_holidays usa
      USING (holiday)  )
, ...
Migliorare le prestazioni delle query in PostgreSQL

ANALYZE

 

1 CREATE TEMP TABLE usa_holidays AS
2 SELECT holiday, holiday_type
3 FROM world_holidays
4 WHERE country_code = 'USA';
5 
6 ANALYZE usa_holidays;
7 
8 SELECT * FROM usa_holidays

Query planner (passi di esecuzione)

Più chef attorno a una grande pentola

  • Statistiche da pg_statistics
  • Stime runtime
Migliorare le prestazioni delle query in PostgreSQL

Passiamo alla pratica !

Migliorare le prestazioni delle query in PostgreSQL

Preparing Video For Download...