La presentazione è in caricamento. Aspetta per favore

La presentazione è in caricamento. Aspetta per favore

WPC018 Supporto ai database temporali in SQL Server 2016

Presentazioni simili


Presentazione sul tema: "WPC018 Supporto ai database temporali in SQL Server 2016"— Transcript della presentazione:

1 WPC018 Supporto ai database temporali in SQL Server 2016
Gianluca Hotz - UGISS

2 Gianluca Hotz Fondatore e Mentor SolidQ Interessi
20 anni con SQL Server (dalla 4.21 nel 1996) Modellazione basi di dati, dimensionamento e amministrazione, sviluppo, ottimizzazione Interessi Modello relazionale, architettura DBMS, alta disponibilità e Disaster Recovery Microsoft MVP SQL Server dal 1998 Fondatore e presidente UGISS User Group Italiano SQL Server (PASS Chapter) Gianluca Hotz

3 Agenda Introduzione Tabelle temporali in SQL Server Scenari Futuro

4 Introduzione Supporto ai database temporali in SQL Server 2016

5 Cos’é un database? Collezione strutturata di fatti
sottoinsieme di fatti: del mondo reale, di interesse Rappresenta proposizioni considerate vere assiomi Permette di derivare nuove proposizioni tramite regole formali di inferenza

6 Database e temporalità
Database convenzionale Codifica solo dati correnti Dati modificati non appena proposizioni cambiano Dati eliminati non appena proposizioni cessano di essere vere Database temporale Codifica anche dati storici (passati e futuri) Nessuna modifica o eliminazione definitiva dei dati

7 Dati senza temporalità
Le righe sono proposizioni istanziate dal predicato: Un fornitore identificato da <S#>, con nome <SNAME>, con sede nella città <CITY>, ha un contratto in corso (correntemente). S (fornitore) S# SNAME CITY S1 Gianluca Varese S2 Davide Milano S3 Andrea Brescia

8 Problema dati non temporali
Non sappiamo quando è iniziato il contratto Non sappiamo quando termina il contratto Se il contratto è già terminato… …non sappiamo quando è finito! …la proposizione viene eliminata… …non sapremo neppure che è stato a contratto! Non posso registrare un contratto futuro

9 Semi Temporalizzazione
Le righe sono proposizioni istanziate dal predicato: Un fornitore identificato da <S#>, con nome <SNAME>, con sede nella città <CITY>, ha un contratto in corso dal <SINCE>. S_SINCE (fornitore) S# SNAME CITY SINCE S1 Gianluca Varese D04 S2 Davide Milano D07 S3 Andrea Brescia D03

10 Problemi dati semi temporali
Sappiamo quando è iniziato il contratto Non sappiamo quando termina il contratto Se il contratto è già terminato… …non sappiamo quando è finito! …la proposizione viene eliminata… …non sapremo neppure che è stato a contratto! Posso registrare un contratto futuro

11 Temporalizzazione completa
Le righe sono proposizioni istanziate dal predicato: Un fornitore identificato da <S#>, con nome <SNAME>, con sede nella città <CITY>, ha (o ha avuto o avrà) un contratto in corso dal <FROM> al <TO>. S_DURING (fornitore) S# SNAME CITY FROM TO S1 Gianluca Varese D04 D10 S2 Davide Milano D07 S3 Andrea Brescia D03

12 Problemi dati completamente temporali
Due attributi per indicare gli estremi dell’intervallo Espressioni complesse per filtri o operazioni relazionali Definizione dei vincoli Chiave primaria Dati ridondanti o contradditori in periodi sovrapposti Circonlocuzione Dati ripetuti in periodi immediatamente consecutivi Chiavi esterne Vincolo di integrità deve comprendere il periodo

13 Bi Temporalità Stated Time (o Valid/Application Time)
Si riferisce a quando crediamo che qualcosa sia/sia stato/sarà vero secondo quanto sappiamo ora Specificato da utente/applicazione Permette di gestire anche periodi futuri Logged Time (o Transaction/System Time) Si riferisce a quando il database ha registrato qualcosa come vero Gestito direttamente dal sistema Permette di gestire solo presente e passato

14 Bi Temporalizzazione Le righe sono proposizioni istanziate dal predicato: Un fornitore identificato da <S#>, con nome <SNAME>, con sede nella città <CITY>, ha (o ha avuto o avrà) un contratto in corso dal <FROM> al <TO>. Si ritengono vere nel periodo da <S_FROM> a <S_TO>. S_DURING_BI (fornitore) S# SNAME CITY FROM TO S_FROM S_TO S1 Gianluca Varese D02 D10 D01 S2 Davide Milano D07 D99 S3 Andrea Brescia D03 D04

15 Tabelle temporali Supporto ai database temporali in SQL Server 2016

16 «Temporal Table» in SQL Server 2016
«System-versioned Temporal Table» Permette di gestire valori correnti e storici Periodo validità definito da due colonne di tipo datetime2 aggiornato dal sistema a fronte di modifiche Implementata tramite due tabelle fisiche «Current Table» per dati correnti «History Table» per dati storici (modificati) Schema speculare a «Current Table»

17 Scenari obiettivo «Workaround» Funzionalità Complessi Limitati
Time Travel Data Audit «Workaround» Complessi Limitati Inefficienti Funzionalità Estensioni DML/DDL ANSI SQL 2011 Slowly Changing Dimensions Repair record-level corruptions Data audit: Turn on system versioning for tables that store critical information where you need to track changes, when, and by which user. Using Temporal Tables will allow you to perform a data audit transparently (without breaking existing applications) and to perform efficient data forensics at any point in time. Time travel: Use Temporal Tables to reconstruct the state of your data at any point in time in the past, compare data from multiple moments side-by-side, and perform trend analysis for important business indicators. Utilize new temporal querying capabilities to review business reports as they were in the past without changing the report. Zoom in for business analysis at a specific point in time naturally using the built-in querying capabilities. Slowly changing dimensions: Keep a history of dimension attribute changes in the data warehouse model without additional ETL code. Repair record-level corruptions: Perform the finest level of data repair in case of accidental corruption made either by a human or application. The affected portion of data can be restored to “last good known state” very precisely, without dealing with backups and introducing downtime to your application

18 Come iniziare con i dati temporali
Microsoft Ignite 2015 11/25/2017 4:17 PM Come iniziare con i dati temporali Nessun cambio modello di programmazione Nuovi «Insights» FOR SYSTEM_TIME AS OF FROM..TO BETWEEN..AND CONTAINED IN Temporal Querying CREATE temporal TABLE PERIOD FOR SYSTEM_TIME… ALTER regular_table TABLE ADD PERIOD… DDL INSERT / BULK INSERT UPDATE DELETE MERGE DML SELECT * FROM temporal Querying Source: 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 – 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 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. Performance © 2015 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

19 Funzionamento modifiche
Microsoft Ignite 2015 11/25/2017 4:17 PM Funzionamento modifiche «Temporal table» (dati correnti) «History table» * Versioni vecchie Source: The SYSTEM_TIME period columns used to record the SysStartTime and SysEndTime values must be defined with a datatype of datetime2. Update */ Delete * Insert / Bulk Insert Performance © 2015 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

20 Funzionamento interrogazioni
Microsoft Ignite 2015 11/25/2017 4:17 PM Funzionamento interrogazioni «Temporal table» (dati correnti) «History table» * Include versioni storiche Source: The SYSTEM_TIME period columns used to record the SysStartTime and SysEndTime values must be defined with a datatype of datetime2. Query normali (dati correnti) Query temporali * FOR SYSTEM_TIME ALL, AS OF, BETWEEN … AND …, FROM … TO, CONTAINED IN Performance © 2015 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

21 Demo

22 Scenari Supporto ai database temporali in SQL Server 2016

23 Gestione «Retention» Stretch Database Partizionamento «History Table»
Script di pulizia personalizzato

24 «Stretch Database» Azure SQL Database SELECT * FROM Department
Microsoft Ignite 2015 11/25/2017 4:17 PM «Stretch Database» Considerazioni: Storico più voluminoso dati correnti Dati conservati da 3 a 10 anni «Caldi»: fino a poche settimane/mesi «Freddi»: interrogati raramente SELECT * FROM Department FOR SYSTEM_TIME AS OF ' ' Soluzione: Storico come «stretch table»: PeriodEnd < “Now - 6 months” Azure SQL Database Source: Example of using temporal tables with Azure SQL Database stretch tables. © 2015 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

25 Demo

26 Partizionamento «History Table»
SWITCH OUT MERGE RANGE SPLIT RANGE Image from

27 Script di pulizia personalizzato
Image from

28 Interoperabilità con «In-Memory OLTP»
Image from

29 Futuro Supporto ai database temporali in SQL Server 2016

30 Limitazioni Tabelle non possono essere FILETABLE
No colonne tipo FILESTREAM No query temporali via «Linked Server» «History Table» nello stesso database «History Table» non può avere vincoli INSERT e UPDATE non possono referenziare colonne periodo TRUNCATE TABLE richiede disabilitazione «versionamento» Dati «History Table» non modificabili direttamente ON [DELETE|UPDATE] CASCADE non permessi INSTEAD OF trigger non permessi AFTER trigger permessi solo su «Current Table» CDC e CDT supportati solo con «Current Table» Altri punti di attenzione…

31 Implementazioni «Valid/Application/Stated Time»
Periodo rappresentato tramite intervallo? Operatori relazionali generici? PACK/UNPACK Operatori di Allen Valutare IntervalCID Operatore Notazione Definizione Equals (i1 = i2) (b1 = b2) AND (e1 = e2) Before (i1 before i2) (e1 < b2) After (i1 after i2) (i2 before i1) Includes (i1 ⊇ i2) (b1 ≤ b2) AND (e1 ≥ e2) Properly Includes (i1 ⊃ i2) (i1 ⊇ i2) AND (i1 ≠ i2) Meets (i1 meets i2) (b2 = e1 + 1) OR (b1 = e2 +1) Overlaps (i1 overlaps i2) (b1 ≤ e2) AND (b2 ≤ e1) Merges (i1 merges i2) (i1 overlaps i2) OR (i1 meets i2) Begins (i1 begins i2) (b1 = b2) AND (e1 ≤ e2) Ends (i1 ends i2) (e1 = e2) AND (b1 ≥ b2)

32 Risorse - Presentazioni
UGISS Workshops Database & Time-Dependent Data - Parte 1 Database & Time-Dependent Data - Parte 2 Disponibili demo e Screencast SQL Conference Temporal Database Introduction Pluralsight Working With Temporal Data in SQL Server

33 Risorse - Libri teoria Temporal Data and The Relational Model
C.J.Date, Hugh Darwen, Nikos A. Lorentzos ISBN Date on Database Writings C.J.Date ISBN Un capitol con critica a standard proposto TSQL2 Time and Relational Theory: Temporal Databases in the Relational Model and SQL ISBN Completamente riscritto (include standard SQL 2011)

34 Risorse - Libri implementazione
Inside Microsoft SQL Server 2008: T-SQL Programming Itzik Ben-Gan, Dejan Sarka, Greg Low et al ISBN Capitolo su dati temporali, UDT basato su CLR e intervalli Developing Time-Oriented Database Applications in SQL Richard T. Snodgrass ISBN Fuori stampa, download gratuito: Joe Celko's SQL for Smarties: Advanced SQL Programming Joe Celko ISBN Capitoli su dati temporali e interrogazioni

35 Risorse - Articoli First Look at System-Versioned Temporal Tables-Part 1: Creating Tables and Modifying Data First Look at System-Versioned Temporal Tables-Part 2: Querying Data and Optimization Considerations Temporal Tables (MSDN Interval Queries in SQL Server

36 Domande e Risposte Q&A

37 Corsi consigliati MOC10986B Updating Your Skills to SQL Server 2016
MOC20762A Developing SQL Databases Corsi consigliati

38 Contatti OverNet Education
Tel @overnete Contatti OverNet Education


Scaricare ppt "WPC018 Supporto ai database temporali in SQL Server 2016"

Presentazioni simili


Annunci Google