Search code examples
oracle-databaseplsqlsqoop

Which of these is more optimized?


I am trying to pull data from a oracle 11g database using sqoop. I do not have a PL/SQL background. I have to filter records on one big table based on certain recordType and starttime and then do a join on another table for the same starttime (all records in the table two are of the same record type as filtered from table1). Can someone please help me and explain which of the query will be consuming less processes and why?

Also will the join cater for filtering of records on Table2 based on starttime (based on join condition or will still parse the whole table) or I need to add another filtering condition for the same?

query1:

Select t2.field1, t2.field2 ..... t1.some_field 
FROM table1 t1 
INNER JOIN table2 t2 ON 
(
t1.field3='certain value' AND 
t1.END_TIME >= TO_DATE('20160428 16','YYYYMMDD HH24') AND t1.END_TIME < TO_DATE('20160428 17','YYYYMMDD HH24') AND t1.START_TIME = t2.START_TIME AND 
t1.field2=t2.field7) 
WHERE $CONDITIONS 

Query2:

Select t2.field1, t2.field2 ..... t1.some_field 
FROM table1 t1 
INNER JOIN table2 t2 ON 
(t1.START_TIME = t2.START_TIME AND 
t1.field2 = t2.field7) 
WHERE 
t1.field3 = 'certain value' AND t1.END_TIME >= TO_DATE('20160428 16','YYYYMMDD HH24') AND t1.END_TIME < TO_DATE('20160428 17','YYYYMMDD HH24') AND 
$CONDITIONS

Solution

  • Can someone please help me and explain which of the query will be consuming less processes and why?

    Generate an explain plan for each query, and compare them - you will se a cost of each query.

    https://docs.oracle.com/cd/B10501_01/server.920/a96533/ex_plan.htm#16889

    Just run two commands:

    1. EXPLAIN PLAN FOR your_query
    2. SELECT * FROM table( DBMS_XPLAN.DISPLAY )

    You can append explain plans to the question, they can be further analyzed to find issues and advice how these queries can be optimized.