I would like to find out the number of unique users who have posted or liked the posts in a particular category.
I have two tables
SELECT user_id FROM posts WHERE category = 0 union SELECT user_id from likes where likes.post_id in (select id from posts where category = 0)
You can only speed up the performance by replacing the subquery by
SELECT user_id FROM posts WHERE category = 0 UNION SELECT user_id FROM likes INNER JOIN posts ON likes.post_id = posts.id WHERE posts.category = 0;
It will be much faster if
likes.post_id is indexed properly and