[Solved] Oracle select with sub queries
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

Oracle select with sub queries

Database type:

I have two oracle select queries like

SELECT loc.location AS LOCATION , req.requisition AS REQ 
FROM location_view loc, requisition_view req, association ass 
WHERE loc.name = 'ABC' AND req.name = 'TRANSFER' 
AND ass.entity_id_2 = req.entity_id AND ass.entity_id_1 = loc.entity_id

And the result looks like:

enter image description here

Other query is like:

 SELECT req.requisition AS req, exp.experiment AS expt 
 FROM experiment_view exp, requisition_view req, association_view ass 
 WHERE expt.name = 'RETRIEVAL'AND req.name = 'TRANSFER' 
 AND ass.entity_id_2 = req.entity_id AND ass.entity_id_1 = expt.entity_id 

Result:

enter image description here

I am trying to combine these two SELECT queries so I get to see these results:

enter image description here

Should I be using Sub-Queries to see the combined result or is there any other way of optimizing?

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
CREATE INDEX association_idx_entity_2_entity_1 ON association (entity_id_2,entity_id_1);
CREATE INDEX location_view_idx_name_entity_id ON location_view (name,entity_id);
CREATE INDEX requisition_view_idx_name_entity_id ON requisition_view (name,entity_id);
The optimized query:
SELECT
        loc.location AS LOCATION,
        req.requisition AS REQ 
    FROM
        location_view loc,
        requisition_view req,
        association ass 
    WHERE
        loc.name = 'ABC' 
        AND req.name = 'TRANSFER' 
        AND ass.entity_id_2 = req.entity_id 
        AND ass.entity_id_1 = loc.entity_id

Related Articles



* original question posted on StackOverflow here.