I have lots of content that needs to be published on a web site some time in the future. What’s the most efficient way to publish it when the time comes?
My current implementation is in two datetime columns: online_at and offline_at.
The sql query to fetch content looks something like this:
SELECT *
FROM contents
WHERE online_at > current_timestamp
AND (offline_at IS NULL OR
offline_at < current_timestamp);
with an index over the online_at and offline_at columns. It works well and there are no obvious performance penalties, but I’m still wondering if there is a more efficient way to go about this. Is there a way to reduce the index to one simpler column (not a datetime, which seems expensive)?
I’ve seen your construction many times and I’ve only seen it become prohibitively slow when hitting millions of rows, so I’m not sure you should really worry about the construction.
One thing, that I haven’t tried myself, but that could give you increased parallelism, is to have seperate indexes on
online_atandoffline_atand then usingEXCEPT/MINUS(depending on your DB). In essence just use the IDs, but this could obviously be extended with the entire field list except for the dates. Ie: