Have you ever dreamed about traveling through time? Now you can with temporal tables in SQL Server! Find out what temporal tables are and how they are completely different from tempdb, temp tables, and other temporary things you might already be familiar with. Join us on our journey from discovery to practical uses as we look at ways you can start using them to help with issues you are facing. Presented at the November 2017 Salt Lake City SQL Server User Group.
2. Traveling Through Time
• JamesWebb SpaceTelescope
• Scheduled Launch in Spring 2019
• Designed to study light from as far
back as 13.5 billion years ago
• TemporalTables in SQL Server
provide a similar look back in time
3. Introductions: Greg McMurray
• Worked in Aerospace, Branding & Marketing, Energy, Healthcare, Software
• Currently Senior Software Engineer atWECC
• Aritus Computer Services, L.L.C. Owner for 19 years
• Active in many local user groups
• Find me online
• @goyuix
• https://www.linkedin.com/in/goyuix
• https://stackoverflow.com/cv/goyuix - top 3% of users
4. Introductions: WECC
• Western Electricity Coordinating Council
• Ensure the reliability & security of the western interconnection
• Approved Regional Entity by Federal Energy Regulatory Commission
• Create, monitor & enforce reliability standards
• Publish studies, models and independent perspective
• Covers 14 western states, 2 Canadian provinces and Baja Mexico
• We are hiring! https://www.wecc.biz/careers
5. Agenda
• What are temporal tables
• Why &When to use them
• How to build and query
• Demos
• Limitations
• Additional Resources
• Q & A
7. What Are Temporal Tables
• Special kind of table used to track changes over time
• Called System-Versioned because the system handles them
• Rule ofTwo: 2 tables and 2 new columns
• Two period columns of type datetime2 (SysStartTime and SysEndTime)
• History table with same column definitions
• Availability: All versions of SQL Server starting with 2016
• Additional improvements in 2017 as well
8. Sample DDL for Creating Temporal Table
CREATE TABLE dbo.Emp (
[EmployeeID] int NOT NULL PRIMARY KEY CLUSTERED
, [Name] nvarchar(100) NOT NULL
, [AnnualSalary] decimal (10,2) NOT NULL
, [StartTime] datetime2 (2) GENERATED ALWAYS AS ROW START
, [EndTime] datetime2 (2) GENERATED ALWAYS AS ROW END
, PERIOD FOR SYSTEM_TIME (StartTime, EndTime)
) WITH
(SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.EmpHistory)
)
10. Observations
• Table shows up with a clock icon overlay
• New in 2017 – Start and End columns can be HIDDEN
• History table doesn't show up in schema browser
• Shows up once you expand the source table
• Schema name is required for history table definition
• This also means you can store it in a different schema
• Why would you want to do that? Advantages?
• What happens if you try to drop a table that is system-versioned?
12. What happens with DML statements?
• INSERT – StartTime set to current UTC time, EndTime set to max
value (9999-12-31)
• UPDATE – Old row is added to history table and EndTime updated to
current UTC time. StartTime in current table is set to current UTC
time, EndTime remains at max value.
• DELETE – Old row is added to history table and EndTime updated to
current UTC time. Row is then removed from current table.
• MERGE – Follows INSERT / UPDATE / DELETE logic as specified
14. Querying Temporal Data
• So far we have just created a log
• Thinking to yourself: I could have done this with triggers!
• What happens when you add a new column?
• What would a query look like that returns for a specific time?
• This is much easier with someT-SQL syntactic sugar
• And like many fabulous nuggets, it starts with the FOR predicate
• Like FOR XML, FOR JSON, …
• SELECT * FROMTable FOR SYSTEM_TIMEALL
• Internally a UNION happens between temporal / history table
15. FOR SYSTEM_TIME Predicates
ALL Returns all matching rows from both tables
AS OF '2017-01-01' StartTime <= '2017-01-01’ AND EndTime > '2017-01-01'
BETWEEN '2017-01-01’
AND '2017-12-31'
StartTime < '2017-12-31’ AND EndTime >= '2017-01-01'
FROM '2017-01-01’
TO '2017-12-31'
StartTime <= '2017-12-31’ AND EndTime > '2017-01-01'
CONTAINED IN
('2017-01-01', '2017-01-01')
StartTime >= '2017-01-01’ AND EndTime <= '2017-12-31'
17. Managing Historical Data
• 2016 had not retention period - manage it yourself
• 2017 includes HISTORY_RETENTION_PERIOD policy
• DAYS,WEEKS, MONTHS,YEARS, INFINITE – assumes infinite if not specified
• Fun Idea: Use Stretch Database
• Can stretch the whole table or just part of it
• Another option: UseTABLE PARTITIONS
• Most exciting and user error prone: Custom Cleanup Scripts
• Remember to turn off SYSTEM_VERSIONING
18. Limitations
• Can’t directly modify the data in the history table
• Source table must have a primary key defined
• FILETABLE & FILESTREAM are not allowed because changes could happen
outside the system and they can’t guarantee versioning
• TRUNCATE isn’t supported while versioning is ON
• CASCADE (delete or update) is not allowed
• INSTEAD OF triggers are not allowed on current or history tables
• AFTER triggers are only allowed on the current table
• Limits on REPLICATION
20. Questions & Answers
• Keep the conversation alive after the meetup
• #SLCSQL hashtag
• Meetup.com has comments for the meeting
• I’d love to connect with you online
• Twitter: @goyuix
• LinkedIn: https://www.linkedin.com/in/goyuix