Upload
leonel-abreu
View
67
Download
2
Embed Size (px)
Citation preview
SQLDay 2017
A time travel with temporal tables
Leonel Abreu
Email: [email protected] - Twitter: @leonel_abreu
LinkedIn: https://www.linkedin.com/in/leonel-abreu/
SQLDay 2017
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
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
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
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
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
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?
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
How does it work?
SQLDay 2017
Temporal table (actual data)
Insert / Bulk Insert
* Old versions
Update */ Delete *
History Table
How does it work?
SQLDay 2017
Temporal table (actual data)
Temporal Queries *(Time travel,etc.)
History Table
Regular queries (current data)
* Include Historical Version
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
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
SQLDay 2017
demo
SQLDay 2017
Q & A
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
SQLDay 2017