In Teradata, we can create several kinds of Tables ; one of them is VOLATILE Tables. Volatile tables are session-specific tables which stores intermediate data and once the session is over, all the data and table definition is removed. Volatile Tables use SPOOL SPACE. In Teradata, use keyword “VOLATILE” in CREATE statement to create a volatile table.
Teradata VOLATILE syntax
Options/Settings for Volatile Tables:
If Volatile table is created with LOG option, then it will use Transient Journal to save “BEFORE IMAGES” of all the transactions. Since the data is intermediate and can be generated again in case of failures I strongly recommend to go ahead with NO LOG option. This is one simple thing you can do to improve performance of volatile tables. Default is LOG option.
ON COMMIT [PRESERVE/DELETE] ROWS:
This is very important option and most of the time you will be using PRESERVE option. If PRESERVE option is kept then Volatile Table will store the data after each transaction however if the DELETE option is kept then it will lose the data once the transaction is completed. Default is “ON COMMIT DELETE ROWS” hence by default volatile table retains data at transaction level and not session level. So you must make it “ON COMMIT PRESERVE ROWS”
Options NOT available for VOLATILE Tables:
- Permanent Journals
- Foreign Key
- Compression at Column Level
- Default Values
- Name of Indexes
Volatile Table Example
Once the session is over, all the data and table definition is lost and is never saved in Data Dictionary. Volatile Tables used Spool Space and once session is over , the occupied Spool space is again available for some other transactions.
VOLATILE table with DATA
In most of the ETL use-cases you will create a volatile table from another existing table. If you are creating a table with another table and you want to get records then you have to use “WITH DATA” clause in volatile table CREATE statement. Also if you want to specify different primary index then you can mention it after “with data” clause.
To check all the volatile table created in current session use below command:
HELP VOLATILE TABLE;
Teradata Volatile Table vs Temporary Table
- Both the Volatile & Temporary table retains session-specific data only. Once session completes data is removed from table.
- Global Temporary table retains table definition permanently. However for volatile DDL is removed too. So you can use Temporary table in another session without the need to create it all the time.
- Volatile table use SPOOL space where as Temporary table use TEMPORARY space (basically un-used Permanent Space).
- In earlier version , collect stats on Volatile was not supported but now it is for both kind of tables. For Temporary table, use keyword “TEMPORARY” before tablename in collect stats.