Presentation is loading. Please wait.

Presentation is loading. Please wait.

It’s About Time : Temporal Table Support in SQL Server 2016/2017

Similar presentations


Presentation on theme: "It’s About Time : Temporal Table Support in SQL Server 2016/2017"— Presentation transcript:

1 It’s About Time : Temporal Table Support in SQL Server 2016/2017
Vern Rabe RabeData, LLC It’s About Time : Temporal Table Support in SQL Server 2016/2017

2 Please Support Our Sponsors

3 Orange County User Group
Local User Groups Orange County User Group 2rd Thursday of each month bigpass.pass.org Los Angeles User Group San Diego User Group 3rd Thursday of each odd month 1st & 3rd Thursday of each month meetup.com/sdsqlug sqlla.pass.org meetup.com/sdsqlbig Malibu User Group Los Angeles - Korean 3rd Wednesday of each month Every Other Tuesday sqlmalibu.pass.org sqlangeles.pass.org SQLSaturday Los Angeles June 9th SQLSaturday San Diego September 15th

4 Discount Code: SSDISODNS
SQL Summit Annual International Conference November 6 -9 | Seattle, WA 2 Days of Pre-Cons 200+ sessions over 3 days Over 5,000 SQL Professionals Evening Networking Activities Discount Code: SSDISODNS

5 Vern Rabe Portland, OR independent consultant, trainer MCTA, MCDBA, MCSE, MCITP Co-VP Oregon PASS chapter Working with/focusing on SQL Server since 1993, starting with SQL Server 4.21a My first PC had a 10MB hard drive

6 Vern Rabe

7 What We’ll Cover What are we trying to solve What is a temporal table Creating and altering SELECT enhancements SQL Server 2016 temporal table limitations What’s new in SQL Server 2017

8 Use Cases Repeatable reports Changing validity periods Who made that change? (auditing) Monitoring business trends Repairing row-level corruption(not a replacement to BACKUP/RESTORE) Slowly Changing Dimension?

9 Definitions Temporal tables hold current and past versions of data
Defined in ISO SQL:2011 What’s a timestamp? Instant, Interval (duration), Period (anchored duration) System Period Application Period Bi-Temporal Closed vs. Open ended Periods

10 Temporal Tables vs. CDC (Change Data Capture)
CDC can be at column level CDC has less of a performance hit (Tlog) CDC is eventually current CDC relies on Agent Reliability CDC designed for short term use, e.g., for ETL incremental loads Temporal Tables designed to be long term, for complete row history Temporal Tables easy to query

11 SQL Server Temporal Table
Base (current) table with attached history table Two datetime2 columns define System Period UTC based US – “CUT” (Coordinated Universal Time) French – “TUC” (Temps Universel Coordonné) System maintained INSERTs – Base table gets System Period columns set to “Now” and No History table activity UPDATEs – Base table row is copied to History table, terminated to “Now”. Base table row set to start “Now” DELETEs –Base table row is moved to History, terminated to “Now” Querying additions to T-SQL

12 Implementing in SQL Server
CREATE/ALTER TABLE Two NOT NULL datetime2 columns to define period (any precision) GENERATED ALWAYS AS ROW START GENERATED ALWAYS AS ROW END Optional HIDDEN PERIOD FOR SYSTEM_TIME for these two columns (closed-open) Table option SYSTEM_VERSIONING ON Optionally specify history table name – schemas must match exactly ALTER can apply to existing non-temporal table (migrating history)

13 Syntax Basic CREATE • Specifying history table name • Specifying history table name for existing table – consistency check • ALTER existing base and history table •

14 Time Traveling Using T-SQL
FOR SYSTEM_TIME AS OF <timestamp> • FROM <start_ timestamp> TO <end_ timestamp> • Closed-open BETWEEN <start_ timestamp> AND <end_timestamp> Closed-closed CONTAINED IN (<start_ timestamp>, <end_ timestamp>) Fully contained, closed-closed Not ISO ALL • Recovering from accidental DELETE/UPDATE •

15 Metadata sys.tables sys.columns sys.periods •
temporal_type: 1=history, 2=current temporal_type_desc: HISTORY_TABLE (1) SYSTEM_VERSIONED_TEMPORAL_TABLE (2) history_table_id sys.columns generated_always_type: 1=start, 2=end generated_always_desc: AS_ROW_START AS_ROW_END NOT_APPLICABLE sys.periods • OBJECTPROPERTY & OBJECTPROPERTYEX TableTemporalType: 1=history, 2=current COLUMNPROPERTY GeneratedAlwaysType IsHidden

16 SQL Server 2016 Limitations
Base table must have PK History table cannot have PK History table must be in same database as current table Querying temporal tables over Linked Server not allowed No data modification directly on History tables No INSERT/UPDATE directly on SYSTEM_TIME columns

17 SQL Server 2016 Limitations (cont.)
Cannot be FILETABLE, cannot have FILESTREAM No constraints on history table (except DEFAULT, which is ineffective) In-Memory OLTP “mostly” implemented Truncate table not allowed No INSTEAD OF triggers No triggers on history table No history table chaining Manual changes to system clock can cause failures

18 New Features in SQL Server 2017
Limit life of history table contents System-versioned temporal tables now support CASCADE DELETE and CASCADE UPDATE

19 Summary Several uses Repeatable reports Trend analysis Data audit Repair row-level data corruption Base + History Table System Period (not Application Period) Fully system maintained Easy to create, easy to query

20 Resources SQL:2011 temporal summary
Temporal Data and the Relational Model C.J.Date Hugh Darwen Nikos A. Lorentzos Developing Time-Oriented Database Applications in SQL Richard T. Snodgrass

21 Thanks. Questions? Vern Rabe @VernRabe


Download ppt "It’s About Time : Temporal Table Support in SQL Server 2016/2017"

Similar presentations


Ads by Google