17
SQLDay 2017 A time travel with temporal tables Leonel Abreu Email: [email protected] - Twitter: @ leonel_abreu LinkedIn: https://www.linkedin.com/in/leonel-abreu/

A time Travel with temporal tables

Embed Size (px)

Citation preview

Page 1: A time Travel with temporal tables

SQLDay 2017

A time travel with temporal tables

Leonel Abreu

Email: [email protected] - Twitter: @leonel_abreu

LinkedIn: https://www.linkedin.com/in/leonel-abreu/

Page 2: A time Travel with temporal tables

SQLDay 2017

Page 3: A time Travel with temporal tables

SQLDay 2017

Agenda

• What’s a Temporal Table?

– Advantages and how to use it

• It’s useful for

– Auditing efects

– Disaster Recovery

– Trending Analytics

• A Short “How to”

– Scenario and demo

Page 4: A time Travel with temporal tables

What is a temporal table?

Also known as System Versioned

Because keep changes

Just two extra columns

To define periods

Finally a second table

Which contains the history

Page 5: A time Travel with temporal tables

Advantages

Security

Is not possible to modify the historical data

Easy to maintain

Additional coding to insert/update/delete data is not required

Can be used

For new tablesFor existing tables

Page 6: A time Travel with temporal tables

Why temporal tables?

Auditing

All data changes and forensics if necessary

Time Travel

State of data as of any time in the past

Recovering

From accidental data changes, as undesired DELETE operations

Page 7: A time Travel with temporal tables

Why temporal tables?

Calculate

Trends over time

Also BI

Can be used to do SCD

Schema included

If the table structure is changed, it is also recorded

Page 8: A time Travel with temporal tables

SQLDay 2017

• 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 easy– No change in programming model

– New Insights

Time Travel Data Audit

Slowly Changing Dimensions

Repair record-level corruptions

Why temporal tables?

Page 9: A time Travel with temporal tables

How to start?

No change in programming model New Insights

INSERT / BULK INSERT

UPDATE

DELETE

MERGE

DML SELECT * FROM temporal

Querying

CREATE temporal TABLE PERIOD FOR SYSTEM_TIME…

ALTER regular_table TABLE ADD PERIOD…

DDL

FOR SYSTEM_TIME

AS OF

FROM..TO

BETWEEN..AND

CONTAINED IN

Temporal Querying

Page 10: A time Travel with temporal tables

How does it work?

SQLDay 2017

Temporal table (actual data)

Insert / Bulk Insert

* Old versions

Update */ Delete *

History Table

Page 11: A time Travel with temporal tables

How does it work?

SQLDay 2017

Temporal table (actual data)

Temporal Queries *(Time travel,etc.)

History Table

Regular queries (current data)

* Include Historical Version

Page 12: A time Travel with temporal tables

Getting insights from temporal – AS OF

SQLDay 2017

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'

A001

A002

A003

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

Page 13: A time Travel with temporal tables

SQLDay 2017

SELECT * FROM Department FOR SYSTEM_TIMEAS OF '2010.01.01' Facts:

1. History is much bigger than actual data

2. 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

Page 14: A time Travel with temporal tables

SQLDay 2017

demo

Page 15: A time Travel with temporal tables

SQLDay 2017

Q & A

Page 16: A time Travel with temporal tables

Further Information

SQLDay 2017

• Querying Data in a System-Versioned Temporal Table

– https://docs.microsoft.com/en-us/sql/relational-databases/tables/querying-data-in-a-system-versioned-temporal-table

• Modifying Data in a System-Versioned Temporal Table

– https://docs.microsoft.com/en-us/sql/relational-databases/tables/modifying-data-in-a-system-versioned-temporal-table

• Changing the Schema of a System-Versioned Temporal Table

– https://docs.microsoft.com/en-us/sql/relational-databases/tables/changing-the-schema-of-a-system-versioned-temporal-table

Page 17: A time Travel with temporal tables

SQLDay 2017