A time travel with temporal tables Leonel Abreu – labreu@seidor.es http://www.geekswithblogs.net/leabreu #516 | Bucharest 2016
Agenda What’s a Temporal Table? It’s useful for A Short “How to” Advantages and how to use it It’s useful for Auditing efects Disaster Recovery Trending Analytics A Short “How to” Scenario and demo
What is a temporal table? Also known as System Versioned Because keep changes Just two extra columns To define periods Finally a second table Which contains the history A system-versioned temporal table is a new type of user table in SQL Server 2016, designed to keep a full history of data changes and allow easy point in time analysis. This type of temporal table is referred to as a system-versioned temporal table because the period of validity for each row is managed by the system (i.e. database engine). Every temporal table has two explicitly defined columns, each with a datetime2 data type. These columns are referred to as period columns. These period columns are used exclusively by the system to record period of validity for each row whenever a row is modified. In addition to these period columns, a temporal table also contains a reference to another table with a mirrored schema. The system uses this table to automatically store the previous version of the row each time a row in the temporal table gets updated or deleted. This additional table is referred to as the history table, while the main table that stores current (actual) row versions is referred to as the current table or simply as the temporal table. During temporal table creation users can specify existing history table (must be schema compliant) or let system create default history table. Source: https://msdn.microsoft.com/en-us/library/dn935015.aspx
How does it work? Source: https://msdn.microsoft.com/en-us/library/dn935015.aspx
How does it work? Source: https://msdn.microsoft.com/en-us/library/dn935015.aspx
Advantages Security Easy to maintain Can be used Is not possible to modify the historical data Easy to maintain Additional coding to insert/update/delete data is not required Can be used For new tables For existing tables
Why temporal tables? Auditing Time Travel Recovering All data changes and forensics if necessary Time Travel State of data as of any time in the past Recovering From accidental data changes, as undesired DELETE operations
Why temporal tables? Calculate Also BI Schema included Trends over time Also BI Can be used to do SCD Schema included If the table structure is changed, it is also recorded
Why temporal tables? Real data sources are dynamic 6/26/2018 3:05 PM Why temporal tables? Real data sources are dynamic Historical data may be critical to business success Traditional databases fail to provide required insights Workarounds are… Complex, expensive, limited, inflexible, inefficient SQL Server 2016 makes life easy No change in programming model New Insights © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Why temporal tables? Azure SQL Database Facts: Solution: Microsoft Ignite 2015 6/26/2018 3:05 PM Why temporal tables? Facts: History is much bigger than actual data Retained between 3 and 10 years “Warm”: up to a few weeks/months “Cold”: rarely queried SELECT * FROM Department FOR SYSTEM_TIME AS OF '2010.01.01' Azure SQL Database Solution: history as a stretch table: PeriodEnd < “Now - 6 months” © 2015 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
How to implement it? Source: https://msdn.microsoft.com/en-us/library/dn935015.aspx
How to implement it?
Query Clauses AS OF FROM TO SysStartTime SysStartTime <= date_time SysEndTime > date_time FROM TO SysStartTime < end_date_time SysEndTime > start_date_time
Query Clauses BETWEEN AND CONTAINED IN ALL SysStartTime <= end_date_time SysEndTime > start_date_time CONTAINED IN SysStartTime >= end_date_time SysEndTime <= start_date_time ALL Returns the union between the current and history table
Demo
Q & A
La revedere
Interesting resources Getting Started with System-Versioned Tables https://msdn.microsoft.com/en-us/library/mt604462.aspx Temporal Tables https://msdn.microsoft.com/en-us/library/dn935015.aspx Changing the Schema of a System-Versioned Temporal Table https://msdn.microsoft.com/en-us/library/mt591016.aspx Querying Data in a System-Versioned Temporal Table https://msdn.microsoft.com/en-us/library/mt591018.aspx