Werken met tijdelijke tabellen

Queryprestaties verbeteren in PostgreSQL

Amy McCarty

Instructor

Over tijdelijke (temp) tabellen

Wat?

  • Kortlevende tabel

Waarom?

  • Tijdelijke opslag
  • Databasesessie
  • Meerdere queries
  • Gebruikersspecifiek
  • Trage tabellen

Hoe?

  • CREATE TEMP TABLE name AS
Queryprestaties verbeteren in PostgreSQL

TEMP-tabelstructuur

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';

 

 

 

USA-feestdagen

holiday holiday_type
Epiphany religious
Thanksgiving secular
Queryprestaties verbeteren in PostgreSQL

Trage, grote tabellen

  • Traag door veel records

 

Tabelstatistiek World Holidays USA Holidays
Type table temp table
# Rijen 591,444 25
Queryprestaties verbeteren in PostgreSQL

Trage, complexe views

  • Traag door view-logica

Diagram dat toont dat individuele landtabellen de World Holidays-view voeden

Tabelstatistiek World Holidays USA Holidays
Type view temp_table
# Rijen 591,444 25
Bronnen 195 1
  • Tabellen bevatten data
  • Views bevatten de aanwijzingen naar data
Queryprestaties verbeteren in PostgreSQL

Meerdere tabellen aan één koppelen

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)  )
, ...
Queryprestaties verbeteren 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

Queryplanner (uitvoerstappen)

Meerdere koks rond een grote kookpot

  • Statistieken uit pg_statistics
  • Schattingen van runtime
Queryprestaties verbeteren in PostgreSQL

Laten we oefenen!

Queryprestaties verbeteren in PostgreSQL

Preparing Video For Download...