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 what to have the result that was returned when the time is 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 so simple and anyone, even on *current* applications, can benefit from it, simplifying solution architecture and saving time (and money) on maintenance an reporting.
1. SQL SERVER 2016 TEMPORAL
TABLES
Davide Mauri
www.davidemauri.it | info@davidemauri.it
@mauridb
2. DAVIDE MAURI
Microsoft SQL Server Data Platform MVP since 2006
Works with SQL Server from 6.5, on BI from 2003
Specialized in DataArchitectures, Database Design, PerformanceTuning, High-
Performance DataWarehousing, BI, Big Data,Azure Data Platform
President of UGISS (Italian SQL Server UG) for 10 years
Regular Speaker @ SQL Server events
E-mail: info@davidemauri.it
Twitter: @mauridb
Blog: http://sqlblog.com/blogs/davide_mauri/default.aspx
3. TEMPORALTABLES
Automatically keeps track of changed data
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 “as-of-time” queries
Transparent to existing applications (if needed)
Integrated with existing features (eg: Partitioning, Hekaton)
Integrated with new features (eg: Stretch Database)
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)
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
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
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
9. 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
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
10. TEMPORALTABLES
Temporal queries with other operators than “AS OF” can be tricky
Especially if you’re joining temporal tables
Rows and 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
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