CS145 Lecture Notes -- Temporal Databases



Premise: Storing and querying inherently temporal data in a conventional relational DBMS can be painful and inefficient. Example - Database of prescription information: patient, drug, start date, end date.
  CREATE TABLE P(patient string, drug string, start DATE, end DATE)
Question: What is key for table?


Example query: Who was taking "Amox" on 5/15/03?








Example query: Has anyone ever taken "Amox" and "Bactrim" at the same time?










Example query: For how long altogether has "Mary" taken "Amox"?









Example query: Who has taken "Amox" for the longest time?








Example query: Who is taking "Amox" now?







Example query: Who will have taken "Amox" the longest a week from now, assuming the currently active prescriptions remain active?


A Temporal Relational Model

New version of prescriptions table:
  CREATE TABLE P (patient string, drug string, time TIMESTAMP)
Example tuple:
  (Sue, Amox, {[5/1/03, 5/4/03],[5/12/03, NOW]})
Question: What is key for table?


Temporal Relational Algebra

Special interpretation for PROJECT: coalesce timestamps

Example: PROJECT[drug](P)








Example query: Who was taking "Amox" on 5/15/03?








Special interpretation for JOIN: implicit intersection of timestamps

Example query: Has anyone ever taken "Amox" and "Bactrim" at the same time?











Example query: For how long altogether has "Mary" taken "Amox"?








Example query: Who is taking "Amox" now?






There is a proposed standard called TSQL2 that incorporates these kinds of temporal operations (and many many others) into SQL

Example: Moving-Window Aggregate

AggFun(R,W,G,A)








Example: Smoothed stock trends

  Stock(ticker, value, timestamp)
  Query: Avg(Stock, 2min, ticker, value)





Example: Network monitoring
  Monitor(node, latency, timestamp)
  Query: Max(Monitor, 2sec, NULL, latency)