fbpx

5 Clarifications On Teradata Temporary Tables

By DWH Pro Admin

December 3, 2019


Teradata temporary tables come in different types and for different purposes. This article shows you the properties and uses of each one.

1. Derived Tables

  • Are materialized by a select statement within a query
  • Only exist within the time the query is executed
  • Use the spool space of the executing user
  • Disappear at the moment the query is finished

Derived tables must always be named with an alias, otherwise, there will be a syntax error!

Here is an example of a derived table (inside the parentheses):

SELECT * FROM (SELECT MAX(Revenue) FROM Invoices) AS InvoiceRevenue (MaxRevenue);

The table is named InvoiceRevenue and the column from the derived table is called MaxRevenue.

However, there are further possibilities to name the column of the derived table:

SELECT * FROM (SELECT MAX(Revenue) AS MaxRevenue FROM Invoices) AS InvoiceRevenue;

In the above example, we name the column already within the derived table.

Which method you use is a matter of taste, the result of the query is the same.

Derived Tables using the WITH Syntax

Here is another method to name a derived table and its columns in advance, and reference it later:

WITH InvoiceRevenue(MaxRevenue) AS  
(SELECT MAX(Revenue) AS MaxRevenue FROM Invoices)
SELECT * FROM  InvoiceRevenue; 

However, you must take into account that only one WITH statement is allowed per query.

The great thing about this syntax is that the table is pre-defined and can be easily inserted into the rest of the query.

Here is an example:

WITH InvoiceRevenue(MaxRevenue) AS  
(SELECT MAX(Revenue) AS MaxRevenue FROM Invoices)
SELECT * 
FROM 
  InvoiceRevenue t01 
INNER JOIN 
  Customer t02 
ON 
  t01.CustomerId = t02.CustomerId; 

It is also important to understand that the derived table exists only during query execution, not for the entire session.

Here is an example of two queries that are executed in one transaction. The second query will cause an error message:

BT;
WITH InvoiceRevenue(MaxRevenue) AS  
(SELECT MAX(Revenue) AS MaxRevenue FROM Invoices)
SELECT * 
FROM 
  InvoiceRevenue t01 
INNER JOIN 
  Customer t02 
ON 
  t01.CustomerId = t02.CustomerId; 

SELECT * FROM  InvoiceRevenue; --> causes an error
ET;

2. Volatile Tables

  • Are created and afterward materialized by adding data with and INSERT/SELECT statement or
  • Are created and materialized at the same time with a CREATE VOLATILE TABLE TheTable AS (<QUERY>) WITH DATA PRIMARY INDEX (PI) ON COMMIT PRESERVE ROWS statement
  • Use the spool space of the executing user.
  • Disappear at the moment the session is logged of
  • The table definition is stored in each AMP’s cache
CREATE VOLATILE TABLE Revenue, NO LOG
(
   CustomerId BIGINT NOT NULL,
   RevenueAmount DECIMAL(18,3)
) ON COMMIT PRESERVE ROWS;

NO LOG means that no Transient Journal is used.

This will usually not be necessary because it is a temporary table, and you will not need this functionality of a ROLLBACK.

NO LOG is the default setting and should usually not be changed.

ON COMMIT PRESERVE ROWS is not a default setting, and must therefore usually always be specified. Otherwise, the contents of the table are deleted immediately at the end of the transaction.

The default setting is ON COMMIT DELETE ROWS!

After the volatile table is created, it can be loaded like any other table:

INSERT INTO Revenue
SELECT CustomerId, SUM(Revenue) FROM Transaction;

Why do I need ON COMMIT DELETE ROWS?

Here is a practical example of how to use ON COMMIT DELETE ROWS.

As already mentioned, ON DELETE COMMIT ROWS deletes the data as soon as the corresponding transaction is completed.

We can take advantage of this to, for example, delete a volatile table immediately after it is no longer needed.

BT;
CREATE VOLATILE TABLE Revenue, NO LOG
(
   CustomerId BIGINT NOT NULL,
   RevenueAmount DECIMAL(18,3)
) ON COMMIT DELETE ROWS;

INSERT INTO Revenue SELECT CustomerId,AVG(Revenue) FROM Transaction;

SELECT * FROM Revenue; 
-- The volatile table still exists as the transaction is not ended!

ET; -- Now the volatile table is empty

-- Further Queries...

Additional possibilities to materialize volatile tables

–> Create the table with content

CREATE VOLATILE TABLE Revenue AS (SELECT SUM(Amount) AS AMT FROM Revenue_All) WITH DATA ON COMMIT PRESERVE ROWS;

–> Create the table empty

CREATE VOLATILE TABLE Revenue AS (SELECT SUM(Amount) ASAMT FROM Revenue_All ) WITH NO DATA ON COMMIT PRESERVE ROWS;
–> This volatile table contains only column AMT and ceratin table attributes from the table Transaction

CREATE VOLATILE TABLE Revenue AS (SELECT SUM(Amount) AS AMT FROM Revenue_All ) WITH DATA ON COMMIT PRESERVE ROWS;
–> This volatile table is a 1:1 copy of the table transaction



CREATE VOLATILE TABLE Revenue AS Revenue_All ) WITH DATA ON COMMIT PRESERVE ROWS;

How can I copy Statistics to the Volatile Table?

CREATE VOLATILE TABLE Revenue AS Revenue_All WITH DATA AND STATISTICS ON COMMIT PRESERVE ROWS;

How do I find all available volatile tables in my session?

All you have to do is execute this command:

HELP VOLATILE TABLE;

3. Global Temporary Tables

  • Are created with a CREATE TABLE DDL which is stored permanently in DBC tables
  • Are materialized with an INSERT/SELECT statement
  • Use the temp space of the executing user (not the spool space)
  • Disappear when the session is logged off (but the table definition stays stored in the DBC tables!)
  • Each user can materialize his own copy a Global Temporary Table

Global Temporary Tables are useful if the definition is to be used by several users.

Like a Volatile Table, the Global Temporary Table only exists in the session where it was materialized. However, unlike the Volatile Table, many users can materialize this table (as the definition is stored permanently in DBC tables).

__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"62516":{"name":"Main Accent","parent":-1}},"gradients":[]},"palettes":[{"name":"Default Palette","value":{"colors":{"62516":{"val":"var(--tcb-skin-color-0)"}},"gradients":[]},"original":{"colors":{"62516":{"val":"rgb(19, 114, 211)","hsl":{"h":210,"s":0.83,"l":0.45}}},"gradients":[]}}]}__CONFIG_colors_palette__
__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"b4fbe":{"name":"Main Accent","parent":-1}},"gradients":[]},"palettes":[{"name":"Default Palette","value":{"colors":{"b4fbe":{"val":"rgb(241, 99, 52)"}},"gradients":[]},"original":{"colors":{"b4fbe":{"val":"rgb(19, 114, 211)","hsl":{"h":210,"s":0.83,"l":0.45}}},"gradients":[]}}]}__CONFIG_colors_palette__
Previous Article
__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"b4fbe":{"name":"Main Accent","parent":-1}},"gradients":[]},"palettes":[{"name":"Default Palette","value":{"colors":{"b4fbe":{"val":"rgb(241, 99, 52)"}},"gradients":[]},"original":{"colors":{"b4fbe":{"val":"rgb(19, 114, 211)","hsl":{"h":210,"s":0.83,"l":0.45}}},"gradients":[]}}]}__CONFIG_colors_palette__
Next Article
Buy the Book Teradata Query Performance Tuning

DWH Pro Admin

Roland Wenzlofsky is an experienced freelance Teradata Consultant & Performance Trainer. Born in Austria's capital Vienna, he is building and tuning some of the largest Teradata Data Warehouses in the European financial and telecommunication sectors for more than 20 years. He has played all the roles of developer, designer, business analyst, and project manager. Therefore, he knows like no other the problems and obstacles that make many data warehouse projects fail and all the tricks and tips that will help you succeed.

  • Avatar
    Paul Sinclair says:

    While it is true there can only be WITH clause at the outermost level of a query in Teradata (and ANSI SQL), the WITH clause can specify multiple derived tables separated by commas and the WITH clause can be used in subqueries of a query.

    • Avatar
      Paul Sinclair says:

      correction: only be WITH –> only be one WITH

  • Avatar
    Aleksey Svitin says:

    Hi

    Thank you for your articles.

    I would like to put attention what,
    in spite of being legal,
    “CREATE VOLATILE TABLE AS [query|table] WITH DATA”
    statement is a very bad practice.

    Data dictionary table is locked while such query is executed (which could be quite long) and it could lead to many locks or deadlocks in the system (which is bad for performance and concurrency).

    It’s also true for regular tables. So I would never recommend having CREATE TABLE AS [query|table] statements in production system (however, it’s quite useful for Development or testing purpose).

    For production, our best practices is to always create volatile tables by mention all columns explicitly.

    Hope, it will be helpful.

    With best regards,
    Aleksei Svitin.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    You might also like

    >