I have several big time-series tables having a lot of Nulls (each table may have up to 300 columns), for example:
Time-series table
time | a | b | c | d
--------------------+---------+----------+---------+---------
2016-05-15 00:08:22 | | | |
2016-05-15 13:50:56 | | | 26.8301 |
2016-05-15 01:41:58 | | | |
2016-05-15 00:01:37 | | | |
2016-05-15 01:45:18 | | | |
2016-05-15 13:45:32 | | | 26.9688 |
2016-05-15 00:01:48 | | | |
2016-05-15 13:47:56 | | | | 27.1269
2016-05-15 00:01:22 | | | |
2016-05-15 13:35:36 | 26.7441 | 29.8398 | | 26.9981
2016-05-15 00:08:53 | | | |
2016-05-15 00:08:30 | | | |
2016-05-15 13:14:59 | | | |
2016-05-15 13:33:36 | 27.4277 | 29.7695 | |
2016-05-15 13:36:36 | 27.4688 | 29.6836 | |
2016-05-15 13:37:36 | 27.1016 | 29.8516 | |
I want to optimize queries for searching first and last values in every column, i.e.:
select MIN(time), MAX(time) from TS where a is not null
(Those queries can run for several minutes)
I plan to create a metadata table holding column names and pointing to the first and last timestamp:
Metadata table
col_name | first_time | last_time
---------+---------------------+--------------------
a | 2016-05-15 13:35:36 | 2016-05-15 13:37:36
b | 2016-05-15 13:35:36 | 2016-05-15 13:37:36
c | 2016-05-15 13:50:56 | 2016-05-15 13:45:32
d | 2016-05-15 13:47:56 | 2016-05-15 13:35:36
This way no Null search will occur during the query and I will just access the value in the first and last timestamps.
But I want to prevent the need to update the metadata table on every time-series data modification. Instead I want to create a generic Trigger Function to which will update first_time and last_time columns of the metadata table on every Insert, Update or Delete to Time-Series table. The trigger function should compare existing timestamps in the metadata table against inserted / deleted rows.
Any idea if it's possible to create a generic Trigger Function which will not hold the exact column names of time-series table?
Thanks
(a asc, time asc)and(a asc, time desc)(and the same forb,candd). If you want you can than go for a "metadata" view. Better try to avoid creating redundancy.