In a table that stores events, the date and time are two separate columns:
CREATE TABLE events (
pk int serial,
detail text,
ev_date date,
ev time without time zone
);
If I filter for dates:
ev_date BETWEEN start_date::date AND end_date::date AND
ev_time BETWEEN start_date::time AND end_date::time
I am missing all events within the two dates that occurred outside the time range of every day.
Thus, the start time is only relevant for the start date and the same for the end date.
Anyone with an advice on how to do it efficiently for millions of events?
Best Answer
You can use the
+
operator.Don't forget to create the index below:
I've inserted many dummy rows, so I show the result of EXPLAIN:
For comparison, I've created other index and tried other form:
According to my investigation, my way (using
+
operator) is better. I recommend to compare with both ways on your machine.