If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Oracle equivalent to OPENQUERY (remote SQL execution)

Hi all,

I feel there should be a nice one-line answer to this question, but after a couple of hours of web searching (and a quick search on these forums!) I haven't come across it yet - I'm probably using the wrong terminology...

Anyway, I'm looking for a way to have one Oracle server tell another Oracle server to execute a query remotely - basically equivalent to SQLServer's OPENQUERY. The response should come back in a resultset-type format (terminology letting me down here), e.g. so that I can use it in a FROM clause.

Quick sketch of what I mean (this running on server1):

Code:

SELECT foo.bar1,
(foo.bar2 + gloop.bar2),
foo.bar3
FROM (SELECT Max(subsel.baz1) AS bar1,
Count(subsel.baz2) AS bar2,
Sum(subsel.xyzzy) AS bar3
FROM (SELECT *
FROM table@server2
) AS subsel
) AS foo,
(SELECT Max(subsel.baz1) AS bar1,
Count(subsel.baz2) AS bar2,
Sum(subsel.xyzzy) AS bar3
FROM (SELECT *
FROM table@server3
) AS subsel
) AS gloop
WHERE ...

except so far as I know, with the code as above server1 will retreive all the columns of server2's table, and then do all the calculations itself. For large applications (and greater levels of nesting) this means a lot of data travelling over the wires; what I'd like is for the whole "AS foo" block to be executed atomically on server2, so that the only thing received by server1 is a resultset containing 3 numbers: the max, count and sum of the respective columns on server2. Likewise the "gloop" block should do the same on server3.

From what I can gather about Oracle's distributed database model, if a SQL statement references all its objects at a remote database, it is a 'remote SQL statement' and gets sent to that other database for processing, with the results sent back. My understanding is that this will not occur if there are multiple remote databases referenced - or will the optimiser consider the subselects as separate statements and execute them remotely like this?

Thanks in advance,
Andrzej

P.S., I know the second nested subselect isn't really necessary - but at least it illustrates the multiple nesting that I'll be using in the actual application!

except so far as I know, with the code as above server1 will retreive all the columns of server2's table, and then do all the calculations itself. For large applications (and greater levels of nesting) this means a lot of data travelling over the wires

Oracle will not necessarily choose that approach. Have you confirmed that it is doing this?

Views are a good idea for explicitly telling Oracle to execute a certain part of a query remotely. One misgiving I have is that the queries aren't likely to be the same each time, and are created dynamically - how expensive (and indeed possible) would it be to create a view on each remote server from the 'master' server and then execute the SQL, each time? Is there a better way to do this?

As you may have guessed I haven't had a great deal of experience with Oracle, so I'm just trying to get a feel for the standard ways of handling distributed remote queries.