Keeping track of how data changed over time in a table has always been a difficult task. Both data insertion or modification and even querying is just more complex when you want to have the result that was returned at a specific point of time in the past. And even more complex when you’re not looking for a specific point in time, but a period of time. Temporal database theory and temporal operators surely can come to the rescue, but they are not a matter for the faint of heart! Luckily one of the biggest - and most requested – feature that has been added to SQL Server 2016 solves exactly this problem, allowing the creation of change audit trails, data history and point-in-time queries in such a simple what that anyone, even on *current* applications, can benefit from it, simplifying solution architecture and saving time (and money) on maintenance an reporting.
In this session we’ll see how the feature work on SQL Server 2016 and Azure SQL v12 and also what will be available in the vNext version of SQL Server.
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
SQL Server & SQL Azure Temporal Tables - V2
1. SQL SERVER & SQL AZURE
TEMPORAL TABLES
Davide Mauri
www.davidemauri.it | info@davidemauri.it
@mauridb
2. DAVIDE MAURI
Microsoft Data Platform MVP since 2006
Works with SQL Server from 6.5, on BI from 2003
Specialized in Data SolutionArchitecture, Database Design, PerformanceTuning, High-
Performance DataWarehousing, BI, Big Data
Very strong developer background
Loves community!
President of UGISS (Italian SQL Server UG) for 11Years, AppDevVG Leader since 2017
Regular Speaker @ SQL Server events
Director SW & Cloud @ Sensoria: http://www.sensoriafitness.com/
E-mail: info@davidemauri.it -Twitter: @mauridb
Blog: http://sqlblog.com/blogs/davide_mauri/default.aspx
4. THINGSTO KEEP IN MIND
Stored interval is half-open: [valid_from, valid_to)
>= valid_fromAND < valid_to
Stored interval is in UTC
5. TEMPORALTABLES
Automatically keeps track of changed data (versioning)
Along with period of validity
Available in all editions of SQL Server (from the Express!)
Available in SQL Azure
ANSI 2011 compliant
Provide native support for temporal queries
Transparent to existing applications (if needed)
Integrated with existing features (eg: Partitioning, Hekaton, ColumnStore)
Integrated with new features (eg: Stretch Database)
6. TEMPORAL QUERIES
SQL Server 2016 supports
AS OF <date_time>
FROM <start_date_time>TO <end_date_time>
BETWEEN <start_date_time> AND <end_date_time>
CONTAINED IN (<start_date_time> , <end_date_time>)
ALL
https://docs.microsoft.com/en-us/sql/relational-databases/tables/temporal-tables
http://www.ics.uci.edu/~alspaugh/cls/shr/allen.html
7. TEMPORALTABLES
Some limitations compared to “classic” tables
NoTRUNCATETABLE support
INSTEADOF triggers not supported
Temporal tables *can* be ALTERed
A few limitations:
Cannot add a computed columns
Cannot add an Identity column
Versioning can be turned on/off as we wish
8. TEMPORALTABLES
SQL 2016:There is *no* automatic cleanup of versioning
Stretch Database offer “a sort of” automatic archival (but still no cleaning!)
Tips: https://msdn.microsoft.com/library/mt637341.aspx
SQL vNext – Azure SQL:There *is* an automatic cleanup process of versioning
https://docs.microsoft.com/en-us/azure/sql-database/sql-database-temporal-tables-
retention-policy
10. TEMPORALTABLES
Temporal queries with other operators than “AS OF” can be tricky
Especially if you’re joining temporal tables
Rows are not unpacked and then packed back!
Hugh Darwen http://www.dcs.warwick.ac.uk/~hugh/
12. ADDITIONAL READS
First Look at System-VersionedTemporalTables
http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-1-creating-
tables-and-modifying-data
http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-
data-and-optimization-conside
Usage Scenarios
https://docs.microsoft.com/en-us/sql/relational-databases/tables/temporal-table-usage-
scenarios
Temporal data (as a general topic)
https://www.amazon.com/Time-Relational-Theory-Second-Management/dp/0128006315
https://www.pluralsight.com/courses/working-with-temporal-data-sql-server
13. VOTE ON CONNECT
Temporal support is good but can be improved! Help telling Microsoft you what
more:
Add SQL Server Engine Support for Interval Queries (Intersection/Overlap and other
Allen’s interval algebra relations)
https://connect.microsoft.com/SQLServer/Feedback/Details/780746
TemporalTables: Connect Item Round Up
http://sqlblog.com/blogs/adam_machanic/archive/2016/10/04/temporal-tables-connect-item-
round-up.aspx