Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

joining 3 tables - how Oracle decides about execution plan

733217Jan 8 2010 — edited Jan 12 2010
Sorry if you find my question too "unclear" to be answered but I will try to be as specific as possible.

I have a normal SQL query, targeting 3 tables.
One of the tables is a table used for storing some logging information and we have one such table for every month.

I.e.:
table_x
table_y
table_01_2009

I needed to run my query for every month of the year, i.e. something like:
/.../ select...
       from table_x
           ,table_y
           ,table_01_2009
      where table_x.external_id = table_y.external_id
        and table_x.network = table_01_2009.network
        and table_y.country = table_01_2009.state;
...and then 11 same queries, only replacing "table_01_2009" for "table_02_2009", "table_03_2009" etc.

All "table_01_2009"-"table_12_2009" tables are basically the same: similar number of rows, identical structure, identical indexes, same "last analysed" date and so on. Even explain plans of all 12 queries are more or less the same (cost, bytes, CPU cost).


And now my problem: some of these 12 queries ran quickly (< 1 min), some of them were disasted (> 3 hours).

In explain plan I found out, that Oracle sometimes makes joining of my 3 tables this way:
1. joining table_x and table_01_2009
2. result of this join is joined with table_y

..and sometimes this way
1. joining table_x and table_y
2. result of this join is joined with table_01_2009


It's absolutely correct that second alternative of joining is disasterous. Because of my data distribution and absence of indexes on key columns it is correct that this query is unusable. (That's OK, I could easily fix it with "LEADING" hint.)

But why Oracle sometimes chooses one way of joining and sometimes the other?
I actually ran first query with January, it ran OK, and when I saw all other queries have basically the same explain plans (cost, bytes, CPU cost), I launched also the other ones without worries - but! - half of them never finished...

And my questions are:
1. Could I have predicted such behaviour? Is it normal that cost, bytes, CPU cost in explain plan are same, but performance is fatally different?
2. Is there any data dictionary table where I could see, what could be Oracle's reasons for choosing first way of joining or the second? Is there any way understand closer how Oracle decides when constructing this "joining sequence" in general?

Thanks for any answers,
jan
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 9 2010
Added on Jan 8 2010
7 comments
1,373 views