Skip to content

Instantly share code, notes, and snippets.

@sunaot
Created May 24, 2013 05:41
Show Gist options
  • Save sunaot/5641488 to your computer and use it in GitHub Desktop.
Save sunaot/5641488 to your computer and use it in GitHub Desktop.
select
  c.id
from
  user u
  join
  recipient r
    on (u.id = r.user_id)
  join
  conversation c
    on (u.id = c.user_id or r.conversation_id = c.id)
where
  u.username = 'okitsu';

index 考慮すると、or で join してるところはチューニングとして union all 使うほうが多そう。

select
  c.id
from
  user u
  join
  conversation c
    on (u.id = c.user_id)
where
  u.username = 'okitsu'
union all
select
  c.id
from
  user u
  join
  recipient r
    on (u.id = r.user_id)
  join
  conversation c
    on (r.conversation_id = c.id)
where
  u.username = 'okitsu';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment