Professional Documents
Culture Documents
0204 Modeling Time Travel Example PDF
0204 Modeling Time Travel Example PDF
TABLE OF CONTENT
1 INTRODUCTION .......................................................................................................................... 3
1.1 Create Tables .............................................................................................................................. 3
1.2 Check Table is a History Table .................................................................................................. 3
1.3 Turn Off Auto Commit ................................................................................................................ 3
1.4 Get the Current Commit ID ......................................................................................................... 4
1.5 Add Initial Table Data ................................................................................................................. 4
1.6 Execute a Join Query ................................................................................................................. 5
1.7 Update Some Records................................................................................................................ 5
1.8 Execute the Query Again............................................................................................................ 5
1.9 Add More Records ...................................................................................................................... 5
1.10 Execute the Query Again............................................................................................................ 6
1.11 Modify Some More Data ............................................................................................................. 6
1.12 Execute the Query Again to Verify Update ................................................................................ 6
1.13 Get the List of Commit IDs ......................................................................................................... 6
2 VERIFYING HISTORY.................................................................................................................. 7
2.1 Execute a Modified Query to View History ................................................................................ 7
3 TIME TRAVELLING ..................................................................................................................... 8
3.1 Execute a Join Query With COMMIT ID ..................................................................................... 8
3.2 SET HISTORY SESSION TO COMMIT ID .................................................................................... 9
3.3 SET HISTORY SESSION TO UTCTIMESTAMP ........................................................................... 9
4 BEHAVIOUR WHEN DELETING RECORDS ..............................................................................10
4.1 Delete a Record .........................................................................................................................10
4.2 Execute the Query Again...........................................................................................................10
4.3 Execute a Modified Query to View History ...............................................................................10
4.4 Execute the Query With AS OF COMMIT ID..............................................................................10
5 HISTORY TABLE MEMORY USAGE ..........................................................................................11
6 PERMANENTLY REMOVING RECORDS FROM A HISTORY TABLE .......................................12
6.1 Query the Contents of the History Table ..................................................................................12
6.2 Verify Current TRANSACTION_HISTORY COMMIT_IDs...........................................................12
6.3 Remove All History Prior to '2011-11-09 05:00:00’ ...................................................................12
6.4 Clean-out History.......................................................................................................................13
6.5 Redisplay the History Table Contents ......................................................................................13
2
SAP HANA – TIME TRAVEL EXAMPLE
1 INTRODUCTION
The SAP In-Memory Computing Engine (NewDB) provides support for History Tables. In such a History
Table, updates to and deletion of a record, results in a new version of the existing record, with the original
record being invalidated.
This document demonstrates how to create, update and query History Tables using two sample tables, some
simple sample data, and sample code showing the results of each step.
A History Table can be created as a History Table, as shown for the MODEL table in the code example, or if
it is subsequently determined that a standard table requires history, then the table can be modified as shown
for the COLOUR table.
Using the SAP HANA Studio, select the Properties tab for the SQL Editor and turn the Auto Commit from On
to Off. This is required in order for time travel to work correctly, and limits the number of Commit IDs that
need to be worked with.
After changing the setting it is important to click elsewhere within the tab in order for the property change to
be applied.
Because Auto Commit has now been turned off, you will need to issue the COMMIT command after making
changes to the data in the tables.
(If you do not change this setting, then an error message to this effect will be displayed when time travel
commands are executed)
3
SAP HANA – TIME TRAVEL EXAMPLE
This Commit ID is the starting ID before any data is entered into the tables. Obtaining this first may make
keeping track of the relevant timestamps and commit IDs easier.
Since Auto Commit has been turned off, the COMMIT command is necessary to ensure that the data is
inserted into the table.
The MERGE DELTA INDEX is required in order to merge the newly added records into the main table, from
the delta table. Data in delta tables is not compressed, so take up more room than is otherwise necessary.
4
SAP HANA – TIME TRAVEL EXAMPLE
This query provides a simple view of the data currently in the tables. This query will be used throughout this
example to demonstrate the current as well as historic view of the table data.
By keeping track of the Commit ID it will make it easier to check that the History and Time Travel is working
correctly and verifiably.
The record update replaced some of the VALUE fields in the table, resulting in a reduced record set for the
join.
5
SAP HANA – TIME TRAVEL EXAMPLE
The new records should have corresponding entries in the other table which should now appear in the query.
As can be seen, there are now more records returned in the dataset.
6
SAP HANA – TIME TRAVEL EXAMPLE
Restricting the query by using the Commit ID means that a smaller dataset is returned. Otherwise it would
be necessary to scan the COMMIT_TIME column to locate the relevant time.
2 VERIFYING HISTORY
The hidden columns “$validfrom$” and “$validto$” (case sensitive) contain the Commit ID when the record
was added – “$validfrom$” – and superceded or deleted - “$validto$”.
Records without an entry in either column have not yet been committed. Records that only have an entry in
the “$validfrom$” column are current records and will appear in normal queries (as above) and are stored in
the “Main” or “Main Delta” tables, while records that have entries in both columns will be excluded from
7
SAP HANA – TIME TRAVEL EXAMPLE
normal queries and have been moved to the “History” or “History Delta” tables. (Records in the “Delta”
tables have not yet been merged into the main tables, and are therefore not compressed.)
3 TIME TRAVELLING
There are a number of ways of “Time Travelling” which will be shown. As the data displayed using the
various methods will be identical, the first method will look at all possible “Time Travel” scenarios and the
datasets returned each time, whereas subsequent methods will only look at one or two particular examples.
8
SAP HANA – TIME TRAVEL EXAMPLE
Executing the query using each of the LAST_COMMIT_IDs obtained after records were added or updated
shows the same datasets that were observed when the queries were executed immediately after the original
changes were made. Using the Commit ID requires knowing what it is. Unless the value of the Commit ID is
stored after the data has been committed, then the TRANSACTION_HISTORY table would need to be
queried to locate the appropriate Commit ID value.
Running a query “AS AT COMMIT ID” will return any matching data that was relevant at the time the
COMMIT associated with the Commit ID was executed. For HISTORY tables, that will be data that was
current at the time of the COMMIT. For non-HISTORY tables, it will return data current as of the time of
running the query. (As this may not be the value of the data that was current at the time of the specified
COMMIT ID, care must be taken when using “Time Travel” with both HISTORY and non-HISTORY” tables in
the same query.)
By setting the HISTORY SESSION to a particular Commit ID, all subsequent queries will return data current
as of that Commit ID, until the command is cancelled by setting the history session to another value, or
cancelling it by setting it to NOW.
9
SAP HANA – TIME TRAVEL EXAMPLE
When using a timestamp to set the history point-in-time, the time need not be exact, but should fall after the
timestamp for the Commit ID of interest, but before the timestamp following it.
The recently deleted record, number 7, shows with a Commit ID in both the “$validfrom$” and “$validto$”
columns, indicating that the record is no longer in the Main table, but has been moved to the History table,
where it is only accessed during “Time Travel” type queries.
10
SAP HANA – TIME TRAVEL EXAMPLE
Running the query using a Commit ID for when the record was still valid, returns the record in the dataset, as
expected.
SELECT MEMORY_SIZE_IN_TOTAL,
MEMORY_SIZE_IN_MAIN,
MEMORY_SIZE_IN_DELTA,
MEMORY_SIZE_IN_HISTORY_MAIN,
MEMORY_SIZE_IN_HISTORY_DELTA,
RAW_RECORD_COUNT_IN_MAIN,
RAW_RECORD_COUNT_IN_DELTA,
RAW_RECORD_COUNT_IN_HISTORY_MAIN,
RAW_RECORD_COUNT_IN_HISTORY_DELTA
FROM SYS.M_CS_TABLES
WHERE SCHEMA_NAME = 'SYSTEM' AND TABLE_NAME = 'COLOUR';
11
SAP HANA – TIME TRAVEL EXAMPLE
Referring to the figure in 4.3, there are 4 records that don’t have an entry in the “$validto$” column, which
indicates that they are current records, and there are 3 records that have an entry in the “$validto$” column,
which indicates that they are no longer current records and are part of the history for this table.
Running the query to determine the memory use for the table confirms that there are 4 records in Main and 3
records in History. The memory used by each of the 4 parts of the table are also shown.
12
SAP HANA – TIME TRAVEL EXAMPLE
COMMIT;
13
www.sap.com
All other product and service names mentioned are the trademarks of
their respective companies. Data contained in this document serves
informational purposes only. National product specifications may vary.