In a typical social-network-like application I have the following query that gathers news from a set of followed channels.
The main query gets the news, while the subquery gets the followed channels:
QUERY 1: Get latest news:
select n.id, n.title, n.description from community.news n where n.id_channel in ( select id_channel from community.user_follows uf where uf.id_user ='3d4c788e0e34657457febba2040297c7' )
The app needs this table of recent news, but also needs the followed channels, so I execute the subquery again:
QUERY 2, same as the subquery:
select id_channel from community.user_follows uf where uf.id_user ='3d4c788e0e34657457febba2040297c7'
Can I optimize this? Both queries are always executed in sequence. Is it worth to use temporary tables or something like that? Or is there a way to access a subquery result?