I have two tables called "nodes" and "links". I am performing the following query and it returns the results I want. How to optimize this one? I am using the resultset of the outer query in inner query. How to reference to the resultset of the outer query in inner queries so as to prevent running the same outer query again. Repeated queries between arrows.
SELECT * FROM nodes where name = 'Compost' union all
-> SELECT * FROM nodes where id in
( SELECT target from links where source in
( SELECT id FROM (SELECT * FROM nodes where name = 'Compost' <- ) as a)) union all SELECT * FROM nodes where id in
( SELECT target from links where source in (SELECT id FROM ( -> SELECT * FROM nodes where id in ( SELECT target from links where source in
( SELECT id FROM (SELECT * FROM nodes where name = 'Compost'<- ) as c))) as b))