Home > Uncategorized, sql > Tuning query over database link

Tuning query over database link

February 11th, 2009

I just learnt from a colleague a very useful hint for remote databases. Over a db link, Oracle does not have the necessary statistics to take the right decision.

Here is the test case :
DB01


create database link lsc_link_2 using 'DB02';
create table lsc_small_1 as 
  select rownum x from dual connect by level<100;
alter table lsc_small_1 add primary key(x);
create table lsc_big_1 as 
  select rownum x from dual connect by level<1000000;
alter table lsc_big_1 add primary key(x);

DB02


create table lsc_small_2 as 
  select rownum x from dual connect by level<100;
alter table lsc_small_2 add primary key(x);
create table lsc_big_2 as 
  select rownum x from dual connect by level<1000000;
alter table lsc_big_2 add primary key(x);

and here are the two test queries (run on DB01)


select count(*) from lsc_big_1 b1 
  join lsc_small_2@lsc_link_2 s2 using (x);
  COUNT(*)
----------
        99
Elapsed: 00:00:00.10

select count(*) from lsc_big_2@lsc_link_2 b2 
  join lsc_small_1 s1 using (x);
  COUNT(*)
----------
        99
Elapsed: 00:00:10.31

As shown above, first one is 100x faster. Anything to tune? For sure!

The trick is to execute the join remotely when the remote table is much larger than the local one!


select /*+ DRIVING_SITE(b2) */ count(*) 
  from lsc_big_2@lsc_link_2 b2 
  join lsc_small_1 s1 using (x);
  COUNT(*)
----------
        99
Elapsed: 00:00:00.06

Ref: the DRIVING_SITE hint

Bookmark and Share

  1. February 11th, 2009 at 19:03 | #1

    Good one.

  2. February 11th, 2009 at 20:20 | #2

    Wow! that’s a good example about using DRIVING_SITE hint with DBLINK.

    Thank You

  3. joel garry
    February 12th, 2009 at 03:38 | #3

    Searching the metalink bug database for driving_site comes up with some interesting minor bugs. Kinda makes one wonder about the undiscovered ones.

  4. Atul Gupta
    February 26th, 2009 at 17:36 | #4

    Never tried but seems to be the good hint

  1. No trackbacks yet.