有事件。事件可以是私人的 - 只有受邀的用户才能看到它,公共的 - 对所有人可见,在群组中 - 只有群组成员才能看到它。组可以是公共的或私有的。我们不应该显示来自私人团体的事件。
event
id, title, group (nullable), is_private
event_invitee
event_id, user_id
group
id, title, is_open
group_participant
group_id, user_id
因此,现在为了显示当前经过身份验证的用户可用的事件,我们执行类似(伪代码)的查询:
SELECT
e
FROM event e
LEFT JOIN e.invitees i WITH i.user = :current_user # is current user invited to event ?
LEFT JOIN e.group g
LEFT JOIN g.participants p WITH g.user = :current_user # is current user are participant of group?
WHERE ((e.group IS NULL OR g.is_open = true OR (g.is_open = false AND p IS NOT NULL))
AND (r.is_private = false OR (r.is_private = true AND i IS NOT NULL)))
AND /* other filters, like by location */
这很好用,但很慢。并且不可能过时。如何改变结构以获得更快和可扩展的结果?