I'm attempting to perform temporal analytic queries against tables with well over 1M rows. A typical question will be how many rows satisfy "some criteria" over an arbitrary time window broken into arbitrary time periods, such as the last 4 months, grouped into every 3 days.
Our current solution is to perform one count query per time period, so the above example would produce 40 different queries, which makes the performance unacceptable.
One approach I've taken is to create a temporary table, like so:
create temporary table time_series (
lower_limit timestamp default current_timestamp,
upper_limit timestamp default current_timestamp
);
insert into time_series (lower_limit, upper_limit) values
('2017-01-15 00:00:00', '2017-01-18 00:00:00'),
('2017-01-18 00:00:00', '2017-01-21 00:00:00'),
...
('2017-05-09 00:00:00', '2017-05-12 00:00:00'),
('2017-05-12 00:00:00', '2017-05-15 00:00:00');
select ts.upper_limit, count(mbt.time) from time_series ts
join my_big_table mbt on
(
mbt.time > ts.lower_limit and
mbt.time <= ts.upper_limit
)
group by ts.upper_limit
order by ts.upper_limit;
drop table time_series;
Produces...
+---------------------+-----------------+
| upper_limit | count(mbt.time) |
+---------------------+-----------------+
| 2017-01-18 00:00:00 | 65890 |
| 2017-01-21 00:00:00 | 98230 |
| ... | |
| 2017-05-12 00:00:00 | 57690 |
| 2017-05-15 00:00:00 | 2349 |
+---------------------+-----------------+
This is much more performant than our current solution, but the problem is I don't own the database. The tables could reside in either Oracle, SQLServer, MySQL, or PostgreSQL, and I will likely have only have SELECT privileges, so the ability to create and drop temporary tables is not guaranteed. For instance, I performed the above SQL in MySQL, but I had to grant myself the CREATE TEMPORARY TABLE to do so.
Is there a way to create a "synthetic table" (not sure what else to call it) that I could use within the scope of the query that would accept the fixed list of timestamps as periodic boundaries, similar to what I have above, except without the temporary table?