SlideShare uma empresa Scribd logo
1 de 28
Arthur Luz | SQL Server MCSA & MCT
arthurjosemberg@gmail.com
http://arthurluz.wordpress.com
Who am I?
Data Insights Consultant at Microsoft
Technical Writer at Data’s Light Blog
MCSA e MCT em SQL Server
Official Instructor at Hepta Novintec
 Overview – SQL Server 2016 Temporal Tables
 New Clauses – Data Definition
 New Clauses – Data Manipulation
 Considerations for resource use
 Main Differences - CDC and Temporal Tables
 Usage Scenarios
 Feature Improvement
Schedule for today
Overview – History Tables
Overview – History Tables
Overview – History Tables
 Auditing all data changes and performing data
forensics when necessary
 Reconstructing state of the data as of any time in the
past
 Calculating trends over time – Anomaly Detection
 Maintaining a slowly changing dimension for
decision support applications
 Recovering from accidental data changes and
application errors
New clauses – Data Definition
CREATE TABLE Department (
DeptID int NOT NULL PRIMARY KEY CLUSTERED ,
DeptName varchar(50) NOT NULL ,
ManagerID INT NULL ,
ParentDeptID int NULL ,
SysStartTime datetime2 GENERATED ALWAYS AS ROW START NOT NULL ,
SysEndTime datetime2 GENERATED ALWAYS AS ROW END NOT NULL ,
PERIOD FOR SYSTEM_TIME (SysStartTime, SysEndTime)
) WITH ( SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.DepartmentHistory) ) ;
GENERATED ALWAYS AS ROW START –
Determina que essa coluna recebá do sistema a
data inicial de validade do registro
GENERATED ALWAYS AS ROW END –
Determina que essa colina receberá do sistema a
data final de validade do registro.
PERIOD FOR SYSTEM_TIME – Realiza a
organização histórica do registro de acordo com
as datas de inicio e fim de validade da tupla.
SYSTEM_VERSIONING = ON – Habilita a
feature para a tabela que está sendo criada ou
alterada.
BEGIN TRANSACTION
-- Add required period columns and designation
ALTER TABLE dbo.Person ADD
DateTimeStart DATETIME2(0) GENERATED ALWAYS AS ROW START NOT NULL
CONSTRAINT DFT_person_datetimeStart DEFAULT('19000101 00:00:00'),
DateTimeEnd DATETIME2(0) GENERATED ALWAYS AS ROW END NOT NULL
CONSTRAINT DFT_person_datetimeEnd DEFAULT('99991231 23:59:59'),
PERIOD FOR SYSTEM_TIME (DateTimeStart, DateTimeEnd);
-- Remove temporary DEFAULT constraints
ALTER TABLE dbo.Person
DROP CONSTRAINT DFT_person_datetimeStart, DFT_person_datetimeEnd;
-- Turn system versioning on
ALTER TABLE dbo.Person
SET ( SYSTEM_VERSIONING = ON ( HISTORY_TABLE = dbo.PersonHistory ) );
COMMIT TRANSACTION;
Demonstration 1
Creating Temporal Table
in SQL Server 2016
New clauses – Data Manipulation
DECLARE @datetime AS DATETIME2 = '2016-11-08 20:00:00'
SELECT *
FROM dbo.Person FOR SYSTEM_TIME
AS OF @datetime
ORDER BY DateTimeStart ASC,
DateTimeEnd ASC
Clauses SYSTEM_TIME
allow to realize temporal
queries transparently for
user and applications.
FOR SYSTEM_TIME AS OF @datetime
Reference: http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-data-and-optimization-conside (Itzik Ben Gan)
sysEnd > @datetime
sysStart <= @datetime
New clauses – Data Manipulation
Returns a table with a rows containing the
values that were actual (current) at the
specified point in time in the past.
FOR SYSTEM_TIME
FROM @Start TO @End
Reference: http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-data-and-optimization-conside (Itzik Ben Gan)
sysStart < @End
sysEnd > @Start
New clauses – Data Manipulation
Returns a table with the values for all row
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.
FOR SYSTEM_TIME
BETWEEN @Start AND @End
Reference: http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-data-and-optimization-conside (Itzik Ben Gan)
sysStart <= @End
sysEnd > @Start
New clauses – Data Manipulation
Same as above in the FOR SYSTEM_TIME
FROM <start_date_time> TO <end_date_time>
description, except the table of rows returned
includes rows that became active on the upper
boundary defined by the <end_date_time>
endpoint.
FOR SYSTEM_TIME
CONTAINED IN (@Start,@End)
Reference: http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-data-and-optimization-conside (Itzik Ben Gan)
sysStart >= @Start
sysEnd <= @End
New clauses – Data Manipulation
Returns a table with the values for all row
versions that were opened and closed within the
specified time range defined by the two
datetime values for the CONTAINED IN
argument.
Demonstration 2
Manipulation historical data in
SQL Server Temporal Tables
Considerations for resource use
 A temporal table must have a primary key defined in order to correlate records between the current table and
the history table, and the history table cannot have a primary key defined.
 The SYSTEM_TIME period columns used to record the SysStartTime and SysEndTime values must be
defined with a datatype of datetime2.
 If the name of a history table is specified during history table creation, you must specify the schema and table
name.
 By default, the history table is PAGE compressed.
 If current table is partitioned, the history table is created on default file group because partitioning
configuration is not replicated automatically from the current table to the history table.
 Temporal and history tables cannot be FILETABLE and can contain columns of any supported datatype other
than FILESTREAM since FILETABLE and FILESTREAM allow data manipulation outside of SQL Server and
thus system versioning cannot be guaranteed.
Considerations for resource use
 While temporal tables support blob data types, such as (n)varchar(max), varbinary(max), (n)text, and image,
their will incur significant storage costs and have performance implications due to their size. As such, when
designing your system, care should be taken when using these data types.
 History table must be created in the same database as the current table. Temporal querying over Linked Server
is not supported.
 History table cannot have constraints (primary key, foreign key, table or column constraints).
 Indexed views are not supported on top of temporal queries (queries that use FOR SYSTEM_TIME clause)
 Online option (WITH (ONLINE = ON) has no effect on ALTER TABLE ALTER COLUMN in case of system-
versioned temporal table. ALTER column is not performed as online regardless of which value was specified for
ONLINE option.
 INSERT and UPDATE statements cannot reference the SYSTEM_TIME period columns. Attempts to insert
values directly into these columns will be blocked.
Considerations for resource use
 TRUNCATE TABLE is not supported while SYSTEM_VERSIONING is ON
 Direct modification of the data in a history table is not permitted.
 ON DELETE CASCADE and ON UPDATE CASCADE are not permitted on the current table. In other words,
when temporal table is referencing table in the foreign key relationship (corresponding to parent_object_id in
sys.foreign_keys) CASCADE options are not allowed. To work around this limitation, use application logic or
after triggers to maintain consistency on delete in primary key table (corresponding to referenced_object_id in
sys.foreign_keys). If primary key table is temporal and referencing table is non-temporal, there’s no such
limitation.
 INSTEAD OF triggers are not permitted on either the current or the history table and AFTER triggers are
permitted only on the current table.
 Regular queries only affect data in the current table. To query data in the history table, you must use temporal
queries.
Considerations for resource use
 An optimal indexing strategy will include a clustered columns store index
and / or a B-tree rowstore index on the current table and a clustered
columnstore index on the history table for optimal storage size and
performance. If you create / use your own history table, we strongly
recommend that you create this type of index consisting of period columns
starting with the end of period column to speed up temporal querying as
well as speeding up the queries that are part of the data consistency check.
The default history table has a clustered rowstore index created for you
based on the period columns (end, start). At a minimum, a non-clustered
rowstore index is recommended.
Table 1
Main Differences – CDC x Temporal Tables
Log
_CDC 1
Uses all table columns.
Possible to select specific columns.
Works synchronously.
Works asynchronously. Allows change in table
when feature is active.
It does not allow change in the
table when the feature is active.
Used to maintain history in the
transactional system.
Used only for momentary
capture of history.
Simple queries that are transparent
to users and applications.
Complex queries that require
in-depth knowledge.
Version INSERTEs, UPDATEs and DELETEs.
Version only UPDATEs and DELETEs.
Demonstration 3
CDC x History Tables
Main Differences
Usage Scenarios
Data Audit
Point in Time
Analysis
(Time Travel)
Calculating trends
over time
(Anomaly Detection)
Slowly-Changing
Dimensions
Repairing Row-Level
Data Corruption
Demonstration 4
Getting rollback of
committed transactions
Columnstore Index
Table Partitioning
Stretch Database
In-Memory Tables
Cleanup
Feature Improvement and Retention
Feature Improvement – In-Memory
 Only durable memory-optimized tables can be system-versioned
(DURABILITY = SCHEMA_AND_DATA).
 Queries that affect only the current table (in-memory) can be used in natively
compiled T-SQL modules. Temporal queries using the FOR SYSTEM TIME
clause are not supported in natively compiled modules. Use of the FOR
SYSTEM TIME clause with memory-optimized tables in ad hoc queries and
non-native modules is supported.
 When SYSTEM_VERSIONING = ON, an internal memory-optimized staging
table is automatically created to accept the most recent system-versioned
changes that are results of update and delete operations on memory-
optimized current table.
 Data from the internal memory-optimized staging table is regularly moved to
the disk-based history table by the asynchronous data flush task. This data
flush mechanism has a goal to keep the internal memory buffers at less than
10% of the memory consumption of their parent objects.
Hybrid Transactional/Analytical Processing (HTAP)
Demonstration 5
Creating Temporal Table
in SQL Server 2016 In-Memory Table
Thank you so much!!
Questions???
Email - arthurjosemberg@gmail.com
Linkedin – Arthur Luz
Twitter - @arthurjosemberg
Skype - arthurjosemberg
Blog – arthurluz.wordpress.com
Contacts

Mais conteúdo relacionado

Mais procurados (20)

Sq lite
Sq liteSq lite
Sq lite
 
Predictive performance analysis using sql pattern matching
Predictive performance analysis using sql pattern matchingPredictive performance analysis using sql pattern matching
Predictive performance analysis using sql pattern matching
 
Back to the future - Temporal Table in SQL Server 2016
Back to the future - Temporal Table in SQL Server 2016Back to the future - Temporal Table in SQL Server 2016
Back to the future - Temporal Table in SQL Server 2016
 
SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?
 
Ch6
Ch6Ch6
Ch6
 
OER UNIT 4 PARTITION
OER UNIT 4 PARTITIONOER UNIT 4 PARTITION
OER UNIT 4 PARTITION
 
Lecture 3. MS SQL. Cursors.
Lecture 3. MS SQL. Cursors.Lecture 3. MS SQL. Cursors.
Lecture 3. MS SQL. Cursors.
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
Oracle SQL Advanced
Oracle SQL AdvancedOracle SQL Advanced
Oracle SQL Advanced
 
Getting Started with MySQL II
Getting Started with MySQL IIGetting Started with MySQL II
Getting Started with MySQL II
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Getting Started with MySQL I
Getting Started with MySQL IGetting Started with MySQL I
Getting Started with MySQL I
 
Oracle: DDL
Oracle: DDLOracle: DDL
Oracle: DDL
 
8. sql
8. sql8. sql
8. sql
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
SQL
SQLSQL
SQL
 
Adbms
AdbmsAdbms
Adbms
 
Sql commands
Sql commandsSql commands
Sql commands
 
New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012
 
Columnrename9i
Columnrename9iColumnrename9i
Columnrename9i
 

Destaque

Query Store and live Query Statistics
Query Store and live Query StatisticsQuery Store and live Query Statistics
Query Store and live Query StatisticsSolidQ
 
SQL Server In-Memory OLTP introduction (Hekaton)
SQL Server In-Memory OLTP introduction (Hekaton)SQL Server In-Memory OLTP introduction (Hekaton)
SQL Server In-Memory OLTP introduction (Hekaton)Shy Engelberg
 
An introduction to SQL Server in-memory OLTP Engine
An introduction to SQL Server in-memory OLTP EngineAn introduction to SQL Server in-memory OLTP Engine
An introduction to SQL Server in-memory OLTP EngineKrishnakumar S
 
Stretch Database
Stretch DatabaseStretch Database
Stretch DatabaseSolidQ
 
Always encrypted overview
Always encrypted overviewAlways encrypted overview
Always encrypted overviewSolidQ
 
Row-level security and Dynamic Data Masking
Row-level security and Dynamic Data MaskingRow-level security and Dynamic Data Masking
Row-level security and Dynamic Data MaskingSolidQ
 
SQL Server 2016 Editions
SQL Server 2016 Editions SQL Server 2016 Editions
SQL Server 2016 Editions Onomi
 
Live Query Statistics & Query Store in SQL Server 2016
Live Query Statistics & Query Store in SQL Server 2016Live Query Statistics & Query Store in SQL Server 2016
Live Query Statistics & Query Store in SQL Server 2016Antonios Chatzipavlis
 
Travelling in time with SQL Server 2016 - Damian Widera
Travelling in time with SQL Server 2016 - Damian WideraTravelling in time with SQL Server 2016 - Damian Widera
Travelling in time with SQL Server 2016 - Damian WideraITCamp
 
Temporal Snapshot Fact Tables
Temporal Snapshot Fact TablesTemporal Snapshot Fact Tables
Temporal Snapshot Fact TablesDavide Mauri
 
Live Presentation Transformation From Boring to Effective - Boris Hristov
Live Presentation Transformation From Boring to Effective - Boris HristovLive Presentation Transformation From Boring to Effective - Boris Hristov
Live Presentation Transformation From Boring to Effective - Boris HristovITCamp
 
High Availability & Disaster Recovery with SQL Server 2012 AlwaysOn Availabil...
High Availability & Disaster Recovery with SQL Server 2012 AlwaysOn Availabil...High Availability & Disaster Recovery with SQL Server 2012 AlwaysOn Availabil...
High Availability & Disaster Recovery with SQL Server 2012 AlwaysOn Availabil...turgaysahtiyan
 
SQL 2016 Mejoras en InMemory OLTP y Column Store Index
SQL 2016 Mejoras en InMemory OLTP y Column Store IndexSQL 2016 Mejoras en InMemory OLTP y Column Store Index
SQL 2016 Mejoras en InMemory OLTP y Column Store IndexEduardo Castro
 
SQL Saturday 510 Paris 2016 - Query Store session - final
SQL Saturday 510 Paris 2016 - Query Store session - finalSQL Saturday 510 Paris 2016 - Query Store session - final
SQL Saturday 510 Paris 2016 - Query Store session - finalPhilippe Geiger
 
SQL Server It Just Runs Faster
SQL Server It Just Runs FasterSQL Server It Just Runs Faster
SQL Server It Just Runs FasterBob Ward
 
SQL Server In-Memory OLTP: What Every SQL Professional Should Know
SQL Server In-Memory OLTP: What Every SQL Professional Should KnowSQL Server In-Memory OLTP: What Every SQL Professional Should Know
SQL Server In-Memory OLTP: What Every SQL Professional Should KnowBob Ward
 
Inside SQL Server In-Memory OLTP
Inside SQL Server In-Memory OLTPInside SQL Server In-Memory OLTP
Inside SQL Server In-Memory OLTPBob Ward
 
Ssis 2016 RC3
Ssis 2016 RC3Ssis 2016 RC3
Ssis 2016 RC3MSDEVMTL
 

Destaque (19)

Query Store and live Query Statistics
Query Store and live Query StatisticsQuery Store and live Query Statistics
Query Store and live Query Statistics
 
SQL Server In-Memory OLTP introduction (Hekaton)
SQL Server In-Memory OLTP introduction (Hekaton)SQL Server In-Memory OLTP introduction (Hekaton)
SQL Server In-Memory OLTP introduction (Hekaton)
 
An introduction to SQL Server in-memory OLTP Engine
An introduction to SQL Server in-memory OLTP EngineAn introduction to SQL Server in-memory OLTP Engine
An introduction to SQL Server in-memory OLTP Engine
 
Stretch Database
Stretch DatabaseStretch Database
Stretch Database
 
Always encrypted overview
Always encrypted overviewAlways encrypted overview
Always encrypted overview
 
Row-level security and Dynamic Data Masking
Row-level security and Dynamic Data MaskingRow-level security and Dynamic Data Masking
Row-level security and Dynamic Data Masking
 
SQL Server 2016 Editions
SQL Server 2016 Editions SQL Server 2016 Editions
SQL Server 2016 Editions
 
Live Query Statistics & Query Store in SQL Server 2016
Live Query Statistics & Query Store in SQL Server 2016Live Query Statistics & Query Store in SQL Server 2016
Live Query Statistics & Query Store in SQL Server 2016
 
Travelling in time with SQL Server 2016 - Damian Widera
Travelling in time with SQL Server 2016 - Damian WideraTravelling in time with SQL Server 2016 - Damian Widera
Travelling in time with SQL Server 2016 - Damian Widera
 
Temporal Snapshot Fact Tables
Temporal Snapshot Fact TablesTemporal Snapshot Fact Tables
Temporal Snapshot Fact Tables
 
Live Presentation Transformation From Boring to Effective - Boris Hristov
Live Presentation Transformation From Boring to Effective - Boris HristovLive Presentation Transformation From Boring to Effective - Boris Hristov
Live Presentation Transformation From Boring to Effective - Boris Hristov
 
High Availability & Disaster Recovery with SQL Server 2012 AlwaysOn Availabil...
High Availability & Disaster Recovery with SQL Server 2012 AlwaysOn Availabil...High Availability & Disaster Recovery with SQL Server 2012 AlwaysOn Availabil...
High Availability & Disaster Recovery with SQL Server 2012 AlwaysOn Availabil...
 
SQL 2016 Mejoras en InMemory OLTP y Column Store Index
SQL 2016 Mejoras en InMemory OLTP y Column Store IndexSQL 2016 Mejoras en InMemory OLTP y Column Store Index
SQL 2016 Mejoras en InMemory OLTP y Column Store Index
 
SQL Saturday 510 Paris 2016 - Query Store session - final
SQL Saturday 510 Paris 2016 - Query Store session - finalSQL Saturday 510 Paris 2016 - Query Store session - final
SQL Saturday 510 Paris 2016 - Query Store session - final
 
SQL Server It Just Runs Faster
SQL Server It Just Runs FasterSQL Server It Just Runs Faster
SQL Server It Just Runs Faster
 
SQL Server In-Memory OLTP: What Every SQL Professional Should Know
SQL Server In-Memory OLTP: What Every SQL Professional Should KnowSQL Server In-Memory OLTP: What Every SQL Professional Should Know
SQL Server In-Memory OLTP: What Every SQL Professional Should Know
 
Otimizando a performance com in memory no sql 2016
Otimizando a performance com in memory no sql 2016Otimizando a performance com in memory no sql 2016
Otimizando a performance com in memory no sql 2016
 
Inside SQL Server In-Memory OLTP
Inside SQL Server In-Memory OLTPInside SQL Server In-Memory OLTP
Inside SQL Server In-Memory OLTP
 
Ssis 2016 RC3
Ssis 2016 RC3Ssis 2016 RC3
Ssis 2016 RC3
 

Semelhante a sql_server_2016_history_tables

Walking down the memory lane with temporal tables
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
 
Webinar - MariaDB Temporal Tables: a demonstration
Webinar - MariaDB Temporal Tables: a demonstrationWebinar - MariaDB Temporal Tables: a demonstration
Webinar - MariaDB Temporal Tables: a demonstrationFederico Razzoli
 
Sql server 2016 new features
Sql server 2016 new featuresSql server 2016 new features
Sql server 2016 new featuresAjeet Singh
 
Informix partitioning interval_rolling_window_table
Informix partitioning interval_rolling_window_tableInformix partitioning interval_rolling_window_table
Informix partitioning interval_rolling_window_tableKeshav Murthy
 
Sql 2016 - What's New
Sql 2016 - What's NewSql 2016 - What's New
Sql 2016 - What's Newdpcobb
 
PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PostgreSQL Performance Tables Partitioning vs. Aggregated Data TablesPostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PostgreSQL Performance Tables Partitioning vs. Aggregated Data TablesSperasoft
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slidesmetsarin
 
Redefining tables online without surprises
Redefining tables online without surprisesRedefining tables online without surprises
Redefining tables online without surprisesNelson Calero
 
Cassandra Table Modeling - an alternate approach
Cassandra Table Modeling - an alternate approachCassandra Table Modeling - an alternate approach
Cassandra Table Modeling - an alternate approachDevopam Mittra
 
MariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB Server 10.3 - Temporale Daten und neues zur DB-KompatibilitätMariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB Server 10.3 - Temporale Daten und neues zur DB-KompatibilitätMariaDB plc
 
Perfect trio : temporal tables, transparent archiving in db2 for z_os and idaa
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
 
Relational Database Language.pptx
Relational Database Language.pptxRelational Database Language.pptx
Relational Database Language.pptxSheethal Aji Mani
 
What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3MariaDB plc
 
Most useful queries
Most useful queriesMost useful queries
Most useful queriesSam Depp
 
Flashback time travel vs Flash back Data Archive.pdf
Flashback time travel  vs Flash back Data Archive.pdfFlashback time travel  vs Flash back Data Archive.pdf
Flashback time travel vs Flash back Data Archive.pdfAlireza Kamrani
 

Semelhante a sql_server_2016_history_tables (20)

Walking down the memory lane with temporal tables
Walking down the memory lane with temporal tablesWalking down the memory lane with temporal tables
Walking down the memory lane with temporal tables
 
Webinar - MariaDB Temporal Tables: a demonstration
Webinar - MariaDB Temporal Tables: a demonstrationWebinar - MariaDB Temporal Tables: a demonstration
Webinar - MariaDB Temporal Tables: a demonstration
 
Sql server 2016 new features
Sql server 2016 new featuresSql server 2016 new features
Sql server 2016 new features
 
Sql server 2016 new features
Sql server 2016 new featuresSql server 2016 new features
Sql server 2016 new features
 
Informix partitioning interval_rolling_window_table
Informix partitioning interval_rolling_window_tableInformix partitioning interval_rolling_window_table
Informix partitioning interval_rolling_window_table
 
Sql 2016 - What's New
Sql 2016 - What's NewSql 2016 - What's New
Sql 2016 - What's New
 
PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PostgreSQL Performance Tables Partitioning vs. Aggregated Data TablesPostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 
Redefining tables online without surprises
Redefining tables online without surprisesRedefining tables online without surprises
Redefining tables online without surprises
 
Cassandra Table Modeling - an alternate approach
Cassandra Table Modeling - an alternate approachCassandra Table Modeling - an alternate approach
Cassandra Table Modeling - an alternate approach
 
MariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB Server 10.3 - Temporale Daten und neues zur DB-KompatibilitätMariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
 
Perfect trio : temporal tables, transparent archiving in db2 for z_os and idaa
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
 
Relational Database Language.pptx
Relational Database Language.pptxRelational Database Language.pptx
Relational Database Language.pptx
 
What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3
 
MariaDB Temporal Tables
MariaDB Temporal TablesMariaDB Temporal Tables
MariaDB Temporal Tables
 
IR SQLite Session #3
IR SQLite Session #3IR SQLite Session #3
IR SQLite Session #3
 
Most useful queries
Most useful queriesMost useful queries
Most useful queries
 
SQL
SQLSQL
SQL
 
Flashback time travel vs Flash back Data Archive.pdf
Flashback time travel  vs Flash back Data Archive.pdfFlashback time travel  vs Flash back Data Archive.pdf
Flashback time travel vs Flash back Data Archive.pdf
 
Teradata sql-tuning-top-10
Teradata sql-tuning-top-10Teradata sql-tuning-top-10
Teradata sql-tuning-top-10
 

Mais de arthurjosemberg

carreira_certificacoes_mercado_de_trabalho
carreira_certificacoes_mercado_de_trabalhocarreira_certificacoes_mercado_de_trabalho
carreira_certificacoes_mercado_de_trabalhoarthurjosemberg
 
por_detras_dos_relatorios
por_detras_dos_relatoriospor_detras_dos_relatorios
por_detras_dos_relatoriosarthurjosemberg
 
realizando_limpeza_de_dados_com_data_quality_services
realizando_limpeza_de_dados_com_data_quality_servicesrealizando_limpeza_de_dados_com_data_quality_services
realizando_limpeza_de_dados_com_data_quality_servicesarthurjosemberg
 
carreira_certificacoes_mercado_de_trabalho
carreira_certificacoes_mercado_de_trabalhocarreira_certificacoes_mercado_de_trabalho
carreira_certificacoes_mercado_de_trabalhoarthurjosemberg
 
global_azure_bootcamp_2016
global_azure_bootcamp_2016global_azure_bootcamp_2016
global_azure_bootcamp_2016arthurjosemberg
 
desvendando_o_microsoft_datazen
desvendando_o_microsoft_datazendesvendando_o_microsoft_datazen
desvendando_o_microsoft_datazenarthurjosemberg
 
Desvendando o Microsoft Datazen
Desvendando o Microsoft DatazenDesvendando o Microsoft Datazen
Desvendando o Microsoft Datazenarthurjosemberg
 
datazen_inicio_ao_fim_sat_df
datazen_inicio_ao_fim_sat_dfdatazen_inicio_ao_fim_sat_df
datazen_inicio_ao_fim_sat_dfarthurjosemberg
 
datazen_inicio_ao_fim_sat_sp
datazen_inicio_ao_fim_sat_spdatazen_inicio_ao_fim_sat_sp
datazen_inicio_ao_fim_sat_sparthurjosemberg
 

Mais de arthurjosemberg (13)

carreira_certificacoes_mercado_de_trabalho
carreira_certificacoes_mercado_de_trabalhocarreira_certificacoes_mercado_de_trabalho
carreira_certificacoes_mercado_de_trabalho
 
por_detras_dos_relatorios
por_detras_dos_relatoriospor_detras_dos_relatorios
por_detras_dos_relatorios
 
realizando_limpeza_de_dados_com_data_quality_services
realizando_limpeza_de_dados_com_data_quality_servicesrealizando_limpeza_de_dados_com_data_quality_services
realizando_limpeza_de_dados_com_data_quality_services
 
carreira_certificacoes_mercado_de_trabalho
carreira_certificacoes_mercado_de_trabalhocarreira_certificacoes_mercado_de_trabalho
carreira_certificacoes_mercado_de_trabalho
 
ssrs_2016_sql_day_bahia
ssrs_2016_sql_day_bahiassrs_2016_sql_day_bahia
ssrs_2016_sql_day_bahia
 
global_azure_bootcamp_2016
global_azure_bootcamp_2016global_azure_bootcamp_2016
global_azure_bootcamp_2016
 
ssrs_2016_sat_joinville
ssrs_2016_sat_joinvillessrs_2016_sat_joinville
ssrs_2016_sat_joinville
 
4_horas_microsoft
4_horas_microsoft4_horas_microsoft
4_horas_microsoft
 
desvendando_o_microsoft_datazen
desvendando_o_microsoft_datazendesvendando_o_microsoft_datazen
desvendando_o_microsoft_datazen
 
Cargas Dinamicas - SSIS
Cargas Dinamicas - SSISCargas Dinamicas - SSIS
Cargas Dinamicas - SSIS
 
Desvendando o Microsoft Datazen
Desvendando o Microsoft DatazenDesvendando o Microsoft Datazen
Desvendando o Microsoft Datazen
 
datazen_inicio_ao_fim_sat_df
datazen_inicio_ao_fim_sat_dfdatazen_inicio_ao_fim_sat_df
datazen_inicio_ao_fim_sat_df
 
datazen_inicio_ao_fim_sat_sp
datazen_inicio_ao_fim_sat_spdatazen_inicio_ao_fim_sat_sp
datazen_inicio_ao_fim_sat_sp
 

sql_server_2016_history_tables

  • 1. Arthur Luz | SQL Server MCSA & MCT arthurjosemberg@gmail.com http://arthurluz.wordpress.com
  • 2. Who am I? Data Insights Consultant at Microsoft Technical Writer at Data’s Light Blog MCSA e MCT em SQL Server Official Instructor at Hepta Novintec
  • 3.  Overview – SQL Server 2016 Temporal Tables  New Clauses – Data Definition  New Clauses – Data Manipulation  Considerations for resource use  Main Differences - CDC and Temporal Tables  Usage Scenarios  Feature Improvement Schedule for today
  • 6. Overview – History Tables  Auditing all data changes and performing data forensics when necessary  Reconstructing state of the data as of any time in the past  Calculating trends over time – Anomaly Detection  Maintaining a slowly changing dimension for decision support applications  Recovering from accidental data changes and application errors
  • 7. New clauses – Data Definition CREATE TABLE Department ( DeptID int NOT NULL PRIMARY KEY CLUSTERED , DeptName varchar(50) NOT NULL , ManagerID INT NULL , ParentDeptID int NULL , SysStartTime datetime2 GENERATED ALWAYS AS ROW START NOT NULL , SysEndTime datetime2 GENERATED ALWAYS AS ROW END NOT NULL , PERIOD FOR SYSTEM_TIME (SysStartTime, SysEndTime) ) WITH ( SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.DepartmentHistory) ) ; GENERATED ALWAYS AS ROW START – Determina que essa coluna recebá do sistema a data inicial de validade do registro GENERATED ALWAYS AS ROW END – Determina que essa colina receberá do sistema a data final de validade do registro. PERIOD FOR SYSTEM_TIME – Realiza a organização histórica do registro de acordo com as datas de inicio e fim de validade da tupla. SYSTEM_VERSIONING = ON – Habilita a feature para a tabela que está sendo criada ou alterada.
  • 8. BEGIN TRANSACTION -- Add required period columns and designation ALTER TABLE dbo.Person ADD DateTimeStart DATETIME2(0) GENERATED ALWAYS AS ROW START NOT NULL CONSTRAINT DFT_person_datetimeStart DEFAULT('19000101 00:00:00'), DateTimeEnd DATETIME2(0) GENERATED ALWAYS AS ROW END NOT NULL CONSTRAINT DFT_person_datetimeEnd DEFAULT('99991231 23:59:59'), PERIOD FOR SYSTEM_TIME (DateTimeStart, DateTimeEnd); -- Remove temporary DEFAULT constraints ALTER TABLE dbo.Person DROP CONSTRAINT DFT_person_datetimeStart, DFT_person_datetimeEnd; -- Turn system versioning on ALTER TABLE dbo.Person SET ( SYSTEM_VERSIONING = ON ( HISTORY_TABLE = dbo.PersonHistory ) ); COMMIT TRANSACTION;
  • 9. Demonstration 1 Creating Temporal Table in SQL Server 2016
  • 10. New clauses – Data Manipulation DECLARE @datetime AS DATETIME2 = '2016-11-08 20:00:00' SELECT * FROM dbo.Person FOR SYSTEM_TIME AS OF @datetime ORDER BY DateTimeStart ASC, DateTimeEnd ASC Clauses SYSTEM_TIME allow to realize temporal queries transparently for user and applications.
  • 11. FOR SYSTEM_TIME AS OF @datetime Reference: http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-data-and-optimization-conside (Itzik Ben Gan) sysEnd > @datetime sysStart <= @datetime New clauses – Data Manipulation Returns a table with a rows containing the values that were actual (current) at the specified point in time in the past.
  • 12. FOR SYSTEM_TIME FROM @Start TO @End Reference: http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-data-and-optimization-conside (Itzik Ben Gan) sysStart < @End sysEnd > @Start New clauses – Data Manipulation Returns a table with the values for all row 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.
  • 13. FOR SYSTEM_TIME BETWEEN @Start AND @End Reference: http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-data-and-optimization-conside (Itzik Ben Gan) sysStart <= @End sysEnd > @Start New clauses – Data Manipulation Same as above in the FOR SYSTEM_TIME FROM <start_date_time> TO <end_date_time> description, except the table of rows returned includes rows that became active on the upper boundary defined by the <end_date_time> endpoint.
  • 14. FOR SYSTEM_TIME CONTAINED IN (@Start,@End) Reference: http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-data-and-optimization-conside (Itzik Ben Gan) sysStart >= @Start sysEnd <= @End New clauses – Data Manipulation Returns a table with the values for all row versions that were opened and closed within the specified time range defined by the two datetime values for the CONTAINED IN argument.
  • 15. Demonstration 2 Manipulation historical data in SQL Server Temporal Tables
  • 16. Considerations for resource use  A temporal table must have a primary key defined in order to correlate records between the current table and the history table, and the history table cannot have a primary key defined.  The SYSTEM_TIME period columns used to record the SysStartTime and SysEndTime values must be defined with a datatype of datetime2.  If the name of a history table is specified during history table creation, you must specify the schema and table name.  By default, the history table is PAGE compressed.  If current table is partitioned, the history table is created on default file group because partitioning configuration is not replicated automatically from the current table to the history table.  Temporal and history tables cannot be FILETABLE and can contain columns of any supported datatype other than FILESTREAM since FILETABLE and FILESTREAM allow data manipulation outside of SQL Server and thus system versioning cannot be guaranteed.
  • 17. Considerations for resource use  While temporal tables support blob data types, such as (n)varchar(max), varbinary(max), (n)text, and image, their will incur significant storage costs and have performance implications due to their size. As such, when designing your system, care should be taken when using these data types.  History table must be created in the same database as the current table. Temporal querying over Linked Server is not supported.  History table cannot have constraints (primary key, foreign key, table or column constraints).  Indexed views are not supported on top of temporal queries (queries that use FOR SYSTEM_TIME clause)  Online option (WITH (ONLINE = ON) has no effect on ALTER TABLE ALTER COLUMN in case of system- versioned temporal table. ALTER column is not performed as online regardless of which value was specified for ONLINE option.  INSERT and UPDATE statements cannot reference the SYSTEM_TIME period columns. Attempts to insert values directly into these columns will be blocked.
  • 18. Considerations for resource use  TRUNCATE TABLE is not supported while SYSTEM_VERSIONING is ON  Direct modification of the data in a history table is not permitted.  ON DELETE CASCADE and ON UPDATE CASCADE are not permitted on the current table. In other words, when temporal table is referencing table in the foreign key relationship (corresponding to parent_object_id in sys.foreign_keys) CASCADE options are not allowed. To work around this limitation, use application logic or after triggers to maintain consistency on delete in primary key table (corresponding to referenced_object_id in sys.foreign_keys). If primary key table is temporal and referencing table is non-temporal, there’s no such limitation.  INSTEAD OF triggers are not permitted on either the current or the history table and AFTER triggers are permitted only on the current table.  Regular queries only affect data in the current table. To query data in the history table, you must use temporal queries.
  • 19. Considerations for resource use  An optimal indexing strategy will include a clustered columns store index and / or a B-tree rowstore index on the current table and a clustered columnstore index on the history table for optimal storage size and performance. If you create / use your own history table, we strongly recommend that you create this type of index consisting of period columns starting with the end of period column to speed up temporal querying as well as speeding up the queries that are part of the data consistency check. The default history table has a clustered rowstore index created for you based on the period columns (end, start). At a minimum, a non-clustered rowstore index is recommended.
  • 20. Table 1 Main Differences – CDC x Temporal Tables Log _CDC 1 Uses all table columns. Possible to select specific columns. Works synchronously. Works asynchronously. Allows change in table when feature is active. It does not allow change in the table when the feature is active. Used to maintain history in the transactional system. Used only for momentary capture of history. Simple queries that are transparent to users and applications. Complex queries that require in-depth knowledge. Version INSERTEs, UPDATEs and DELETEs. Version only UPDATEs and DELETEs.
  • 21. Demonstration 3 CDC x History Tables Main Differences
  • 22. Usage Scenarios Data Audit Point in Time Analysis (Time Travel) Calculating trends over time (Anomaly Detection) Slowly-Changing Dimensions Repairing Row-Level Data Corruption
  • 23. Demonstration 4 Getting rollback of committed transactions
  • 24. Columnstore Index Table Partitioning Stretch Database In-Memory Tables Cleanup Feature Improvement and Retention
  • 25. Feature Improvement – In-Memory  Only durable memory-optimized tables can be system-versioned (DURABILITY = SCHEMA_AND_DATA).  Queries that affect only the current table (in-memory) can be used in natively compiled T-SQL modules. Temporal queries using the FOR SYSTEM TIME clause are not supported in natively compiled modules. Use of the FOR SYSTEM TIME clause with memory-optimized tables in ad hoc queries and non-native modules is supported.  When SYSTEM_VERSIONING = ON, an internal memory-optimized staging table is automatically created to accept the most recent system-versioned changes that are results of update and delete operations on memory- optimized current table.  Data from the internal memory-optimized staging table is regularly moved to the disk-based history table by the asynchronous data flush task. This data flush mechanism has a goal to keep the internal memory buffers at less than 10% of the memory consumption of their parent objects. Hybrid Transactional/Analytical Processing (HTAP)
  • 26. Demonstration 5 Creating Temporal Table in SQL Server 2016 In-Memory Table
  • 27. Thank you so much!! Questions???
  • 28. Email - arthurjosemberg@gmail.com Linkedin – Arthur Luz Twitter - @arthurjosemberg Skype - arthurjosemberg Blog – arthurluz.wordpress.com Contacts

Notas do Editor

  1. Lembrar que o versionamento dos registros s’ao realizados com ‘sysdatetimeoffset’
  2. Retorna uma tabela com os valores de todas as versões de linhas que estavam ativas dentro do intervalo de tempo especificado, independentemente de terem começado a ser ativas antes do valor do parâmetro <start_date_time> para o argumento FROM ou terem deixado de estar ativas após o valor do parâmetro <end_date_time> TO.
  3. O mesmo que acima na descrição do FOR SYSTEM_TIME FROM <start_date_time> TO <end_date_time>, exceto que a tabela de linhas retornada inclui linhas que se tornaram ativas no limite superior definido pelo nó de extremidade <end_date_time>.
  4. Retorna uma tabela com os valores para todas as versões de linha que foram abertas e fechadas dentro do intervalo de tempo especificado definido pelos dois valores de data e hora para o argumento CONTAINED IN.
  5. Do not perform massive deletes from current table in order to increase available RAM by cleaning up the space. Consider deleting data in multiple batches with manually invoked data flush in between by invoking sp_xtp_flush_temporal_history, or while SYSTEM_VERSIONING = OFF. Do not perform massive table updates at once as it can result in memory consumption that is twice the amount of memory required to update a non-termporal memory-optimized table. Doubled memory consumption is temporary because data flush task works regularly to keep memory consumption of internal staging table within projected boundaries in the steady state (around 10% of memory consumption of current temporal table). Consider doing massive updates in multiple batches or while SYSTEM_VERSIONING = OFF, such as using updates to set the defaults for newly added columns.