Heterogeneous queries can be slow even if both RDBMS systems used in the heterogeneous query are the same (e.g. two Oracle databases.)
The engine used for heterogeneous queries works by “vertically” splitting the query by table.
Example
ORDER_DATES (Oracle table) with columns ORDER_ID (primary key), ORDER_DATE
ORDER_AMTS (SQL Server table) with columns ORDER_ID (primary key), ORDER_AMT
Query:
select
od.order_id, od.order_date, oa.order_amt
from
[ODBC connection to Oracle.ORDER_DATES] od
inner join [ODBC connection to SQL Server.ORDER_AMTS] oa
on (od.order_id = oa.order_id) ;
The Oracle database engine will see the query
“select order_id, order_date from order_dates”
The SQL Server database will see the query
“select order_id, order_amt from order_amts”
Both result sets (notice that this is a full table scan in both databases) are returned to the client and the heterogenous query engine on the client will do the joining and eliminating rows that don’t match.
Example 2:
select
od.order_id, od.order_date, oa.order_amt
from
[ODBC connection to Oracle.ORDER_DATES] od
inner join [ODBC connection to SQL Server.ORDER_AMTS] oa
on (od.order_id = oa.order_id)
where
od.order_id = 1 ;
The Oracle database engine will see the query
“select order_id, order_date from order_dates where order_id = 1”
The SQL Server database will see the query
“select order_id, order_amt from order_amts”
Only one row is returned from Oracle, but all the rows are returned to the client from SQL server and the heterogenous query engine on the client will do the joining and eliminating rows that don’t match.
Example 3:
select
od.order_id, od.order_date, oa.order_amt
from
[ODBC connection to Oracle.ORDER_DATES] od
inner join [ODBC connection to SQL Server.ORDER_AMTS] oa
on (od.order_id = oa.order_id)
where
od.order_id = 1 and oa.order_id = 1 ;
The Oracle database engine will see the query
“select order_id, order_date from order_dates where order_id = 1”
The SQL Server database will see the query
“select order_id, order_amt from order_amts where order_id = 1”
Only one row is returned from Oracle, and only one row is returned from SQL server. (The heterogenous query engine on theclient will do the joining and eliminating rows that don’t match.) This will run fast because both databases are reading using an index and returning one row. So even though the where condition looks redundant it is a good thing to put both conditions in: “od.order_id = 1 AND oa.order_id = 1”