Using the timslot_slots table for any two intervals
Let p1 = opening_slot_id, p2 = closing_slot_id Let q1 = opening_slot_id, q2 = closing_slot_id
- equals -> allens/equals.mysql
- before -> allens/before.mysql
- after -> allens/before.mysql
- meets -> allens/meets.mysql
- meets-by -> allens/meets.mysql
- overlaps -> allens/overlaps.mysql
- overlaps-by allens/overlaps.mysql
- during -> allens/during.mysql
- includes -> allens/during.mysql
- starts -> allens/starts.mysql
- stared-by -> allens/starts.mysql
- finishes -> allens/finishes.mysql
- finished-by -> allens/finishes.mysql
- Aligns -> combine/aligns.mysql
- Excludes -> combine/excludes.mysql
- Fills -> combine/fills.mysql
- Intersects -> combine/intersects.mysql
- occupies -> combine/occupies.mysql
There are four ways to represent an interval depending on the inclusion of the opening and closing values.
The generally prefered format in database is closed:open a closing interval is always closing value +1 which actually the opening of the next range.
Two intervals with 5 mintues each open:open (0:6) closed:closed [1:5] open:closed (0:5] close:open [1:6)
The closed:open is prefered as it allows a continous range of intervals to be easily searched. If closed:closed is used the above range would be [1:5] and the next range would be [6:10]. Its easier to build a continous set if the intervals p[1:6) & q[6:11) we can match 'p2=q1'. If we used closed-closed we would need to know the length of the clock tick between intervals so we could write a query like '(p2 - q1) = clock-tick'
The Relation Interval Tree is an adaption of and Interval Tree structure into a relational context. The algorithm was outlined out in a paper called Managing Intervals Efficiently in Object-Relational Databases.
I have implement their fixed height tree to speed up my intersection queries.
The table timeslot_slots_tree
tree has been modifed to support the RITree. It is
based on the original timeslot_slots
table.
An example of every day query can be found in tree/basic.mysql. A comparision query of common interval search can be found in tree/normal.mysql.
Using a low teir VPS hosted with Linode, running both queries to find intersection of timeslots within a particular calendar day.
Duration(sec): Normal : 0.07690900 RITree : 0.00163325
The RITree was able to significantly speed up the intersects query.