MODEL
In the current model of my application, events can occur at several non-combinable frequencies, whose values are enumerated as follows: ONCE, DAILY, WEEKLY, MONTHLY, YEARLY, and that (i.e. the event occurrences) are starting at a certain date and optionally ending at a later one (when they're not ending, their value is NULL).
For instance, an event:
has:
USAGE
This data can be updated via a back-office very frequently. It is needed on our public pages in order to display a 3-day timetable of all event occurrences, gathered by 1-hour slots.
The default start of the timetable is the date of today at midnight. The timetable start can go down to 1 month before the current date and 2 months after.
WHAT I NEED
All events data is inserted without any problem via the back-office. The tricky part comes when I need to fetch them, organized in a timetable way. I tried a brute approach (select all event, and duplicating the occurrences myself) but I kinda failed (and this raises the issue of updated the big cache of replicated events whenever the data changes and everyday!).
What I really want from this beautiful model of mine (stupid me: I didn't think of both the write AND read query at the same time) is to have all occurrences of events between beginning_date and end_date, organized by 1-hour slot.
What do you suggest ?
By the way, the model is open, so it can be changed in order to better address the issue. Everything is open to discussion here.
Best regards,
Rolf
P.S.: the current (interesting part of the) model looks like:
mysql> describe occurrences;
+----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| start_date | datetime | NO | | NULL | |
| end_date | datetime | YES | | NULL | |
| frequency_type | varchar(50) | NO | MUL | NULL | |
| event_id | bigint(20) | NO | MUL | NULL | |
+----------------+-------------+------+-----+---------+----------------+
mysql> describe frequency_types;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| value | varchar(50) | NO | PRI | NULL | |
+-------+-------------+------+-----+---------+-------+
You can query all events occurring whose start is before the requested end and end is null OR after the requested beginning, and then replicate/calculate all events from the result set.