I have a follow-up question regarding an issue I previously had on SO here. I would like to know how to further optimize/refine my issue.
My old query was:
-- OLD QUERY --
SELECT start, end, title, details, location
FROM event
WHERE (start >= 2012-02-26 00:00:00 AND end <= 2012-04-01 00:00:00)
AND user_id= $user_id;
This query works great because it does not get old events, say from January. So the query is fast and efficient. But, it does not work for queries that may overlap into the next month. (Let’s say an event has a “start_date” of 2012-03-29, but an “end_date” of 2012-04-05. Well, then it’s not pulled in the query, thus, not displaying.
So, my new query ended up being:
-- NEW QUERY --
SELECT start, end, title, details, location
FROM event
WHERE (end > 2012-02-26 00:00:00 OR start < 2012-04-01 00:00:00)
AND user_id= $user_id;
But, with the new query, it ends up pulling ALL events prior to 02-26-2012, thus making it VERY slow and inefficent.
How can I optimize this query to achieve both of these:
a) Make it work so that it gathers events that span the end of one month into another?
b) And at the same time NOT query every single other event in the past?
Hopefully there’s a way to do this! Thanks very much for any input.
To find overlapping events, change
ORtoANDin your query (changed the dates slightly to get overlaps with March only):