Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Temporal Databases: Queries

2,009 views

Published on

An overview of typical queries on a temporal database, e.g., temporal natural join, temporal coalescing, or temporal set operators. Examples are provided using valid-time, transaction-time and bitemporal databases.

Published in: Technology
  • DOWNLOAD FULL BOOKS, INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/yxufevpm } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/yxufevpm } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/yxufevpm } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/yxufevpm } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/yxufevpm } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/yxufevpm } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Temporal Databases: Queries

  1. 1. Temporal Databases: Queries Kristian Torp Department of Computer Science Aalborg University people.cs.aau.dk/˜torp torp@cs.aau.dk November 2, 2015 daisy.aau.dk Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 1 / 122
  2. 2. Outline 1 Overview and Foundation Current, Valid Time, Transaction Time, and Bitemporal Overlap Between Intervals 2 Temporal Join 3 Aggregation 4 Coalescing 5 Set Operators 6 Timeslice Valid Time Bitemporal 7 Summary Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 2 / 122
  3. 3. Learning Goals Learning Goals Understand the differences between valid time and transaction time Understand bitemporal support Temporal queries: aggregation, join, and timeslicing Temporal interval and overlap Note The four-timestamp data model used is widely supported n person will make at least n + 1 temporal data models Words period and interval are central And switched English versus SQL Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 3 / 122
  4. 4. Outline 1 Overview and Foundation Current, Valid Time, Transaction Time, and Bitemporal Overlap Between Intervals 2 Temporal Join 3 Aggregation 4 Coalescing 5 Set Operators 6 Timeslice Valid Time Bitemporal 7 Summary Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 4 / 122
  5. 5. Outline 1 Overview and Foundation Current, Valid Time, Transaction Time, and Bitemporal Overlap Between Intervals 2 Temporal Join 3 Aggregation 4 Coalescing 5 Set Operators 6 Timeslice Valid Time Bitemporal 7 Summary Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 5 / 122
  6. 6. Bitemporal Graphs: Explicit Valid Time TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac Bill, Prozac Bill, Prozac Bill, Morphine Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 6 / 122
  7. 7. Temporal Data: Tabular Overview pname medicine Bill Prozac Bill Morphine pname medicine vts vte tts tte Bill Prozac 1 6 1 5 Bill Prozac 1 13 5 8 Bill Prozac 1 9 8 UC Bill Morphine 10 15 2 UC Current Bitemporal pname medicine vts vte Bill Prozac 1 9 Bill Morphine 10 15 pname medicine tts tte Bill Prozac 1 5 Bill Prozac 5 8 Bill Prozac 8 UC Bill Morphine 2 UC Valid Time Transaction Time Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 7 / 122
  8. 8. Bitemporal Graphs: Implicit Valid Time TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, Shoe Joe, Shoe Joe, Toy Joe, Toy Joe, Shoe Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 8 / 122
  9. 9. Overview: Tabular Form ename dept Joe Shoe ename dept vts vte tts tte Joe Shoe 1 now 1 5 Joe Shoe 1 5 5 UC Joe Toy 5 now 5 10 Joe Toy 5 10 10 UC Joe Shoe 10 now 10 UC Current Bitemporal ename dept vts vte Joe Shoe 1 5 Joe Toy 5 10 Joe Shoe 10 now ename dept tts tte Joe Shoe 1 5 Joe Toy 5 10 Joe Shoe 10 UC Valid Time Transaction Time Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 9 / 122
  10. 10. now in Real Life Examples of now in data Marriage certificate Hiring period Permanent salary benefits How long you have owned your car/house/dog Office room allocation Sickness period Note When end-date is uncertain/unknown/unspecified use now Interpretation of now different valid time and transaction time Also called until changed (UC) for transaction time Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 10 / 122
  11. 11. Valid Time or Transaction Time? What do you think? Reservation time this room this seminar? The appointments in your calendar? The timestamps in a GPS log from a vehicle? The positions that you have held listed on your CV? File modification timestamp? TV commercial: “Offer only valid next week!” Web log? Mortgage loan period? Diary? Bank day/date (tricky)? Birthday (tricky)? Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 11 / 122
  12. 12. Outline 1 Overview and Foundation Current, Valid Time, Transaction Time, and Bitemporal Overlap Between Intervals 2 Temporal Join 3 Aggregation 4 Coalescing 5 Set Operators 6 Timeslice Valid Time Bitemporal 7 Summary Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 12 / 122
  13. 13. Allen’s Operators 0 1 2 3 4 5 6 7 8 9 10 11 12 Y Y Y Y Y Y Y Y Y Y Y Y Y X Note 13 cases are complete How intervals can overlap The symmetries Half-open/closed intervals Does not matter! All cases have names Some of them are weird J.F.Allen CACM 26(11), Nov. 1983 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 13 / 122
  14. 14. Simplified Overlap Operators Example (Allen) 0 1 2 3 4 5 6 7 8 9 10 11 12 Y Y Y Y Y Y Y Y Y Y Y Y Y X Example (Simplified) 0 1 2 3 4 5 6 7 8 9 10 11 12 Y Y Y Y Y Y X Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 14 / 122
  15. 15. Names Simplified Overlap Operators Example (Naming) 0 1 2 3 4 5 6 7 8 9 10 11 12 Before After Left Overlaps During Right Overlaps Total Overlaps X Note These names are not official just used for reference in this presentation. Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 15 / 122
  16. 16. Five Cases Simplified Overlap Operators Example (The Cases) 0 1 2 3 4 5 6 7 8 9 10 11 12 Y Y Y Y Y Y X The Boolean Expressions Case Expression No Y.vte ≤ X.vts or X.vte ≤ Y.vts Right X.vts < Y.vts and Y.vts < X.vte Left X.vts < Y.vte and Y.vte < X.vte Full Y.vts ≤ X.vts and X.vte ≤ X.vte Do intervals overlap? X.vts < Y.vte AND Y.vts < X.vte Get Overlap [greatest(X.vte, Y.vte), least(X.vts, Y.vts)) provided that the intervals X and Y overlap Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 16 / 122
  17. 17. Query Types: Current, Sequence, and Non-Sequenced Note Current state for backwards compatibility Sequenced for built-in temporal semantic/support Non-sequenced for accessing timestamps columns directly Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 17 / 122
  18. 18. Valid Time versus Transaction Time Exercise In groups, use 3 minutes to compare valid time and transaction time. Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 18 / 122
  19. 19. Valid Time versus Transaction Time Exercise In groups, use 3 minutes to compare valid time and transaction time. Valid Time Past, present, and future User supplied Supports now Physical delete “History correction” Transaction Time Past and present System supplied Supports now Logical delete History preserving (log) Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 18 / 122
  20. 20. Valid Time versus Transaction Time Exercise In groups, use 3 minutes to compare valid time and transaction time. Valid Time Past, present, and future User supplied Supports now Physical delete “History correction” Transaction Time Past and present System supplied Supports now Logical delete History preserving (log) Valid Time for sure if: Time is into the future Time can be supplied by the user Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 18 / 122
  21. 21. Summary: Motivation I Valid, Transaction, and User-Defined Time Valid time and transaction time are orthogonal Valid time: Past, present, and future Can be user-defined (default similar to transaction time) Transaction time: Past and present Always system defined User-defined time (if not valid time or transaction time) e.g., birthday Query Types Current Sequenced Non-sequenced Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 19 / 122
  22. 22. Summary: Motivation II Database Variables Special variable now Different semantics valid time and transaction time Databases do not support variables now = 9999-12-31 (maximum value of date domain) Intervals Timestamps are half-open intervals Closed intervals +/- 1 therefore more complicated Overlap between intervals is essential to understand Not difficult 5 cases to consider Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 20 / 122
  23. 23. Outline 1 Overview and Foundation Current, Valid Time, Transaction Time, and Bitemporal Overlap Between Intervals 2 Temporal Join 3 Aggregation 4 Coalescing 5 Set Operators 6 Timeslice Valid Time Bitemporal 7 Summary Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 21 / 122
  24. 24. The Idea: One Example (Emp) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Joe, HR Kim, HR Example (Dept) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 HR, 10 Example (Emp Temporal Natural Join Dept) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Joe, HR, 10 Kim, HR, 10 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 22 / 122
  25. 25. The Idea: Two Example (Emp) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Joe, HR Kim, HR Example (Dept) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 HR, 10 HR, 20 Example (Emp Temporal Natural Join Dept) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Joe, HR, 10 Kim, HR, 10 Kim, HR, 20 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 23 / 122
  26. 26. The Idea: Three Example (Emp) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Joe, HR Kim, HR Example (Dept) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 HR, 5 Example (Emp Temporal Natural Join Dept) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 24 / 122
  27. 27. The Idea: Four Example (Emp) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Joe, HR Kim, RD Example (Dept) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 HR, 10 RD, 20 Example (Emp Temporal Natural Join Dept) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Joe, HR, 10 Kim, RD, 10 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 25 / 122
  28. 28. Natural Join Informal Definition Idea Temporal Natural Join The explicit attribute must be equal The intersection of the timestamp intervals must be non-empty The Overlap Cases to Consider 0 1 2 3 4 5 6 7 8 9 10 11 12 Before After Left Overlaps During Right Overlaps Total Overlaps X Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 26 / 122
  29. 29. Temporal Natural-Join Example select cname , adr , ptype , p . vts , p . vte −− t o t a l overlap from c u s t v t as c , property vt as p where c . cid = p . cid and c . vts <= p . vts and p . vte <= c . vte union select cname , adr , ptype , c . vts , p . vte −− r i g h t overlap from c u s t v t as c , property vt as p where c . cid = p . cid and c . vts between p . vts and p . vte and c . vte > p . vte union select cname , adr , ptype , p . vts , c . vte −− l e f t overlap from c u s t v t as c , property vt as p where c . cid = p . cid and c . vte between p . vts and p . vte and c . vts < p . vts union −− during select cname , adr , ptype , c . vts , c . vte from c u s t v t as c , property vt as p where c . cid = p . cid and c . vts between p . vts and p . vte and c . vte between p . vts and p . vte Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 27 / 122
  30. 30. Quiz: What is the Natural-Join Result VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Bill, support Pam, sale Pam, RD Pam, RD Sam, RD Jim, RD Jim, sale Jim, RD Tim, web Tim, sale Tom, sale Tom, test Joe, RD Joe, sale Joe, RD VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 RD, 50 RD, 60 RD, 80 sale, 10 sale, 20 sale, 25 web, 5 test, 20 test, 15 support, 30 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 28 / 122
  31. 31. Summary: Temporal Join Main Points Temporal natural join most widely used Like in non-temporal databases Valid-time natural-join complicated because multiple overlap scenarios to consider It is possible to join a valid-time table with a non-temporal table On all rows in the non-temporal table the valid-time is [-forever, forever) Note Efficiency may be a problem Can you see why? Temporal versions of Cartesian product, outer joins, and equi join are well-defined See www.cs.arizona.edu/projects/stagg/papers/TR-71.pdf Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 29 / 122
  32. 32. Outline 1 Overview and Foundation Current, Valid Time, Transaction Time, and Bitemporal Overlap Between Intervals 2 Temporal Join 3 Aggregation 4 Coalescing 5 Set Operators 6 Timeslice Valid Time Bitemporal 7 Summary Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 30 / 122
  33. 33. The Data Foundation Query in English Find the number of persons employed over time and in which intervals. Example VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Joe, 10 Joe, 12 Joe, 13 Jim, 12 Jim, 14 Jill, 10 Jill, 15 Pat, 9 Pat, 9 Pat, 10 Bill, 9 Boris, 9 Pam, 11 Kurt, 11 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 31 / 122
  34. 34. Constant Regions Example VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Joe, 10 Joe, 12 Joe, 13 Jim, 12 Jim, 14 Jill, 10 Jill, 15 Pat, 9 Pat, 9 Pat, 10 Bill, 9 Boris, 9 Pam, 11 Kurt, 11 Note Vertical lines at all vts and vte values Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 32 / 122
  35. 35. The Result VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Joe, 10 Joe, 12 Joe, 13 Jim, 12 Jim, 14 Jill, 10 Jill, 15 Pat, 9 Pat, 9 Pat, 10 Bill, 9 Boris, 9 Pam, 11 Kurt, 11 cnt vts vte 3 1 3 5 3 5 5 5 6 6 6 7 5 7 11 4 11 13 5 13 14 5 14 16 6 16 17 5 17 18 6 18 19 5 19 21 5 21 23 4 23 24 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 33 / 122
  36. 36. Main Idea in Finding Constant Regions Five cases to consider No other interval overlaps me Another interval has vts between my vts and vte Another interval has vte between my vts and vte Another interval totally overlaps me Another interval is after me Note For each case there can be no vts or vte between returned result A single interval can qualify for more than one of the cases above SQL is declarative therefore ”after” could be ”before” But just one of the cases can be considered Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 34 / 122
  37. 37. Main Query Example (The Main Query) select count (ename) as cnt , const regions . vts as vts , const regions . vte as vte from emp vt , const regions where emp vt . vts < const regions . vte and const regions . vts < emp vt . vte group by const regions . vts , const regions . vte order by vts , vte Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 35 / 122
  38. 38. Constant Regions Part 1: No Overlap with const regions as ( −− get t1 . vts and t1 . vte select t1 . vts as vts , t1 . vte as vte from emp vt t1 where not exists ( select ∗ from emp vt t3 where ( t1 . vts < t3 . vts and t3 . vts < t1 . vte ) or ( t1 . vts < t3 . vte and t3 . vte < t1 . vte ) ) Example VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 result = t1 not exists t3 not exists t3 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 36 / 122
  39. 39. Constant Regions Part 2: union −− t1 . vts and t2 . vts select t1 . vts as vts , t2 . vts as vte from emp vt t1 , emp vt t2 where t1 . vts < t2 . vts and t2 . vts < t1 . vte and not exists ( select ∗ from emp vt t3 where ( t1 . vts < t3 . vts and t3 . vts < t2 . vts ) or ( t1 . vts < t3 . vte and t3 . vte < t2 . vts ) ) Example VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 t1 t2 result Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 37 / 122
  40. 40. Constant Regions Part 3: union −− t1 . vts and t2 . vte select t1 . vts as vts , t2 . vte as vte from emp vt t1 , emp vt t2 where t1 . vts < t2 . vte and t2 . vte < t1 . vte and not exists ( select ∗ from emp vt t3 where ( t1 . vts < t3 . vts and t3 . vts < t2 . vte ) or ( t1 . vts < t3 . vte and t3 . vte < t2 . vte ) ) Example VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 t1 t2 result Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 38 / 122
  41. 41. Constant Regions Part 4: Before union −− t1 . vte and t2 . vts select t1 . vte as vts , t2 . vts as vte from emp vt t1 , emp vt t2 where t1 . vte < t2 . vts and not exists ( select ∗ from emp vt t3 where ( t1 . vte < t3 . vts and t3 . vts < t2 . vts ) or ( t1 . vte < t3 . vte and t3 . vte < t2 . vts ) ) Example VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 t1 t2 result Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 39 / 122
  42. 42. Constant Regions Part 5: union −− t1 . vte and t2 . vte select t1 . vte as vts , t2 . vte as vte from emp vt t1 , emp vt t2 where t2 . vts < t1 . vte and t1 . vte < t2 . vte and not exists ( select ∗ from emp vt t3 where ( t1 . vte < t3 . vts and t3 . vts < t2 . vte ) or ( t1 . vte < t3 . vte and t3 . vte < t2 . vte ) ) ) Example VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 t1 t2 result Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 40 / 122
  43. 43. Correctness of Constant Regions Five = Original row + Four Combinations [t1.vts, t1.vte) [t1.vts, t2.vts) [t1.vts, t2.vte) [t2.vte, t2.vts) [t2.vte, t2.vte) Note All cases are covered Informal argument Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 41 / 122
  44. 44. Quiz Query in English Find the constant regions and average budget for all departments over time. Example (Data Foundation) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 RD, 50 RD, 60 RD, 80 sale, 10 sale, 20 sale, 25 web, 5 test, 20 test, 15 support, 30 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 42 / 122
  45. 45. Quiz: Solution, One Example (Data Foundation with Dividers) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 RD, 50 RD, 60 RD, 80 sale, 10 sale, 20 sale, 25 web, 5 test, 20 test, 15 support, 30 Rest of Solution Now we have the constant regions Next step is to do the aggregation within each region Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 43 / 122
  46. 46. Quiz: Solution, Two cnt vts vte 50.00 1 4 30.00 4 6 21.67 6 8 21.25 8 10 26.67 10 11 33.33 11 15 33.75 15 16 32.50 16 17 37.50 17 22 45.00 22 24 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 50.00 30.00 21.67 21.25 26.67 33.33 33.75 32.50 37.50 45.00 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 44 / 122
  47. 47. Summary: Aggregation Main Points Constant regions must be identified Can be done in multiple ways Approach used is consider efficient (allowed to prove me wrong :-)) Due to various ways intervals may overlap the queries are long Same approach to average, count, minimum, and so on Note The approach also works if grouping on non-temporal column It is an exercise to figure-out how! Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 45 / 122
  48. 48. Outline 1 Overview and Foundation Current, Valid Time, Transaction Time, and Bitemporal Overlap Between Intervals 2 Temporal Join 3 Aggregation 4 Coalescing 5 Set Operators 6 Timeslice Valid Time Bitemporal 7 Summary Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 46 / 122
  49. 49. Coalescing in Figures Example (How long have Lars and Peter been with us?) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Lars, web Lars, web Lars, web Lars, test Lars, test Peter, web Peter, test Peter, test Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 47 / 122
  50. 50. Coalescing in Figures Example (How long have Lars and Peter been with us?) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Lars, web Lars, web Lars, web Lars, test Lars, test Peter, web Peter, test Peter, test Example (Result) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Lars Peter Note Select just the employee name Coalescing resulted in fewer rows Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 47 / 122
  51. 51. Coalescing in Figures, cont Example (How long have the ’web’ and ’test’ projects been active?) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Lars, web Lars, web Lars, web Lars, test Lars, test Peter, web Peter, test Peter, test Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 48 / 122
  52. 52. Coalescing in Figures, cont Example (How long have the ’web’ and ’test’ projects been active?) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Lars, web Lars, web Lars, web Lars, test Lars, test Peter, web Peter, test Peter, test Example (Result) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 test web web Note Coalescing gives better overview Coalescing gives new insight Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 48 / 122
  53. 53. Example: Valid-Time State Data Model Example (State and Event) pname vts vte Bush 1989 1993 Clinton 1993 1997 Clinton 1997 2001 Bush jr. 2001 2005 Bush jr. 2005 2009 Obama 2009 2013 Obama 2013 2017 pname vts vte Bush 1989 1993 Clinton 1993 2001 Bush jr. 2001 2009 Obama 2009 2017 Questions Do the tables have the same content? Does it have anything to do with duplicate elimination? What can you say about primary keys (tricky)? Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 49 / 122
  54. 54. Example: Event Data Model Example (Similar Content) pname vts vte Bush 1989 1993 Clinton 1993 1997 Clinton 1997 2001 Bush jr. 2001 2005 Bush jr. 2005 2009 Obama 2009 2013 Obama 2013 2017 pname vts Bush 1989 Clinton 1993 Clinton 1997 Bush jr. 2001 Bush jr. 2005 Obama 2009 Obama 2013 pname vts Bush 1989 Clinton 1993 Bush jr. 2001 Obama 2009 Questions Any implicit assumptions for state or event table? Future data any differences? What are the pros/cons state versus event table? Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 50 / 122
  55. 55. Quiz: Coalescing Questions How much space can be saved coalescing temporal data? Informal argument using examples! Can data in valid-time event tables be un-coalesced? Informal argument using examples! Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 51 / 122
  56. 56. Quiz: Coalescing Questions How much space can be saved coalescing temporal data? Informal argument using examples! Can data in valid-time event tables be un-coalesced? Informal argument using examples! Answers The saving coalescing the data stored can be significant. Consider many overlaps of value equivalent rows. Event-tables can also be un-coalesced. Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 51 / 122
  57. 57. Advantages and Disadvantages Coalesced Minimal space Row delete info. lost Queries faster Less data Modifications slower Many checks Un-coalesced Additional space Possibly much larger Queries slower More data Modifications faster Less checking Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 52 / 122
  58. 58. Coalescing in SQL (Proposed by Celko/Bohlen) select d i s t i n c t f i . ename, f i . vts , la . vte from projemp vt f i , projemp vt la where f i . vts < la . vte and f i . ename = la . ename and not exists ( −− ensure no holes and extend towards f i . vts select ∗ from projemp vt mi where mi . ename = f i . ename and f i . vts < mi . vts and mi . vts < la . vte and not exists ( select ∗ from projemp vt a1 where a1 . ename = f i . ename and a1 . vts < mi . vts and mi . vts <= a1 . vte ) ) and not exists ( −− ensure only maximal periods select ∗ from projemp vt a2 where a2 . ename = f i . ename and ( a2 . vts < f i . vts and f i . vts <= a2 . vte or a2 . vts <= la . vte and la . vte < a2 . vte ) ) order by f i . ename, f i . vts Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 53 / 122
  59. 59. Quiz: Coalesced? Example (Is the data coalesced?) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Joe, Shoe Joe, Shoe Joe, Toy Tom, Shoe Tom, Shoe Tom, Toy Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 54 / 122
  60. 60. Quiz: Coalesced? Example (Is the data coalesced?) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Joe, Shoe Joe, Shoe Joe, Toy Tom, Shoe Tom, Shoe Tom, Toy Example (Is the data coalesced?) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Joe, Shoe Joe, Shoe Joe, Toy Tom, Shoe Tom, Toy Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 54 / 122
  61. 61. Questions Related to Coalesced Questions Can coalescing increase the number of rows in table? What happens if you coalesce a table/query twice? If you make a projection on a coalesce table is the result coalesced? If you make a selection on a coalesce table is the result coalesced? Are ename and vts a primary key of a coalesced table? Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 55 / 122
  62. 62. Summary: Coalescing Main Points Similar to duplicate elimination also called fold/unfold Coalesced = uncoalesed is domain specific Follow the data Can data be coalesced without information loss? Note Avoid making coalescing into “politics” or “religion” Some persons have strong feelings for/against coalescing Coalescing is an expensive operation to perform It is related to duplicate elimination Nothing to do with the coalesce function Do you know what this function does? Additional information on coalescing timecenter.cs.aau.dk/TimeCenterPublications/TR-9.pdf Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 56 / 122
  63. 63. Outline 1 Overview and Foundation Current, Valid Time, Transaction Time, and Bitemporal Overlap Between Intervals 2 Temporal Join 3 Aggregation 4 Coalescing 5 Set Operators 6 Timeslice Valid Time Bitemporal 7 Summary Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 57 / 122
  64. 64. Overview Example (One) VT 0 1 2 3 4 5 6 7 8 9 10 R S R ∪ S R ∩ S VT 0 1 2 3 4 5 6 7 8 9 10 R S R − S S − R Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 58 / 122
  65. 65. Overview Example (One) VT 0 1 2 3 4 5 6 7 8 9 10 R S R ∪ S R ∩ S VT 0 1 2 3 4 5 6 7 8 9 10 R S R − S S − R Example (Two) VT 0 1 2 3 4 5 6 7 8 9 10 R S R ∪ S R ∩ S VT 0 1 2 3 4 5 6 7 8 9 10 S R − S S − R Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 58 / 122
  66. 66. Overview Example (One) VT 0 1 2 3 4 5 6 7 8 9 10 R S R ∪ S R ∩ S VT 0 1 2 3 4 5 6 7 8 9 10 R S R − S S − R Example (Two) VT 0 1 2 3 4 5 6 7 8 9 10 R S R ∪ S R ∩ S VT 0 1 2 3 4 5 6 7 8 9 10 S R − S S − R Note Think Venn diagrams No big surprises! Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 58 / 122
  67. 67. Set Intersection: As Figure Example (Emp1) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Jens Jens Lene Lene Example (Emp2) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Jens Lene Lene Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 59 / 122
  68. 68. Set Intersection: As Figure Example (Emp1) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Jens Jens Lene Lene Example (Emp2) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Jens Lene Lene Example (Emp1 ∩ Emp2) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Jens Lene Lene Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 59 / 122
  69. 69. Set Intersection: As Table Example (Emp) ename dept vts vte Jens RD 0 5 Lene Cust 2 10 Jens Sup 6 15 Jens Cust 16 20 Example (Boss) ename dept vts vte Lene Cust 5 9 Jens Cust 17 20 Example (Emp ∩ Boss) ename dept vts vte Lene Cust 5 9 Jens Cust 17 20 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 60 / 122
  70. 70. Set Intersection: The Cases Example (Before) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss empty Example (After) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss empty Example (Right Overlap) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss result Example (Left Overlap) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss result Example (Included) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss result Example (Totally Overlaps) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss result Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 61 / 122
  71. 71. Set Intersection: Informally Definition Explicit columns must be equal If overlap between the intervals then take the The largest of the vts values The smallest of the vte values Otherwise skip the row Note Recall that vts ≤ vte for interval to be valid Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 62 / 122
  72. 72. Set Intersection in SQL select emp. ename, emp. dept , −− overlap emp and boss greatest (emp. vts , boss . vts ) as vts , least (emp. vte , boss . vte ) as vte from emp dept vt as emp, bossemp dept vt as boss where emp. ename = boss . ename and emp. dept = boss . dept and emp. vts < boss . vte and boss . vts < emp. vte and greatest (emp. vts , boss . vts ) <= least (emp. vte , boss . vte ) Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 63 / 122
  73. 73. Set Union: As Figure Example (Emp1) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Jens Jens Lene Lene Example (Emp2) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Jens Lene Lene Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 64 / 122
  74. 74. Set Union: As Figure Example (Emp1) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Jens Jens Lene Lene Example (Emp2) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Jens Lene Lene Example (Emp1 ∪ Emp2) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Jens Jens Lene Lene Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 64 / 122
  75. 75. Set Union: As Table Example (Emp) ename dept vts vte Jens RD 0 5 Lene Cust 2 10 Jens Sup 6 15 Jens Cust 16 20 Example (Boss) ename dept vts vte Lene Cust 5 9 Jens Cust 17 20 Example (Emp ∪ Boss) ename dept vts vte Jens RD 0 5 Lene Cust 2 10 Jens Sup 6 15 Jens Cust 16 20 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 65 / 122
  76. 76. Set Union: The Cases Example (Before) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss result result Example (After) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss result result Example (Right Overlap) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss result Example (Left Overlap) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss result Example (Included) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss result Example (Totally Overlaps) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss result Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 66 / 122
  77. 77. Set Union: Informally Definition Explicit columns must be equal If overlap between the intervals then take the The smallest of the vts values The largest of the vte values Else take the intervals in the first table, and take the intervals in the second table Note Three cases to consider Mutual exclusive cases therefore union all between cases The rows must be snapshot equal, i.e., have the same explicit columns Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 67 / 122
  78. 78. Set Union in SQL select emp. ename, emp. dept , −− overlap emp and boss least (emp. vts , boss . vts ) as vts , greatest (emp. vte , boss . vte ) as vte from emp dept vt as emp, bossemp dept vt as boss where emp. ename = boss . ename and emp. dept = boss . dept and emp. vts < boss . vte and boss . vts < emp. vte union a l l select emp. ename, emp. dept , emp. vts , emp. vte −− emp no overlap from emp dept vt as emp where not exists ( select ∗ from bossemp dept vt as b where emp. ename = b . ename and emp. dept = b . dept and emp. vts < b . vte and b . vts < emp. vte ) union a l l select boss . ename, boss . dept , boss . vts , boss . vte −− boss no overlap from bossemp dept vt as boss where not exists ( select ∗ from emp dept vt as b where boss . ename = b . ename and boss . dept = b . dept and boss . vts < b . vte and b . vts < boss . vte ) −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 68 / 122
  79. 79. Set Difference: As Figure Example (Emp) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Jens, RD Jens, Sup Jens, Cust Lene, Cust Example (Boss) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Jens, Cust Lene, Cust Example (Emp - Boss) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Jens, RD Jens, Sup Jens, Cust Lene, Cust Lene, Cust Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 69 / 122
  80. 80. Set Difference: As Table Example (Emp) ename dept vts vte Jens RD 0 5 Lene Cust 2 10 Jens Sup 6 15 Jens Cust 16 20 Example (Boss) ename dept vts vte Lene Cust 5 9 Jens Cust 17 20 Example (Emp - Boss) ename dept vts vte Jens RD 0 5 Lene Cust 2 5 Lene Cust 9 10 Jens Sup 6 15 Jens Cust 16 17 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 70 / 122
  81. 81. Set Difference: The Cases Example (Before) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss result Example (After) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss result Example (Right Overlap) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss result Example (Left Overlap) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss result Example (Included) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss result result Example (Totally Overlaps) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 emp boss empty Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 71 / 122
  82. 82. Set Difference: Informally Definition Explicit columns must be equal If overlap between the intervals then Row with interval [emp.vts, boss.vts) if emp.vts< boss.vts Row with interval [boss.vte, emp.vte) if boss.vte< emp.vte Otherwise skip the row Note Before or after does not matter Total overlap does not add rows to the result During is handled like a combination of left and right overlap One interval may be split into one or two intervals Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 72 / 122
  83. 83. Set Difference in SQL, Part 1 select emp. ename, emp. dept , emp. vts , emp. vte −− no overlap from emp dept vt as emp where not exists ( select ∗ from bossemp dept vt as b where emp. ename = b . ename and emp. dept = b . dept and emp. vts < b . vte and b . vts < emp. vte ) union Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 73 / 122
  84. 84. Set Difference in SQL, Part 2 select emp. ename, emp. dept , emp. vts , boss . vts −− r i g h t overlap from emp dept vt as emp, bossemp dept vt as boss where emp. ename = boss . ename and emp. dept = boss . dept and emp. vts < boss . vts and boss . vts < emp. vte and not exists ( select ∗ from bossemp dept vt as b where emp. ename = b . ename and emp. dept = b . dept and emp. vts < b . vte and b . vts < boss . vts ) union select emp. ename, emp. dept , boss . vte , emp. vte −− l e f t overlap from emp dept vt as emp, bossemp dept vt as boss where emp. ename = boss . ename and emp. dept = boss . dept and boss . vte < emp. vte and emp. vts < boss . vte and not exists ( select ∗ from bossemp dept vt as b where emp. ename = b . ename and emp. dept = b . dept and boss . vte < b . vte and b . vts < emp. vte ) Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 74 / 122
  85. 85. Quiz: Set Operations Questions R ∪ S = S ∪ R ? Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
  86. 86. Quiz: Set Operations Questions R ∪ S = S ∪ R ? Yes! R ∩ S = S ∩ R ? Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
  87. 87. Quiz: Set Operations Questions R ∪ S = S ∪ R ? Yes! R ∩ S = S ∩ R ? Yes! R − S = S − R ? Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
  88. 88. Quiz: Set Operations Questions R ∪ S = S ∪ R ? Yes! R ∩ S = S ∩ R ? Yes! R − S = S − R ? No! Can R ∪ S return fewer rows than there are in R? Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
  89. 89. Quiz: Set Operations Questions R ∪ S = S ∪ R ? Yes! R ∩ S = S ∩ R ? Yes! R − S = S − R ? No! Can R ∪ S return fewer rows than there are in R? Yes! In contrast to the non-temporal version Can R − S return more rows than there are in R? Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
  90. 90. Quiz: Set Operations Questions R ∪ S = S ∪ R ? Yes! R ∩ S = S ∩ R ? Yes! R − S = S − R ? No! Can R ∪ S return fewer rows than there are in R? Yes! In contrast to the non-temporal version Can R − S return more rows than there are in R? Yes! In contrast with non-temporal version Can R ∩ S return more rows than there are in R? Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
  91. 91. Quiz: Set Operations Questions R ∪ S = S ∪ R ? Yes! R ∩ S = S ∩ R ? Yes! R − S = S − R ? No! Can R ∪ S return fewer rows than there are in R? Yes! In contrast to the non-temporal version Can R − S return more rows than there are in R? Yes! In contrast with non-temporal version Can R ∩ S return more rows than there are in R? Yes! In contrast with non-temporal version Can R − S return an empty result? Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
  92. 92. Quiz: Set Operations Questions R ∪ S = S ∪ R ? Yes! R ∩ S = S ∩ R ? Yes! R − S = S − R ? No! Can R ∪ S return fewer rows than there are in R? Yes! In contrast to the non-temporal version Can R − S return more rows than there are in R? Yes! In contrast with non-temporal version Can R ∩ S return more rows than there are in R? Yes! In contrast with non-temporal version Can R − S return an empty result? Yes! Like the non-temporal version! Can R ∩ S return an empty result? Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
  93. 93. Quiz: Set Operations Questions R ∪ S = S ∪ R ? Yes! R ∩ S = S ∩ R ? Yes! R − S = S − R ? No! Can R ∪ S return fewer rows than there are in R? Yes! In contrast to the non-temporal version Can R − S return more rows than there are in R? Yes! In contrast with non-temporal version Can R ∩ S return more rows than there are in R? Yes! In contrast with non-temporal version Can R − S return an empty result? Yes! Like the non-temporal version! Can R ∩ S return an empty result? Yes! Like the non-temporal version! Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
  94. 94. Summary: Set Operators Main Points No big surprises in how union, intersection, and difference are defined The size of the result sets may be a surprise! Set difference is surprisingly difficult Challenge that single row can be split into two rows Note Notation of union compatibility still applies! Alternative interpretations of the set operators exists However not in widespread usage Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 76 / 122
  95. 95. Outline 1 Overview and Foundation Current, Valid Time, Transaction Time, and Bitemporal Overlap Between Intervals 2 Temporal Join 3 Aggregation 4 Coalescing 5 Set Operators 6 Timeslice Valid Time Bitemporal 7 Summary Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 77 / 122
  96. 96. Timeslicing: As a Figure Example VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Joe, Shoe Joe, Toy Jim, Toy Jill, Shoe Example (In Table Format) ename dept vts vte Joe Shoe 2 8 Joe Toy 10 now Jim Toy 4 14 Jill Shoe 12 now Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 78 / 122
  97. 97. Timeslicing: As a Figure Example VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Joe, Shoe Joe, Toy Jim, Toy Jill, Shoe Example (Timeslice at time 1) ename dept Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 78 / 122
  98. 98. Timeslicing: As a Figure Example VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Joe, Shoe Joe, Toy Jim, Toy Jill, Shoe Example (Timeslice at time 3) ename dept Joe Shoe Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 78 / 122
  99. 99. Timeslicing: As a Figure Example VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Joe, Shoe Joe, Toy Jim, Toy Jill, Shoe Example (Timeslice at time 9) ename dept Jim Toy Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 78 / 122
  100. 100. Timeslicing: As a Figure Example VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Joe, Shoe Joe, Toy Jim, Toy Jill, Shoe Example (Timeslice at time 13) ename dept Joe Toy Jim Toy Jill Shoe Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 78 / 122
  101. 101. Timeslicing: As a Figure Example VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Joe, Shoe Joe, Toy Jim, Toy Jill, Shoe Example (Timeslice at time 20) ename dept Joe Toy Jill Shoe Note Timeslicing can be both in valid time and transaction time Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 78 / 122
  102. 102. Outline 1 Overview and Foundation Current, Valid Time, Transaction Time, and Bitemporal Overlap Between Intervals 2 Temporal Join 3 Aggregation 4 Coalescing 5 Set Operators 6 Timeslice Valid Time Bitemporal 7 Summary Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 79 / 122
  103. 103. Current TimeSlice (now = 20) Example (Content) ename dept vts vte Joe Shoe 2 8 Joe Toy 10 now Jim Toy 4 14 Jill Shoe 12 now Example (Timeslice) ename dept Joe Toy Jill Shoe Example (SQL Statement) select ename, dept from emp where vte = 99 −− 99 i s the value used f o r now Note Only explicit attributes SQL statement is simple Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 80 / 122
  104. 104. Timeslice Back in Time 13 (now = 20) Example (Content) ename dept vts vte Joe Shoe 2 8 Joe Toy 10 now Jim Toy 4 14 Jill Shoe 12 now Example (Timeslice) ename dept Joe Toy Jim Toy Jill Shoe Example (SQL Statement) select ename, dept from emp where vts < 13 and vte >= 13 Note between cannot be used due to half-open intervals Query different from current timeslice SQL statement again simple Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 81 / 122
  105. 105. Summmary: Valid-Time Timeslice Main Points Current state most often used Timeslice back and forward in time same query SQL statements are simple Must pick a value for now Typically 9999-12-30 or 9999-12-31 Transaction-time slicing basically the same as for valid-time Note Often current state separated from other data Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 82 / 122
  106. 106. Outline 1 Overview and Foundation Current, Valid Time, Transaction Time, and Bitemporal Overlap Between Intervals 2 Temporal Join 3 Aggregation 4 Coalescing 5 Set Operators 6 Timeslice Valid Time Bitemporal 7 Summary Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 83 / 122
  107. 107. First Example of Transaction-Time Timeslice Example (Bitemporal Graph) TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, Shoe Joe, Shoe Joe, Toy Joe, Toy Joe, Shoe Table Content Content as of time 15 Only one employee, to make graph readable Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 84 / 122
  108. 108. Timeslice at 1 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, Shoe ename dept vts vte Joe Shoe 1 now Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 85 / 122
  109. 109. Timeslice at 2 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, Shoe ename dept vts vte Joe Shoe 1 now Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 86 / 122
  110. 110. Timeslice at 3 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, Shoe ename dept vts vte Joe Shoe 1 now Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 87 / 122
  111. 111. Timeslice at 4 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, Shoe ename dept vts vte Joe Shoe 1 now Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 88 / 122
  112. 112. Timeslice at 5 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, ShoeJoe, Shoe Joe, Toy ename dept vts vte Joe Shoe 1 5 Joe Toy 5 now Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 89 / 122
  113. 113. Timeslice at 6 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, ShoeJoe, Shoe Joe, Toy ename dept vts vte Joe Shoe 1 5 Joe Toy 5 now Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 90 / 122
  114. 114. Timeslice at 7 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, ShoeJoe, Shoe Joe, Toy ename dept vts vte Joe Shoe 1 5 Joe Toy 5 now Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 91 / 122
  115. 115. Timeslice at 8 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, ShoeJoe, Shoe Joe, Toy ename dept vts vte Joe Shoe 1 5 Joe Toy 5 now Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 92 / 122
  116. 116. Timeslice at 9 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, ShoeJoe, Shoe Joe, Toy ename dept vts vte Joe Shoe 1 5 Joe Toy 5 now Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 93 / 122
  117. 117. Timeslice at 10 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, ShoeJoe, Shoe Joe, ToyJoe, Toy Joe, Shoe ename dept vts vte Joe Shoe 1 5 Joe Toy 5 10 Joe Shoe 10 now Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 94 / 122
  118. 118. Timeslice at 11 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, ShoeJoe, Shoe Joe, ToyJoe, Toy Joe, Shoe ename dept vts vte Joe Shoe 1 5 Joe Toy 5 10 Joe Shoe 10 now Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 95 / 122
  119. 119. Timeslice at 12 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, ShoeJoe, Shoe Joe, ToyJoe, Toy Joe, Shoe ename dept vts vte Joe Shoe 1 5 Joe Toy 5 10 Joe Shoe 10 now Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 96 / 122
  120. 120. Timeslice at 13 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, Shoe Joe, Shoe Joe, ToyJoe, Toy Joe, Shoe ename dept vts vte Joe Shoe 1 5 Joe Toy 5 10 Joe Shoe 10 now Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 97 / 122
  121. 121. Timeslice at 14 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, Shoe Joe, Shoe Joe, ToyJoe, Toy Joe, Shoe ename dept vts vte Joe Shoe 1 5 Joe Toy 5 10 Joe Shoe 10 now Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 98 / 122
  122. 122. Timeslice at 15 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Joe, Shoe Joe, Shoe Joe, Toy Joe, Toy Joe, Shoe ename dept vts vte Joe Shoe 1 5 Joe Toy 5 10 Joe Shoe 10 now Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 99 / 122
  123. 123. Summary: First Example Note that All previous states retained Backward compatible (with “plain” SQL) End-used has supplied no valid-time values Transaction-time slice contains valid-time columns Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 100 / 122
  124. 124. Second Example of Transaction-Time Timeslice Example (Bitemporal Graph) TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Morphine, Jones Table Content Content as of time 15 Only one patient Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 101 / 122
  125. 125. Timeslice at 1 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper pname medicin vts vte Bill Prozac 1 6 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 102 / 122
  126. 126. Timeslice at 2 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper Bill, Morphine, Jones pname medicin vts vte Bill Prozac 1 6 Bill Morphine 10 15 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 103 / 122
  127. 127. Timeslice at 3 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper Bill, Morphine, Jones pname medicin vts vte Bill Prozac 1 6 Bill Morphine 10 15 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 104 / 122
  128. 128. Timeslice at 4 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper Bill, Morphine, Jones pname medicin vts vte Bill Prozac 1 6 Bill Morphine 10 15 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 105 / 122
  129. 129. Timeslice at 5 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Morphine, Jones pname medicin vts vte Bill Prozac 1 13 Bill Morphine 10 15 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 106 / 122
  130. 130. Timeslice at 6 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Morphine, Jones pname medicin vts vte Bill Prozac 1 13 Bill Morphine 10 15 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 107 / 122
  131. 131. Timeslice at 7 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Morphine, Jones pname medicin vts vte Bill Prozac 1 13 Bill Morphine 10 15 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 108 / 122
  132. 132. Timeslice at 8 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Morphine, Jones pname medicin vts vte Bill Prozac 1 9 Bill Morphine 10 15 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 109 / 122
  133. 133. Timeslice at 9 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Morphine, Jones pname medicin vts vte Bill Prozac 1 9 Bill Morphine 10 15 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 110 / 122
  134. 134. Timeslice at 10 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Morphine, Jones pname medicin vts vte Bill Prozac 1 9 Bill Morphine 10 15 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 111 / 122
  135. 135. Timeslice at 11 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Morphine, Jones pname medicin vts vte Bill Prozac 1 9 Bill Morphine 10 15 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 112 / 122
  136. 136. Timeslice at 12 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Morphine, Jones pname medicin vts vte Bill Prozac 1 9 Bill Morphine 10 15 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 113 / 122
  137. 137. Timeslice at 13 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Morphine, Jones pname medicin vts vte Bill Prozac 1 9 Bill Morphine 10 15 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 114 / 122
  138. 138. Timeslice at 14 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Morphine, Jones pname medicin vts vte Bill Prozac 1 9 Bill Morphine 10 15 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 115 / 122
  139. 139. Timeslice at 15 TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Morphine, Jones pname medicin vts vte Bill Prozac 1 9 Bill Morphine 10 15 Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 116 / 122
  140. 140. Summary: Second Example Note that All previous states retained Valid-time does not retain your errors! However, bitemporal does! Not-backwards compatible (with “plain” SQL) End-used has supplied all valid-time values Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 117 / 122
  141. 141. Quiz: Does Bill Die? Example (Medication for Bill) TT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Prozac, Pepper Bill, Morphine, Jones Alive or Dead? If prozac and morphine is a leathal combination does Bill die? Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 118 / 122
  142. 142. Summary: Timeslicing Main Points Timeslicing see the table as of a point in time Timeslicing can be both in valid-time and transaction-time For transaction-time timeslicing only into the past For valid-time timeslicing both into the past and the future Timeslicing is a fairly simple SQL select statement Note Current timeslice = the database state stored by non-temporal databases Physical database design: current timeslice stored separately from other data Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 119 / 122
  143. 143. Outline 1 Overview and Foundation Current, Valid Time, Transaction Time, and Bitemporal Overlap Between Intervals 2 Temporal Join 3 Aggregation 4 Coalescing 5 Set Operators 6 Timeslice Valid Time Bitemporal 7 Summary Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 120 / 122
  144. 144. Quiz Example (Emp Table One) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Joe, Shoe Joe, Toy Joe, Shoe Tom, Shoe Tom, Toy Questions Were Joe and Tom employed during the same period? Did Joe and Tom work in the same department at the same time? Is the data coalesced? Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 121 / 122
  145. 145. Quiz Example (Emp Table Two) VT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Joe, Shoe Joe, Toy Joe, Food Tom, Food Tom, Shoe Tom, Toy Tom, Toy Questions Were Joe and Tom employed during the same period? Did Joe and Tom work in the same department at the same time? Is the data coalesced? Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 121 / 122
  146. 146. Summary: Main Ideas Query Types Time dimensions: valid time, transaction time, and user-defined time Bitemporal = valid time + transaction time Query types: current, sequenced, non-sequenced Extensions of plain SQL: Join, aggregation, set operations Also projection and selection New possibilities: Timeslicing New problems: Coalescing Intervals overlap, central to understand how works Not difficult, just many cases Note A temporal data model implemented using plain SQL IBM DB2 10, SQL Server 2015, and TeraData 13.10 have built-in bitemporal support Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 122 / 122

×