How can I optimize below SQL (simplified view of my complex query.) Ideally, I should be able to cache the first SQL result (order ids) and do some kind of projection on OrderLine table in the second query.
Any pointers will be helpful.
Restrictions – I cannot create temporary tables, cursors or procedures / functions. I am connecting to Oracle 10g.
SELECT 'Object_id', id, mod_id FROM
(
(Select 'Order_id', order_id, mod_id FROM Orders)
UNION
(select 'Order_line_id', order_line_id, mod_id FROM OrderLine
WHERE order_id IN (Select order_id FROM Orders)
)
)
The optimizer is responsible for optimizing your query; relax and let it do its stuff.
Any explicit caching you attempt will involve things like temporary tables (which you’ve ruled out), so you can only make the common query more explicit, perhaps, by using a CTE (common table expression, aka WITH clause) to name the common sub-query. But the optimizer might well process things the same way regardless.
You can replace the IN clause with a JOIN; that will likely be faster. Again, the optimizer might do that anyway. However, that’s not a caching operation; it is a standard query rewrite.