back to the future - temporal table in sql server 2016

16
Back to the future – Temporal Tables in SQL Server 2016 Stéphane Fréchette Thursday September 17, 2015

Upload: stephane-frechette

Post on 11-Apr-2017

3.925 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Back to the future - Temporal Table in SQL Server 2016

Back to the future – Temporal Tables inSQL Server 2016

Stéphane FréchetteThursday September 17, 2015

Page 2: Back to the future - Temporal Table in SQL Server 2016

My name is Stéphane Fréchette

SQL Server MVP | Data & Business Intelligence Solutions Architect | Consultant | Speaker | Big Data | NoSQL | Data Science. Drums, good food and fine wine.

I have a passion for architecting, designing and building solutions that matter.

Twitter: @sfrechetteLinkedIn: ca.linkedin.com/stephanefrechetteBlog: stephanefrechette.comEmail: [email protected]

SQLSaturday Boston #364

Who Am I?

Page 3: Back to the future - Temporal Table in SQL Server 2016

TemporalQuery back in time

Page 4: Back to the future - Temporal Table in SQL Server 2016

A temporal table is a table for which a PERIOD definition exists and which contains system columns with a datatype of datetime2 into which the period of validity is recorded by the system, and which has an associated history table into which the system records all prior versions of each record with their period of validity. With a temporal table, the value of each record at any point in time can be determined, rather than just the current value of each record. A temporal table is also referred to as a system-versioned table

What is a Temporal Table?

Page 5: Back to the future - Temporal Table in SQL Server 2016

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 easyNo change in programming model

New Insights

Why Temporal

Time Travel Data Audit

Slowly Changing Dimensions

Repair record-level corruptions

Page 6: Back to the future - Temporal Table in SQL Server 2016

No change in programming model

New Insights

INSERT / BULK INSERT

UPDATE

DELETE

MERGE

DML SELECT * FROM temporal

Querying

How to start with temporal

CREATE temporal TABLE PERIOD FOR SYSTEM_TIME…

ALTER regular_table TABLE ADD PERIOD…

DDL

FOR SYSTEM_TIMEAS OF FROM..TOBETWEEN..ANDCONTAINED IN

Temporal Querying

Page 7: Back to the future - Temporal Table in SQL Server 2016

Temporal table (actual data)

Insert / Bulk Insert

* Old versions

Update */ Delete *

How system-time works?History Table

Page 8: Back to the future - Temporal Table in SQL Server 2016

Temporal table (actual data)

Temporal Queries * (Time travel,etc.)

How system-time works?History Table

Regular queries (current data)

* Include Historical Version

Page 9: Back to the future - Temporal Table in SQL Server 2016

Querying Temporal DataExpression Qualifying Rows

AS OF <date_time> SysStartTime < = date_time AND SysEndTime > date_time

FROM <start_date_time> TO <end_date_time>

SysStartTime < end_date_time AND SysEndTime > start_date_time

BETWEEN <start_date_time> AND <end_date_time>

SysStartTime < = end_date_time AND SysEndTime > start_date_time

CONTAINED IN (<start_date_time>, <end_date_time>

SysStartTime > = start_date_time AND SysEndTime < = end_date_time

Page 10: Back to the future - Temporal Table in SQL Server 2016

DepNum DepName MngrID From To

A001 Marketing 5 2005 2008

A002 Sales 2 2005 2007

A003 Consulting 6 2005 2006

A003 Consulting 10 2009 2012

DepNum DepName

MngrID

A001 Marketing 5

A001 Marketing 6

A002 Sales 2

A002 Sales 5

A003 Consulting 6

A003 Consulting 10

DepNum DepName MngrID From To

A001 Marketing 6 2008 ∞A002 Sales 5 2007 ∞

A001

A002

A003

period of validity current time

∞∞

Department (current)

Department (history)

Department (current + history)

2005 2015

SELECT * FROM DepartmentSELECT * FROM Department FOR SYSTEM_TIMEBETWEEN '2006.01.01' AND '2007.01.01'

SELECT * FROM Department FOR SYSTEM_TIMECONTAINED IN ('2007.01.01', '2009.01.01')SELECT * FROM Department FOR SYSTEM_TIME AS OF '2006.01.01'

Getting insights from temporal

A001

A002

A003

“Get actual row versions”AS OFBETWEEN..ANDCONTAINED IN

Page 11: Back to the future - Temporal Table in SQL Server 2016

Provides correct information about stored facts at any point in time, or between 2 points in time. There are two orthogonal sets of scenarios with regards to temporal data:

System(transaction)-time Application-time

SELECT * FROM Person.BusinessEntityContactFOR SYSTEM_TIME BETWEEN @Start AND @EndWHERE ContactTypeID = 17

Performance

Temporal database support - BETWEEN

Page 12: Back to the future - Temporal Table in SQL Server 2016

• A temporal table must have a primary key defined• History table cannot have constraints; primary key, foreign key, table or column

constraints• INSERT and UPDATE statements cannot reference the SYSTEM_TIME period columns• TRUNCATE TABLE is not supported while SYSTEM_VERSIONING is ON• Direct modification of the data in a history table is not permitted• INSTEAD OF triggers not permitted on current and history table, AFTER triggers

permitted only on current table• REPLICATION usage is limited, some objects/properties are not replicated

Limitations…

Page 13: Back to the future - Temporal Table in SQL Server 2016

SELECT * FROM Department FOR SYSTEM_TIME AS OF '2010.01.01'

Facts:1. History is much bigger than actual

data2. Retained between 3 and 10 years

3. “Warm”: up to a few weeks/months

4. “Cold”: rarely queried

Solution:History as a stretch table:

PeriodEnd < “Now - 6 months”

Azure SQL Database

Business Scenario…

Page 15: Back to the future - Temporal Table in SQL Server 2016

What Questions Do You Have?

Page 16: Back to the future - Temporal Table in SQL Server 2016

Thank YouFor attending this session