我有一个选择的球队/过滤器,并计算下面的查询有多少协会与它们匹配加快联合查询
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版本,表定义。并请定义“小尺寸”。人们认为“小”的程度依数量级而定。 –