select TABLE1.FIELD1,
TABLE1.FIELD2,
TABLE1.FIELD3,
TABLE1.FIELD4,
TABLE1.FIELD5,
TABLE2.FIELD6,
TABLE2.FIELD7
from TABLE1,
TABLE2
where TABLE1.FIELD8 = 'value'
and TABLE2.FIELD6 = TABLE1.FIELD6;
I am searching for some data from 2 different tables. (Oracle database - wherefields indexed for both tables) The above query is taking 500ms to be executed. When I search the tables seperately for the same fields they finish in less than 20ms each.
I could search TABLE1 for the data I need (+FIELD6) and then search TABLE2 for the rest using FIELD6.
My question is. Why is it so much slower when I join the tables. Am I doing something wrong?
EDIT: Adding oracle's explain plan
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost |
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 6318 | 586K| 620 |
| 1 | HASH JOIN | | 6318 | 586K| 620 |
| 2 | TABLE ACCESS BY INDEX ROWID| TABLE1 | 6318 | 450K| 2 |
| 3 | INDEX RANGE SCAN | INDEX_TABLE1_FIELD8 | 2527 | | 1 |
| 4 | TABLE ACCESS FULL | TABLE2 | 430K| 9242K| 508 |
----------------------------------------------------------------------------------------
Note: cpu costing is off, 'PLAN_TABLE' is old version
If there are 25 records in TABLE1 that satisfy field8='value' and if it takes 20ms to select ... from table2 where field6=??? then 500ms is in the realm of expected time.
So, it's quite meaningles to say each query takes 20ms, you'd also have to state how many records statisfy the field8 condition in TABLE1 and how many records on average satisfy a condition on TABLE2.FIELD6.
But to eliminate all guessing you should have Oracle explain the query and display (or post) the explained plan here for further analysis.
EDIT: Since there seems to be a 1:1 relationship between the criterias (and the query subsequently returns 1 record), 500ms is not expected. In this case I'd really stress the need for having the query explained. In case you're not familiar with it, you can do that like so:
explain plan for
select .... <your entire select statement goes here>
;
select * from table(dbms_xplan.display);
And then post the result. This will allow us to help you better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With