我有一個選擇的球隊/過濾器,並計算下面的查詢有多少協會與它們匹配加快聯合查詢
select t.name, count(c.id) from teams t
left join users u on t.id = u.team_id
left join conversation_users cu on cu.user_id = u.id
left join conversations c on cu.conversation_id = c.id
group by t.id
UNION
select 'Me', count(cu.id) from conversations c
left join conversation_users cu on cu.conversation_id = c.id
where cu.user_id = 'logged in user'
UNION
select 'All', count(c.id) from conversations c
left join apps a on c.app_id = a.id
where a.id = 'current app'
UNION
select 'Unassigned', count(c.id) from conversations c
left join apps a on c.app_id = a.id
where a.id = 'current app' and c.user_id is null
我不是數據庫的天才,但是這似乎是一個非常低效/不SCALEABLE做法。有沒有更好的方法來完成這一點?
目前大約需要50ms才能在小尺寸的桌子上運行。
我想我可以用這種方法
select
count(case when c.started then 1 end) as all,
count(case when COALESCE(c.assigned_user_id::text, c.assigned_team_id::text) is null and c.started then 1 end) as unassigned,
count(case when c.assigned_user_id = 'clt8ojxvk0000dp2fyvwq126' and c.started then 1 end) as me
from apps a
left join conversations c on c.app_id = a.id
where a.id = 'ASnYW1-RgCl0I'
非常quickyl取我,所有的和無符號的過濾器(約15毫秒),我能做到的球隊類似的東西,並把它們合併起來?
缺少通常的嫌疑人:Postgres版本,表定義。並請定義「小尺寸」。人們認爲「小」的程度依數量級而定。 –