I have so-called links that can have tags assigned to them, so I store it in 3 tables:
- tag: id, name
- tag_in_link: tag_id, link_id
- link: id, url
Now I need to get basic tag counts: how many times a tag was used (including 0 times). I have two queries:
select t.id, t.name, count(*)
from tag as t inner join tag_in_link as tl
on tl.tag_id = t.id
group by t.id, t.name
union
select t.id, t.name, 0
from tag as t left outer join tag_in_link as tl
on tl.tag_id = t.id where tl.tag_id is null
and
select t.id, t.name,
(select count(*) from tag_in_link as tl
where tl.tag_id = t.id
) as count from tag as t
they both give the same (up to the order of records) results and work almost as fast
Problem is that I don't have much data to test it, but I need to pick one way or another today. All I know is that, there will be:
- up to 100 tags
- millions of links
So my question:
- which approach : a dependent subquery or union of joins has better performance on large tables in postgres?

