Back to the future - Temporal Table in SQL Server 2016

Stéphane Fréchette
Stéphane FréchetteData & Business Intelligence Solutions Architect | Consultant | Big Data | NoSQL | Data Science | Data Platform MVP
Back to the future – Temporal Tables
in
SQL Server 2016
Stéphane Fréchette
Thursday September 17, 2015
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: @sfrechette
LinkedIn: ca.linkedin.com/stephanefrechette
Blog: stephanefrechette.com
Email: stephanefrechette@ukubu.com
SQLSaturday Boston #364
Who Am I?
Temporal
Query back in time
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?
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
Why Temporal
Time Travel Data Audit
Slowly Changing
Dimensions
Repair record-level
corruptions
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_TIME
AS OF
FROM..TO
BETWEEN..AND
CONTAINED IN
Temporal Querying
Temporal table (actual data)
Insert / Bulk Insert
* Old versions
Update */ Delete *
How system-time works?
History Table
Temporal table (actual data)
Temporal Queries *
(Time travel,etc.)
How system-time works?
History Table
Regular queries
(current data)
* Include Historical
Version
Querying Temporal Data
Expression 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
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_TIME
BETWEEN '2006.01.01' AND '2007.01.01'
SELECT * FROM Department FOR SYSTEM_TIME
CONTAINED 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
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.BusinessEntityContact
FOR SYSTEM_TIME BETWEEN @Start AND @End
WHERE ContactTypeID = 17
Performance
Temporal database support - BETWEEN
• 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…
SELECT * FROM Department
FOR SYSTEM_TIME
AS 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
Business Scenario…
MSDN Documentation
https://msdn.microsoft.com/en-CA/library/dn935015.aspx
Temporal in SQL Server 2016 (Video) – Channel 9
https://channel9.msdn.com/Shows/Data-Exposed/Temporal-in-SQL-Server-2016
SQL Server Pro – First Look at System-Versioned Temporal Tables…
http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-1-creating-tables-and-modifying-data
Resources
What Questions Do You Have?
Thank You
For attending this session
1 of 16

Recommended

SQL Server Workshop for Developers - Visual Studio Live! NY 2012 by
SQL Server Workshop for Developers - Visual Studio Live! NY 2012SQL Server Workshop for Developers - Visual Studio Live! NY 2012
SQL Server Workshop for Developers - Visual Studio Live! NY 2012Andrew Brust
2.6K views195 slides
Teradata a z by
Teradata a zTeradata a z
Teradata a zDhanasekar T
1.8K views243 slides
Trivadis TechEvent 2017 SQL Server 2016 Temporal Tables by Willfried Färber by
Trivadis TechEvent 2017 SQL Server 2016 Temporal Tables by Willfried FärberTrivadis TechEvent 2017 SQL Server 2016 Temporal Tables by Willfried Färber
Trivadis TechEvent 2017 SQL Server 2016 Temporal Tables by Willfried FärberTrivadis
273 views22 slides
Tuning and Optimizing U-SQL Queries (SQLPASS 2016) by
Tuning and Optimizing U-SQL Queries (SQLPASS 2016)Tuning and Optimizing U-SQL Queries (SQLPASS 2016)
Tuning and Optimizing U-SQL Queries (SQLPASS 2016)Michael Rys
4.3K views59 slides
Introduction to Teradata And How Teradata Works by
Introduction to Teradata And How Teradata WorksIntroduction to Teradata And How Teradata Works
Introduction to Teradata And How Teradata WorksBigClasses Com
4.1K views14 slides
Teradata 13.10 by
Teradata 13.10Teradata 13.10
Teradata 13.10Teradata
10.6K views43 slides

More Related Content

What's hot

Data Movement issues: Explicit SQL Pass-Through can do the trick by
Data Movement issues: Explicit SQL Pass-Through can do the trickData Movement issues: Explicit SQL Pass-Through can do the trick
Data Movement issues: Explicit SQL Pass-Through can do the trickKiran Venna
662 views37 slides
Teradata sql-tuning-top-10 by
Teradata sql-tuning-top-10Teradata sql-tuning-top-10
Teradata sql-tuning-top-10Roland Wenzlofsky
4.7K views4 slides
SQL Server 2016 new features by
SQL Server 2016 new featuresSQL Server 2016 new features
SQL Server 2016 new featuresSpanishPASSVC
634 views17 slides
U-SQL Partitioned Data and Tables (SQLBits 2016) by
U-SQL Partitioned Data and Tables (SQLBits 2016)U-SQL Partitioned Data and Tables (SQLBits 2016)
U-SQL Partitioned Data and Tables (SQLBits 2016)Michael Rys
5.7K views37 slides
Introduction to Azure Data Lake and U-SQL for SQL users (SQL Saturday 635) by
Introduction to Azure Data Lake and U-SQL for SQL users (SQL Saturday 635)Introduction to Azure Data Lake and U-SQL for SQL users (SQL Saturday 635)
Introduction to Azure Data Lake and U-SQL for SQL users (SQL Saturday 635)Michael Rys
2.5K views34 slides
Azure Data Factory Data Flows Training (Sept 2020 Update) by
Azure Data Factory Data Flows Training (Sept 2020 Update)Azure Data Factory Data Flows Training (Sept 2020 Update)
Azure Data Factory Data Flows Training (Sept 2020 Update)Mark Kromer
1.7K views71 slides

What's hot(20)

Data Movement issues: Explicit SQL Pass-Through can do the trick by Kiran Venna
Data Movement issues: Explicit SQL Pass-Through can do the trickData Movement issues: Explicit SQL Pass-Through can do the trick
Data Movement issues: Explicit SQL Pass-Through can do the trick
Kiran Venna662 views
SQL Server 2016 new features by SpanishPASSVC
SQL Server 2016 new featuresSQL Server 2016 new features
SQL Server 2016 new features
SpanishPASSVC634 views
U-SQL Partitioned Data and Tables (SQLBits 2016) by Michael Rys
U-SQL Partitioned Data and Tables (SQLBits 2016)U-SQL Partitioned Data and Tables (SQLBits 2016)
U-SQL Partitioned Data and Tables (SQLBits 2016)
Michael Rys5.7K views
Introduction to Azure Data Lake and U-SQL for SQL users (SQL Saturday 635) by Michael Rys
Introduction to Azure Data Lake and U-SQL for SQL users (SQL Saturday 635)Introduction to Azure Data Lake and U-SQL for SQL users (SQL Saturday 635)
Introduction to Azure Data Lake and U-SQL for SQL users (SQL Saturday 635)
Michael Rys2.5K views
Azure Data Factory Data Flows Training (Sept 2020 Update) by Mark Kromer
Azure Data Factory Data Flows Training (Sept 2020 Update)Azure Data Factory Data Flows Training (Sept 2020 Update)
Azure Data Factory Data Flows Training (Sept 2020 Update)
Mark Kromer1.7K views
Sql Server 2005 Business Inteligence by abercius24
Sql Server 2005 Business InteligenceSql Server 2005 Business Inteligence
Sql Server 2005 Business Inteligence
abercius24684 views
ADF Mapping Data Flows Level 300 by Mark Kromer
ADF Mapping Data Flows Level 300ADF Mapping Data Flows Level 300
ADF Mapping Data Flows Level 300
Mark Kromer357 views
Hands-On with U-SQL and Azure Data Lake Analytics (ADLA) by Jason L Brugger
Hands-On with U-SQL and Azure Data Lake Analytics (ADLA)Hands-On with U-SQL and Azure Data Lake Analytics (ADLA)
Hands-On with U-SQL and Azure Data Lake Analytics (ADLA)
Jason L Brugger7.3K views
Data ware house design by Sayed Ahmed
Data ware house designData ware house design
Data ware house design
Sayed Ahmed292 views
Working with informtiaca teradata parallel transporter by Anjaneyulu Gunti
Working with informtiaca teradata parallel transporterWorking with informtiaca teradata parallel transporter
Working with informtiaca teradata parallel transporter
Anjaneyulu Gunti10.8K views
How The Weather Company Uses Apache Spark to Serve Weather Data Fast at Low Cost by Databricks
How The Weather Company Uses Apache Spark to Serve Weather Data Fast at Low CostHow The Weather Company Uses Apache Spark to Serve Weather Data Fast at Low Cost
How The Weather Company Uses Apache Spark to Serve Weather Data Fast at Low Cost
Databricks313 views
Sql server introduction by Riteshkiit
Sql server introductionSql server introduction
Sql server introduction
Riteshkiit1.8K views
Understanding System Performance by Teradata
Understanding System PerformanceUnderstanding System Performance
Understanding System Performance
Teradata4.5K views
Change Data Feed in Delta by Databricks
Change Data Feed in DeltaChange Data Feed in Delta
Change Data Feed in Delta
Databricks1.6K views
Azure Data Factory Data Flows Training v005 by Mark Kromer
Azure Data Factory Data Flows Training v005Azure Data Factory Data Flows Training v005
Azure Data Factory Data Flows Training v005
Mark Kromer2K views
Optimizing Your Cloud Applications in RightScale by RightScale
Optimizing Your Cloud Applications in RightScaleOptimizing Your Cloud Applications in RightScale
Optimizing Your Cloud Applications in RightScale
RightScale571 views

Similar to Back to the future - Temporal Table in SQL Server 2016

Walking down the memory lane with temporal tables by
Walking down the memory lane with temporal tablesWalking down the memory lane with temporal tables
Walking down the memory lane with temporal tablesArgelo Royce Bautista
242 views11 slides
sql_server_2016_history_tables by
sql_server_2016_history_tablessql_server_2016_history_tables
sql_server_2016_history_tablesarthurjosemberg
266 views28 slides
Tracking your data across the fourth dimension by
Tracking your data across the fourth dimensionTracking your data across the fourth dimension
Tracking your data across the fourth dimensionJeremy Cook
810 views65 slides
Perfect trio : temporal tables, transparent archiving in db2 for z_os and idaa by
Perfect trio : temporal tables, transparent archiving in db2 for z_os and idaaPerfect trio : temporal tables, transparent archiving in db2 for z_os and idaa
Perfect trio : temporal tables, transparent archiving in db2 for z_os and idaaCuneyt Goksu
297 views29 slides
Webinar - MariaDB Temporal Tables: a demonstration by
Webinar - MariaDB Temporal Tables: a demonstrationWebinar - MariaDB Temporal Tables: a demonstration
Webinar - MariaDB Temporal Tables: a demonstrationFederico Razzoli
31 views32 slides
Sql 2016 - What's New by
Sql 2016 - What's NewSql 2016 - What's New
Sql 2016 - What's Newdpcobb
822 views22 slides

Similar to Back to the future - Temporal Table in SQL Server 2016(20)

Tracking your data across the fourth dimension by Jeremy Cook
Tracking your data across the fourth dimensionTracking your data across the fourth dimension
Tracking your data across the fourth dimension
Jeremy Cook810 views
Perfect trio : temporal tables, transparent archiving in db2 for z_os and idaa by Cuneyt Goksu
Perfect trio : temporal tables, transparent archiving in db2 for z_os and idaaPerfect trio : temporal tables, transparent archiving in db2 for z_os and idaa
Perfect trio : temporal tables, transparent archiving in db2 for z_os and idaa
Cuneyt Goksu297 views
Webinar - MariaDB Temporal Tables: a demonstration by Federico Razzoli
Webinar - MariaDB Temporal Tables: a demonstrationWebinar - MariaDB Temporal Tables: a demonstration
Webinar - MariaDB Temporal Tables: a demonstration
Federico Razzoli31 views
Sql 2016 - What's New by dpcobb
Sql 2016 - What's NewSql 2016 - What's New
Sql 2016 - What's New
dpcobb822 views
Tracking your data across the fourth dimension by Jeremy Cook
Tracking your data across the fourth dimensionTracking your data across the fourth dimension
Tracking your data across the fourth dimension
Jeremy Cook321 views
Time Travelling With DB2 10 For zOS by Laura Hood
Time Travelling With DB2 10 For zOSTime Travelling With DB2 10 For zOS
Time Travelling With DB2 10 For zOS
Laura Hood499 views
Track your data across the fourth dimension by Jeremy Cook
Track your data across the fourth dimensionTrack your data across the fourth dimension
Track your data across the fourth dimension
Jeremy Cook625 views
SQL Server Temporal Tables by Greg McMurray
SQL Server Temporal TablesSQL Server Temporal Tables
SQL Server Temporal Tables
Greg McMurray181 views
A time Travel with temporal tables by Leonel Abreu
A time Travel with temporal tablesA time Travel with temporal tables
A time Travel with temporal tables
Leonel Abreu187 views
Sql server 2016 new features by Ajeet Singh
Sql server 2016 new featuresSql server 2016 new features
Sql server 2016 new features
Ajeet Singh4.2K views
Temporal Tables, Transparent Archiving in DB2 for z/OS and IDAA by Cuneyt Goksu
Temporal Tables, Transparent Archiving in DB2 for z/OS and IDAATemporal Tables, Transparent Archiving in DB2 for z/OS and IDAA
Temporal Tables, Transparent Archiving in DB2 for z/OS and IDAA
Cuneyt Goksu3K views
Temporal by sunsie
TemporalTemporal
Temporal
sunsie1.9K views
Database versioning by macsolve
Database versioningDatabase versioning
Database versioning
macsolve5.8K views

More from Stéphane Fréchette

Self-Service Data Integration with Power Query - SQLSaturday #364 Boston by
Self-Service Data Integration with Power Query - SQLSaturday #364 Boston  Self-Service Data Integration with Power Query - SQLSaturday #364 Boston
Self-Service Data Integration with Power Query - SQLSaturday #364 Boston Stéphane Fréchette
1.4K views24 slides
Power BI - Bring your data together by
Power BI - Bring your data togetherPower BI - Bring your data together
Power BI - Bring your data togetherStéphane Fréchette
1.9K views28 slides
Data Analytics with R and SQL Server by
Data Analytics with R and SQL ServerData Analytics with R and SQL Server
Data Analytics with R and SQL ServerStéphane Fréchette
5.9K views30 slides
Self-Service Data Integration with Power Query by
Self-Service Data Integration with Power QuerySelf-Service Data Integration with Power Query
Self-Service Data Integration with Power QueryStéphane Fréchette
2.5K views24 slides
Introduction to Azure HDInsight by
Introduction to Azure HDInsightIntroduction to Azure HDInsight
Introduction to Azure HDInsightStéphane Fréchette
3.2K views29 slides
Le journalisme de données... par où commencer? by
Le journalisme de données... par où commencer?Le journalisme de données... par où commencer?
Le journalisme de données... par où commencer?Stéphane Fréchette
1.1K views36 slides

More from Stéphane Fréchette(18)

Self-Service Data Integration with Power Query - SQLSaturday #364 Boston by Stéphane Fréchette
Self-Service Data Integration with Power Query - SQLSaturday #364 Boston  Self-Service Data Integration with Power Query - SQLSaturday #364 Boston
Self-Service Data Integration with Power Query - SQLSaturday #364 Boston
Graph Databases for SQL Server Professionals - SQLSaturday #350 Winnipeg by Stéphane Fréchette
Graph Databases for SQL Server Professionals - SQLSaturday #350 WinnipegGraph Databases for SQL Server Professionals - SQLSaturday #350 Winnipeg
Graph Databases for SQL Server Professionals - SQLSaturday #350 Winnipeg
On the move with Big Data (Hadoop, Pig, Sqoop, SSIS...) by Stéphane Fréchette
On the move with Big Data (Hadoop, Pig, Sqoop, SSIS...)On the move with Big Data (Hadoop, Pig, Sqoop, SSIS...)
On the move with Big Data (Hadoop, Pig, Sqoop, SSIS...)
Introduction to Master Data Services in SQL Server 2012 by Stéphane Fréchette
Introduction to Master Data Services in SQL Server 2012Introduction to Master Data Services in SQL Server 2012
Introduction to Master Data Services in SQL Server 2012
Stéphane Fréchette19.3K views

Recently uploaded

ChatGPT and AI for Web Developers by
ChatGPT and AI for Web DevelopersChatGPT and AI for Web Developers
ChatGPT and AI for Web DevelopersMaximiliano Firtman
181 views82 slides
Empathic Computing: Delivering the Potential of the Metaverse by
Empathic Computing: Delivering  the Potential of the MetaverseEmpathic Computing: Delivering  the Potential of the Metaverse
Empathic Computing: Delivering the Potential of the MetaverseMark Billinghurst
470 views80 slides
Voice Logger - Telephony Integration Solution at Aegis by
Voice Logger - Telephony Integration Solution at AegisVoice Logger - Telephony Integration Solution at Aegis
Voice Logger - Telephony Integration Solution at AegisNirmal Sharma
17 views1 slide
Black and White Modern Science Presentation.pptx by
Black and White Modern Science Presentation.pptxBlack and White Modern Science Presentation.pptx
Black and White Modern Science Presentation.pptxmaryamkhalid2916
14 views21 slides
Combining Orchestration and Choreography for a Clean Architecture by
Combining Orchestration and Choreography for a Clean ArchitectureCombining Orchestration and Choreography for a Clean Architecture
Combining Orchestration and Choreography for a Clean ArchitectureThomasHeinrichs1
69 views24 slides
Data-centric AI and the convergence of data and model engineering: opportunit... by
Data-centric AI and the convergence of data and model engineering:opportunit...Data-centric AI and the convergence of data and model engineering:opportunit...
Data-centric AI and the convergence of data and model engineering: opportunit...Paolo Missier
34 views40 slides

Recently uploaded(20)

Empathic Computing: Delivering the Potential of the Metaverse by Mark Billinghurst
Empathic Computing: Delivering  the Potential of the MetaverseEmpathic Computing: Delivering  the Potential of the Metaverse
Empathic Computing: Delivering the Potential of the Metaverse
Mark Billinghurst470 views
Voice Logger - Telephony Integration Solution at Aegis by Nirmal Sharma
Voice Logger - Telephony Integration Solution at AegisVoice Logger - Telephony Integration Solution at Aegis
Voice Logger - Telephony Integration Solution at Aegis
Nirmal Sharma17 views
Black and White Modern Science Presentation.pptx by maryamkhalid2916
Black and White Modern Science Presentation.pptxBlack and White Modern Science Presentation.pptx
Black and White Modern Science Presentation.pptx
maryamkhalid291614 views
Combining Orchestration and Choreography for a Clean Architecture by ThomasHeinrichs1
Combining Orchestration and Choreography for a Clean ArchitectureCombining Orchestration and Choreography for a Clean Architecture
Combining Orchestration and Choreography for a Clean Architecture
ThomasHeinrichs169 views
Data-centric AI and the convergence of data and model engineering: opportunit... by Paolo Missier
Data-centric AI and the convergence of data and model engineering:opportunit...Data-centric AI and the convergence of data and model engineering:opportunit...
Data-centric AI and the convergence of data and model engineering: opportunit...
Paolo Missier34 views
STPI OctaNE CoE Brochure.pdf by madhurjyapb
STPI OctaNE CoE Brochure.pdfSTPI OctaNE CoE Brochure.pdf
STPI OctaNE CoE Brochure.pdf
madhurjyapb12 views
Five Things You SHOULD Know About Postman by Postman
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About Postman
Postman27 views
.conf Go 2023 - Data analysis as a routine by Splunk
.conf Go 2023 - Data analysis as a routine.conf Go 2023 - Data analysis as a routine
.conf Go 2023 - Data analysis as a routine
Splunk93 views
AI: mind, matter, meaning, metaphors, being, becoming, life values by Twain Liu 刘秋艳
AI: mind, matter, meaning, metaphors, being, becoming, life valuesAI: mind, matter, meaning, metaphors, being, becoming, life values
AI: mind, matter, meaning, metaphors, being, becoming, life values
Understanding GenAI/LLM and What is Google Offering - Felix Goh by NUS-ISS
Understanding GenAI/LLM and What is Google Offering - Felix GohUnderstanding GenAI/LLM and What is Google Offering - Felix Goh
Understanding GenAI/LLM and What is Google Offering - Felix Goh
NUS-ISS41 views
Attacking IoT Devices from a Web Perspective - Linux Day by Simone Onofri
Attacking IoT Devices from a Web Perspective - Linux Day Attacking IoT Devices from a Web Perspective - Linux Day
Attacking IoT Devices from a Web Perspective - Linux Day
Simone Onofri15 views
PharoJS - Zürich Smalltalk Group Meetup November 2023 by Noury Bouraqadi
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023
Noury Bouraqadi120 views
handbook for web 3 adoption.pdf by Liveplex
handbook for web 3 adoption.pdfhandbook for web 3 adoption.pdf
handbook for web 3 adoption.pdf
Liveplex19 views
Perth MeetUp November 2023 by Michael Price
Perth MeetUp November 2023 Perth MeetUp November 2023
Perth MeetUp November 2023
Michael Price15 views
How the World's Leading Independent Automotive Distributor is Reinventing Its... by NUS-ISS
How the World's Leading Independent Automotive Distributor is Reinventing Its...How the World's Leading Independent Automotive Distributor is Reinventing Its...
How the World's Leading Independent Automotive Distributor is Reinventing Its...
NUS-ISS15 views
Business Analyst Series 2023 - Week 3 Session 5 by DianaGray10
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5
DianaGray10209 views

Back to the future - Temporal Table in SQL Server 2016

  • 1. Back to the future – Temporal Tables in SQL Server 2016 Stéphane Fréchette Thursday September 17, 2015
  • 2. 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: @sfrechette LinkedIn: ca.linkedin.com/stephanefrechette Blog: stephanefrechette.com Email: stephanefrechette@ukubu.com SQLSaturday Boston #364 Who Am I?
  • 4. 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?
  • 5. 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 Why Temporal Time Travel Data Audit Slowly Changing Dimensions Repair record-level corruptions
  • 6. 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_TIME AS OF FROM..TO BETWEEN..AND CONTAINED IN Temporal Querying
  • 7. Temporal table (actual data) Insert / Bulk Insert * Old versions Update */ Delete * How system-time works? History Table
  • 8. Temporal table (actual data) Temporal Queries * (Time travel,etc.) How system-time works? History Table Regular queries (current data) * Include Historical Version
  • 9. Querying Temporal Data Expression 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
  • 10. 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_TIME BETWEEN '2006.01.01' AND '2007.01.01' SELECT * FROM Department FOR SYSTEM_TIME CONTAINED 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
  • 11. 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.BusinessEntityContact FOR SYSTEM_TIME BETWEEN @Start AND @End WHERE ContactTypeID = 17 Performance Temporal database support - BETWEEN
  • 12. • 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…
  • 13. SELECT * FROM Department FOR SYSTEM_TIME AS 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 Business Scenario…
  • 14. MSDN Documentation https://msdn.microsoft.com/en-CA/library/dn935015.aspx Temporal in SQL Server 2016 (Video) – Channel 9 https://channel9.msdn.com/Shows/Data-Exposed/Temporal-in-SQL-Server-2016 SQL Server Pro – First Look at System-Versioned Temporal Tables… http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-1-creating-tables-and-modifying-data Resources
  • 15. What Questions Do You Have?
  • 16. Thank You For attending this session

Editor's Notes

  1. Source: https://msdn.microsoft.com/en-us/library/dn935015(v=sql.130).aspx A temporal table is a new type of table that provides correct information about stored facts at any point in time. Each temporal table consists of two tables actually, one for the current data and one for the historical data. The system automagically ensures that when the data changes in the table with the current data the previous values are stored in the historical table. Querying constructs are provided to hide this complexity from users. For more information, see Temporal Tables. Introduction to Key Components and Concepts What is a Temporal Table? 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. Why Temporal? Real data sources are dynamic and more often than not business decisions rely on insights that analysts can get from data evolution. Use cases for temporal tables include: Understanding business trends over time Tracking data changes over time Auditing all changes to data Maintaining a slowly changing dimension for decision support applications Recovering from accidental data changes and application errors
  2. Source: https://msdn.microsoft.com/en-us/library/dn935015.aspx A temporal table is a new type of table that provides correct information about stored facts at any point in time. Each temporal table consists of two tables actually, one for the current data and one for the historical data. The system automagically ensures that when the data changes in the table with the current data the previous values are stored in the historical table. Querying constructs are provided to hide this complexity from users. For more information, see Temporal Tables. Introduction to Key Components and Concepts What is a Temporal Table? 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. Why Temporal? Real data sources are dynamic and more often than not business decisions rely on insights that analysts can get from data evolution. Use cases for temporal tables include: Understanding business trends over time Tracking data changes over time Auditing all changes to data Maintaining a slowly changing dimension for decision support applications Recovering from accidental data changes and application errors
  3. Source: https://msdn.microsoft.com/en-us/library/dn935015.aspx How Does Temporal Work? System-versioning for a table is implemented as a pair of tables, a current table and a history table. Within each of these tables, two additional datetime (datetime2 datatype) columns are used to define the period of validity for each record – a system start time (SysStartTime) column and a system end time (SysEndTime) column. The current table contains the current value for each record. The history table contains the each previous value for each record, if any, and the start time and end time for the period for which it was valid. INSERTS: On an INSERT, the system sets the value for the SysStartTime column to the UTC time of the current transaction based on the system clock and assigns the value for the SysEndTime column to the maximum value of 9999-12-31 – this marks the record as open. UPDATES: On an UPDATE, the system stores the previous value of the record in the history table and sets the value for the SysEndTime column to the UTC time of the current transaction based on the system clock. This marks the record as closed, with a period recorded for which the record was valid. In the current table, the record is updated with its new value and the system sets the value for the SysStartTime column to the UTC time for the transaction based on the system clock. The value for the updated record in the current table for the SysEndTime column remains the maximum value of 9999-12-31. DELETES: On a DELETE, the system stores the previous value of the record in the history table and sets the value for the SysEndTime column to the UTC time of the current transaction based on the system clock. This marks this record as closed, with a period recorded for which the previous record was valid. In the current table, the record is removed. Queries of the current table will not return this value. Only queries that deal with history data return data for which a record is closed. MERGE: On a MERGE, MERGE behaves as an INSERT, an UPDATE, or a DELETE based on the condition for each record.
  4. Source: https://msdn.microsoft.com/en-us/library/dn935015.aspx The SYSTEM_TIME period columns used to record the SysStartTime and SysEndTime values must be defined with a datatype of datetime2.
  5. Source: https://msdn.microsoft.com/en-us/library/dn935015.aspx The SYSTEM_TIME period columns used to record the SysStartTime and SysEndTime values must be defined with a datatype of datetime2.
  6. Source: https://msdn.microsoft.com/en-us/library/dn935015.aspx Returns a table with the values for all record versions that were active within the specified time range, regardless of whether they started being active before the <start_date_time> parameter value for the FROM argument or ceased being active after the <end_date_time> parameter value for the TO argument. Internally, a union is performed between the temporal table and its history table and the results are filtered to return the values for all row versions that were active at any time during the time range specified. Records that became active exactly on the lower boundary defined by the FROM endpoint are included and records that became active exactly on the upper boundary defined by the TO endpoint are also included.
  7. Source: https://msdn.microsoft.com/en-us/library/dn935015.aspx Returns a table with single record for each row containing the values that were actual (current) at the specified point in time in the past. Internally, a union is performed between the temporal table and its history table and the results are filtered to return the values in the row that was valid at the point in time specified by the <date_time> parameter. The value for a row is deemed valid if the system_start_time_column_name value is less than or equal to the <date_time> parameter value and the system_end_time_column_name value is greater than the <date_time> parameter value.
  8. Source: https://msdn.microsoft.com/en-us/library/dn935015.aspx Returns a table with the values for all record versions that were active within the specified time range, regardless of whether they started being active before the <start_date_time> parameter value for the FROM argument or ceased being active after the <end_date_time> parameter value for the TO argument. Internally, a union is performed between the temporal table and its history table and the results are filtered to return the values for all row versions that were active at any time during the time range specified. Records that became active exactly on the lower boundary defined by the FROM endpoint are included and records that became active exactly on the upper boundary defined by the TO endpoint are also included.
  9. Source: https://msdn.microsoft.com/en-us/library/dn935015.aspx A temporal table is a new type of table that provides correct information about stored facts at any point in time. Each temporal table consists of two tables actually, one for the current data and one for the historical data. The system automagically ensures that when the data changes in the table with the current data the previous values are stored in the historical table. Querying constructs are provided to hide this complexity from users. For more information, see Temporal Tables. Introduction to Key Components and Concepts What is a Temporal Table? 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. Why Temporal? Real data sources are dynamic and more often than not business decisions rely on insights that analysts can get from data evolution. Use cases for temporal tables include: Understanding business trends over time Tracking data changes over time Auditing all changes to data Maintaining a slowly changing dimension for decision support applications Recovering from accidental data changes and application errors
  10. Source: http://channel9.msdn.com/Shows/Data-Exposed/Temporal-in-SQL-Server-2016 Example of using temporal tables with Azure SQL Database stretch tables.