Bi-temporal rdbms 2014

Embed Size (px)

DESCRIPTION

An update to my original slideshow of about 2 years ago. I've added some refinements and (I hope) made the design process a little more clear.

Citation preview

  • 1. Adding Bi-Temporal Functionality to RDBMS

2. 2008-2014 2 References Dr. Richard Snodgrass Developing Time-Oriented Database Applications Temporal Data Types Kinds of Time (Instant, Interval, Period) Temporal Statements Current, Sequenced, Non-sequenced Non-temporal Temporal (Past, Current, Future) Tom Johnston Time & Time Again series 3. 2008-2014 3 Terminology Temporal: fancy-schmancy word for time Were entering the temporal anomaly, Captain! Time-varying table Temporally Insensitive Data Never changes Changes are ignored Temporally Sensitive Data Changes are tracked } static versioned 4. 2008-2014 4 Terminology Mono-temporal: tracking changes Versioning: transaction time is valid time Source code revision (SVN, Jit, CVS, etc.) Bi-Temporal Transaction: when did we record it? Effective: when did/will it happen? 5. 2008-2014 5 Terminology Version/Temporal Normal Form (vnf/tnf) Really? A new normalization pattern? Not really. Special case of 2nf. Differences and similarities 6. 2008-2014 6 What We Get All changes to TS data are tracked Look back queries Transaction time Valid/Effective time Database undo after commit Changes may be pre-inserted or pre-updated to take effect at a scheduled date and time 7. 2008-2014 7 Current State of the Art From Wikipedia: Slowly Changing Dimension (SCD). Data Warehouse Type 1: Do nothing Type 6: One version almost workable Two tables: current versions and past versions. Two dates: start and end (row spanning dependency). Lot of data changing for undo. Referential integrity: refer to current table. 8. 2008-2014 8 Characteristics of Temporal Design Logical / physical differences. The row is the entity The row is one version of the entity. Versions do not overlap. There cannot be gaps between versions. One and only one relative current version. 9. 2008-2014 9 Requirements All data (past/current/future) from the same source (no separate history tables). Normal referential integrity constraints used. All queries follow a consistent pattern. Only one date / time value (no TO/FROM pairs). NO separate field used to indicate current version. Updates write one version. No other DML required. All this in a transactional database without maintaining separate snapshot, history or audit tables, and with only minor impact on performance. 10. 2008-2014 10 1. There cannot be more than one version of the truth at any given time. In its simplest form, this means that any query that returns more than one version of the same entity is erroneous. 2. We cant change the past. This is a corollary of the previous and simply means that physical updates to versioned data should be prohibited. Versioning Rules 11. 2008-2014 11 Temporal Normal Form (tnf) How it is implemented 12. 2008-2014 12 Building up to tnf EmpNo (pk) FName LName HireDate PayRate Other Info 1001 Sally Jones 1998-10-10 15.35 1002 Sam Spade 1998-11-11 10.56 1004 Katherine Great 1998-12-12 9.00 Figure1: Simple Non-versioned Data EmpNo (pk) FName LName HireDate PayRate Other Info 1001 Sally Smith 1998-10-10 15.35 1002 Sam Spade 1998-11-11 10.56 1004 Katherine Great 1998-12-12 9.00 Figure 2: Jones becomes Smith option 1 EmpNo (pk) FName LName HireDate PayRate Other Info 1001 Sally Jones 1998-10-10 15.35 1002 Sam Spade 1998-11-11 10.56 1004 Katherine Great 1998-12-12 9.00 1005 Sally Smith 1998-10-10 15.35 Figure 2: Jones becomes Smith option 2 EmpNo (pk) Effective(pk) FName LName HireDate PayRate Other Info 1001 2010-06-19 Sally Jones 1998-10-10 15.35 1002 2007-01-01 Sam Spade 1998-11-11 10.56 1004 2008-02-26 Katherine Great 1998-12-12 9.00 1001 2012-01-01 Sally Smith 1998-10-10 15.35 Figure 4: Simple Versioned Data 13. 2008-2014 13 Temporal Normal Form EmpNo (pk) Created Deleted FName HireDate Other Info 1001 2008-10-01 9999-12-31 Sally 2008-10-10 1002 2008-11-01 9999-12-31 Sam 2008-11-11 1004 2008-12-01 9999-12-31 Katherine 2008-12-12 Employees Entity Table after TNF EmpNo (pk) Effective(pk) Created LName PayRate Other Info 1001 2008-10-10 2008-10-01 Jones 15.35 1002 2008-11-11 2008-11-01 Spade 10.56 1004 2008-12-12 2008-12-01 Great 9.00 Temporally Normalized (Versioned) Employee Data EmpNo (pk) Effective(pk) Created LName PayRate Other Info 1001 2008-10-10 2008-10-01 Jones 15.35 1002 2008-11-11 2008-11-01 Spade 10.56 1004 2008-12-12 2008-12-01 Great 9.00 1001 2012-01-01 2012-02-01 Smith 15.35 14. 2008-2014 14 CODE! declare @EmpNo int; insert into Employees( Created, Deleted, HireDate, FName, ... ) values( SysDate(), '9999-12-31', '2008-10-01', 'Sally', ... ); set @EmpNo = @@Identity; insert into EmployeeVersions( EmpNo, Effective, Created, LName, ... ) values( @EmpNo, '2008-10-01', SysDate(), 'Jones', ... ); Creating an entity (insert): Modifying entity data (update): insert into EmployeeVersions( EmpNo, Effective, LName, ... ) values( 1001, '2012-01-01', 'Smith', ... ); 15. 2008-2014 15 Temporal Normal Form EmpNo (pk) Created Deleted FName HireDate Other Info 1001 2008-10-01 9999-12-31 Sally 2008-10-10 1002 2008-11-01 9999-12-31 Sam 2008-11-11 1004 2008-12-01 9999-12-31 Katherine 2008-12-12 Employees Entity Table after TNF EmpNo (pk) Effective(pk) Created LName PayRate Other Info 1001 2008-10-10 2008-10-01 Jones 15.35 1002 2008-11-11 2008-11-01 Spade 10.56 1004 2008-12-12 2008-12-01 Great 9.00 Temporally Normalized (Versioned) Employee Data EmpNo (pk) Effective(pk) Created LName PayRate Other Info 1001 2008-10-10 2008-10-01 Jones 15.35 1002 2008-11-11 2008-11-01 Spade 10.56 1004 2008-12-12 2008-12-01 Great 9.00 1001 2012-01-01 2012-02-01 Smith 15.35 16. 2008-2014 16 Temporal Normal Form EmpNo (pk) Created Deleted FName HireDate Other Info 1001 2008-10-01 9999-12-31 Sally 2008-10-10 1002 2008-11-01 9999-12-31 Sam 2008-11-11 1004 2008-12-01 9999-12-31 Katherine 2008-12-12 Employees Entity Table after TNF EmpNo (pk) Effective(pk) Created LName PayRate Other Info 1001 2008-10-10 2008-10-01 Jones 15.35 1002 2008-11-11 2008-11-01 Spade 10.56 1004 2008-12-12 2008-12-01 Great 9.00 Temporally Normalized (Versioned) Employee Data EmpNo (pk) Effective(pk) Created LName PayRate Other Info 1001 2008-10-10 2008-10-01 Jones 15.35 1002 2008-11-11 2008-11-01 Spade 10.56 1004 2008-12-12 2008-12-01 Great 9.00 1001 2012-01-01 2012-02-01 Smith 15.35 17. 2008-2014 17 Version Code! Query: EmpNo Effective FName LName PayRate HireDate Other Info 1001 2010-06-19 Sally Jones 15.35 2008-10-10 1001 2012-01-01 Sally Smith 15.35 2008-10-10 1002 2011-01-01 Sam Spade 10.56 2008-11-11 1004 2011-02-26 Katherine Great 9.00 2008-12-12 Figure 5: Simple Join Query All Rows Select e.EmpNo, ev.Effective, e.FName, ev.Lname, e.HireDate, ev.PayRate, ... from Employees e join EmployeeVersions ev on e.EmpNo = ev.EmpNo where e.Deleted > :AsOfDate and ev.Effective = ( select Max( Effective ) from EmployeeVersions ev1 where ev1.EmpNo = e.EmpNo and ev1.Effective