Materialized view for the last 24 hour period

materialized-vieworacle

I am collecting spatial point data that has an associated timestamp with it. I want to create a view of this data for the last 24 hour period. Because I am piping this spatial data through ArcGIS server, there are performance problems related to just piping in a straight view (this view can contain easily 100-200k+ points). To make a long story short, to mitigate these performance issues, I have created materialized views of the data. However, I want this data to be updated every 3 – 4 minutes.

My question is, when I add the current_timestamp to the query:

SELECT * 
FROM points 
WHERE timestamp > current_timestamp - interval '24' hour

…this no longer can be used as a fast refresh. Is there any way to correct that, or will it always be a complete refresh? If shown, what should I be mindful of when it comes to the performance of updating the materialized view (again, this dataset can easily be 100-200k points with 3 – 6 points added every second).

Best Answer

A fast-refreshable materialized view cannot contain a non-deterministic function like current_timestamp. So if you want to materialize the data from the last 24 hours in a materialized view, the materialized view would need to do a complete refresh every time.

Do you need a materialized view? Could you maintain your own staging table and create a custom job that runs every few minutes, deletes the data that is now more than 24 hours old, and inserts the new data (either by directly querying the table or creating a trigger that writes the new data to a different table)? That's likely a non-starter if you need to use the query rewrite functionality of a materialized view but if you just want the benefit of having a smaller table to query, custom code may be more efficient.

Or could you partition the table by day so that queries for the past 24 hours always just have to hit the two most recent partitions?