Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id C3B9C9FC215; Fri, 8 Jun 2007 05:29:49 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 86892-03; Fri, 8 Jun 2007 05:29:40 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from interscan.indicator.com (mail.indicator.be [193.121.199.3]) by postgresql.org (Postfix) with ESMTP id 6DB589FC239; Fri, 8 Jun 2007 05:29:41 -0300 (ADT) Received: from webaccess.indicator.be ([10.100.1.7]) by interscan.indicator.com with InterScan Message Security Suite; Fri, 08 Jun 2007 10:14:38 +0200 Received: from DOMEXT-MTA by webaccess.indicator.bewith Novell_GroupWise; Fri, 08 Jun 2007 10:29:36 +0200 Message-Id: <46692F86.A3DD.0030.0@indicator.be> X-Mailer: Novell GroupWise Internet Agent 7.0.1 Date: Fri, 08 Jun 2007 10:29:26 +0200 From: "Bart Degryse" To: , Cc: "Loredana Curugiu" , "Sean Davis" Subject: Re: [NOVICE] Query with tables from 2 different databasesinJava References: <1c23c8e70706070708m2f3be6f9o75571fd8a82ee390@mail.gmail.com> <4 668154A.3090105@mail.nih.gov> <4668390D.A3DD.0030.0@indicator.be><1c23c8e70706070850h7696955aw2bfc8cee38d 7b43f@mail.gmail.com><1c23c8e70706070850h7696955aw2bfc8cee38d7b43f@mail.gma il.com><46682AF8.1040901@mail.nih.gov> In-Reply-To: <46682AF8.1040901@mail.nih.gov> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="=__Part22050076.2__=" X-imss-version: 2.047 X-imss-result: Passed X-imss-approveListMatch: *@indicator.be X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.553 tagged_above=0 required=5 tests=AWL=-0.081, BAYES_50=0.001, FORGED_RCVD_HELO=0.135, HTML_40_50=0.496, HTML_MESSAGE=0.001, UNPARSEABLE_RELAY=0.001 X-Spam-Level: X-Archive-Number: 200706/34 X-Sequence-Number: 17096 --=__Part22050076.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Actually you can. Create 2 srf function, one for each database. Then use them as tables in a = select statement. In my example I will select joined data from an Oracle and a PostgreSQL = database from within another PostgreSQL database. =20 On Oracle CREATE TABLE ADDRESSFORMATHEADING ( ADDRFORMAT VARCHAR2(10 BYTE) DEFAULT '.' NOT NULL, NAME VARCHAR2(30 BYTE) DEFAULT '.' NOT NULL, DATAAREAID VARCHAR2(3 BYTE) DEFAULT 'dat' NOT NULL, RECID NUMBER(10) NOT NULL ) Insert into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) = Values ('national', 'This country', 'ash', 30); Insert into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) = Values ('be', 'Address Belgium', 'lil', 501); Insert into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) = Values ('fr', 'Address France', 'lil', 496); Insert into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) = Values ('it', 'Italie', 'bol', 3138); Insert into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) = Values ('national', 'National', '012', 687181679); Insert into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) = Values ('internatio', 'International countries', 'ash', 29); Insert into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) = Values ('be', 'Beglie', 'bol', 3187); Insert into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) = Values ('sp', 'Address Spain', 'bar', 1302174); Insert into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) = Values ('internatio', 'International countries', 'as0', 29); Insert into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) = Values ('national', 'This country', 'as0', 30); Insert into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) = Values ('national', 'National', '011', 216774985); Insert into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) = Values ('internatio', 'International', '011', 216774984); Insert into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) = Values ('national', 'National', 'hlm', 451094066); Insert into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) = Values ('internatio', 'International', 'hlm', 451094067); =20 On PostgreSQL database webdev CREATE TABLE public.def_dataarea ( dataareaid varchar(3) PRIMARY KEY, description text NOT NULL ); INSERT INTO def_dataarea VALUES ('ash', 'Ashford'); INSERT INTO def_dataarea VALUES ('lil', 'Lille'); INSERT INTO def_dataarea VALUES ('bol', 'Bolognia'); INSERT INTO def_dataarea VALUES ('012', '012'); INSERT INTO def_dataarea VALUES ('bar', 'Barcelona'); INSERT INTO def_dataarea VALUES ('011', 'Leuven'); INSERT INTO def_dataarea VALUES ('hlm', 'Helmond'); =20 On PostgreSQL database defrev_dev CREATE TYPE db1_row AS ("ADDRFORMAT" varchar(10), "NAME" varchar(30), = "DATAAREAID" varchar(3), "RECID" numeric(10,0)); CREATE TYPE db2_row AS (dataareaid varchar(3), description text); =20 CREATE OR REPLACE FUNCTION "public"."data_from_db1" (text) RETURNS SETOF = db1_row AS $body$ use DBI; my $query =3D shift; return unless (defined $query); my $dbh_ora =3D DBI->connect('dbi:Oracle:database=3Dbmssa;host=3Dfirev120-1= .indicator.be;sid=3Dmars', 'bmssa', '********') or die "Couldn't connect to database: " . DBI->errstr; my $sel =3D $dbh_ora->prepare($query) or die "Couldn't prepare statement: " . $dbh_ora->errstr; $sel->execute() or die "Couldn't execute statement: " . $sel->errstr; while (my $row =3D $sel->fetchrow_hashref) { return_next($row); } $dbh_ora->disconnect; return; $body$ LANGUAGE 'plperlu' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER; =20 CREATE OR REPLACE FUNCTION "public"."data_from_db2" (text) RETURNS SETOF = db2_row AS $body$ use DBI; my $query =3D shift; return unless (defined $query); my $dbh_pg =3D DBI->connect('dbi:Pg:dbname=3Dwebdev;host=3D10.100.1.21;po= rt=3D5432', 'logstock_sys', '********') or die "Couldn't connect to database: " . DBI->errstr; my $sel =3D $dbh_pg->prepare($query) or die "Couldn't prepare statement: " . $dbh_pg->errstr; $sel->execute() or die "Couldn't execute statement: " . $sel->errstr; while (my $row =3D $sel->fetchrow_hashref) { return_next($row); } $dbh_pg->disconnect; return; $body$ LANGUAGE 'plperlu' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER; =20 select A."ADDRFORMAT", A."NAME", A."RECID", A."DATAAREAID", COALESCE(B.desc= ription, 'not defined yet') as "DATAAREA" from data_from_db1('SELECT * FROM AddressFormatHeading') A left join = data_from_db2('SELECT * FROM def_dataarea') B on A."DATAAREAID" =3D = B.dataareaid where A."ADDRFORMAT" like '%natio%'; =20 ADDRFORMATNAMERECIDDATAAREAIDDATAAREA nationalThis country30ashAshford nationalNational687181679012012 internatioInternational countries29ashAshford internatioInternational countries29as0not defined yet nationalThis country30as0not defined yet nationalNational216774985011Leuven internatioInternational216774984011Leuven nationalNational451094066hlmHelmond internatioInternational451094067hlmHelmond =20 Some remarks - Performance is influenced by the performance capacities of all three = databases and of the network - In PostgreSQL 8.2.4 return_next(...) is still buffered in memory, = meaning that the database only returns the data to the calling environment = when the return; statement is reached. So memory is heavily used and thus = also a limiting factor in how much data you can fetch this way. Maybe in = some future version return_next will return data immediately. That being = said I have done joins of 25000 X 2000 records returning 300000 records. = It takes a while, but is acceptable. - If you connect to a certain database several time during your session = performance gain is to be found in using seperate connect and disconnect = functions which stores $dbh_ora and $dbh_pg in the global hash %_SHARED - Carefully read http://www.postgresql.org/docs/8.2/interactive/plperl.html= and the perldoc on DBI (http://search.cpan.org/~timb/DBI/DBI.pm) It's all = in there...somewhere. - I suppose this could be done with pljava as well but I have never tried. =20 Good luck =20 =20 >>> Sean Davis 2007-06-07 17:57 >>> Loredana Curugiu wrote: >=20 >=20 > On 6/7/07, *Bart Degryse* > wrote: >=20 > Personally I have no good experiences with neither dblink nor = dbi-link. > They perferm only acceptably on very few records. > My approach now is to use srf plperl functions. That approach > requires you > to have a reasonable knowledge of perl, but is in my experience the > fastest > (or at least slow) one and certainly the most flexible one. >=20 >=20 > How would you use tables from different databases in the same select > using perl? You cannot. You would need to write a function that does the queries within it and joins the results within the function. Sean --=__Part22050076.2__= Content-Type: text/html; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Description: HTML
Actually you can.
Create 2 srf function, one for each database. Then use them as = tables in a select statement.
In my example I will select joined data from an Oracle and a = PostgreSQL database from within another PostgreSQL database.
 
On Oracle
CREATE TABLE ADDRESSFORMATHEADING (
  ADDRFORMAT VARCHAR2(10 = BYTE) DEFAULT '.' NOT NULL,
  NAME VARCHAR2(30 BYTE) DEFAULT '.' = NOT NULL,
  DATAAREAID VARCHAR2(3 BYTE) DEFAULT 'dat' NOT = NULL,
  RECID NUMBER(10) NOT NULL
)
Insert into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, = RECID) Values ('national', 'This country', 'ash', 30);
Insert into = addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) Values ('be', = 'Address Belgium', 'lil', 501);
Insert into addressformatheading = (ADDRFORMAT, NAME, DATAAREAID, RECID) Values ('fr', 'Address France', = 'lil', 496);
Insert into addressformatheading (ADDRFORMAT, NAME, = DATAAREAID, RECID) Values ('it', 'Italie', 'bol', 3138);
Insert into = addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) Values = ('national', 'National', '012', 687181679);
Insert into addressformathea= ding (ADDRFORMAT, NAME, DATAAREAID, RECID) Values ('internatio', 'Internati= onal countries', 'ash', 29);
Insert into addressformatheading (ADDRFORMA= T, NAME, DATAAREAID, RECID) Values ('be', 'Beglie', 'bol', 3187);
Insert= into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) Values = ('sp', 'Address Spain', 'bar', 1302174);
Insert into addressformatheadin= g (ADDRFORMAT, NAME, DATAAREAID, RECID) Values ('internatio', 'Internationa= l countries', 'as0', 29);
Insert into addressformatheading (ADDRFORMAT, = NAME, DATAAREAID, RECID) Values ('national', 'This country', 'as0', = 30);
Insert into addressformatheading (ADDRFORMAT, NAME, DATAAREAID, = RECID) Values ('national', 'National', '011', 216774985);
Insert into = addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) Values = ('internatio', 'International', '011', 216774984);
Insert into = addressformatheading (ADDRFORMAT, NAME, DATAAREAID, RECID) Values = ('national', 'National', 'hlm', 451094066);
Insert into addressformathea= ding (ADDRFORMAT, NAME, DATAAREAID, RECID) Values ('internatio', 'Internati= onal', 'hlm', 451094067);
 
On PostgreSQL database webdev
CREATE TABLE public.def_dataarea (
  dataareaid varchar(3) = PRIMARY KEY,
  description text NOT NULL
);
INSERT INTO def_dataarea VALUES ('ash', 'Ashford');
INSERT INTO = def_dataarea VALUES ('lil', 'Lille');
INSERT INTO def_dataarea VALUES = ('bol', 'Bolognia');
INSERT INTO def_dataarea VALUES ('012', '012');
= INSERT INTO def_dataarea VALUES ('bar', 'Barcelona');
INSERT INTO = def_dataarea VALUES ('011', 'Leuven');
INSERT INTO def_dataarea VALUES = ('hlm', 'Helmond');
 
On PostgreSQL database defrev_dev
CREATE TYPE db1_row AS ("ADDRFORMAT" varchar(10), "NAME" varchar(30), = "DATAAREAID" varchar(3), "RECID" numeric(10,0));
CREATE TYPE db2_row AS (dataareaid varchar(3), description text);
 
CREATE OR REPLACE FUNCTION "public"."data_from_db1" (text) RETURNS = SETOF db1_row AS
$body$
use DBI;
my $query =3D shift;
return = unless (defined $query);
my $dbh_ora =3D DBI->connect('dbi:Oracle:dat= abase=3Dbmssa;host=3Dfirev120-1.indicator.be;sid=3Dmars', 'bmssa', = '********')
          =     or die "Couldn't connect to database: " . DBI->errstr= ;
my $sel =3D $dbh_ora->prepare($query)
    &n= bsp;     or die "Couldn't prepare statement: " . = $dbh_ora->errstr;
$sel->execute() or die "Couldn't execute = statement: " . $sel->errstr;
while (my $row =3D $sel->fetchrow_has= href) {
  return_next($row);
}
$dbh_ora->disconnect;
re= turn;
$body$
LANGUAGE 'plperlu' VOLATILE CALLED ON NULL INPUT = SECURITY INVOKER;
 
CREATE OR REPLACE FUNCTION "public"."data_from_db2" (text) RETURNS = SETOF db2_row AS
$body$
  use DBI;
  my $query =3D = shift;
  return unless (defined $query);
  my $dbh_pg =3D = DBI->connect('dbi:Pg:dbname=3Dwebdev;host=3D10.100.1.21;port=3D5432', = 'logstock_sys', '********')
       &n= bsp;       or die "Couldn't connect to = database: " . DBI->errstr;
  my $sel =3D $dbh_pg->prepare($qu= ery)
            = or die "Couldn't prepare statement: " . $dbh_pg->errstr;
  = $sel->execute() or die "Couldn't execute statement: " . $sel->errstr;=
  while (my $row =3D $sel->fetchrow_hashref) {
  &= nbsp; return_next($row);
  }
  $dbh_pg->disconnect;
&= nbsp; return;
$body$
LANGUAGE 'plperlu' VOLATILE CALLED ON NULL = INPUT SECURITY INVOKER;
 
select A."ADDRFORMAT", A."NAME", A."RECID", A."DATAAREAID", COALESCE(B= .description, 'not defined yet') as "DATAAREA"
from data_from_db1('SELEC= T * FROM AddressFormatHeading') A left join data_from_db2('SELECT * FROM = def_dataarea') B on A."DATAAREAID" =3D B.dataareaid
where A."ADDRFORMAT" like '%natio%';
 
<= /TR>
ADDRFORMAT NAME RECID DATAAREAID DATAAREA
national This country 30 ash Ashford
national National 687181679 012 012
internatio International countries 29 ash Ashford
internatio International countries 29 as0 not defined yet
national This country 30 as0 not defined yet
national National 216774985 011 Leuven
internatio International 216774984 011 Leuven
national National 451094066 hlm Helmond
internatio International 451094067 hlm Helmond
 
Some remarks
- Performance is influenced by the performance capacities of all = three databases and of the network
- In PostgreSQL 8.2.4 return_next(...) is still buffered in memory, = meaning that the database only returns the data to the calling environment = when the return; statement is reached. So memory is heavily used and = thus also a limiting factor in how much data you can fetch this way. Maybe = in some future version return_next will return data immediately. That = being said I have done joins of 25000 X 2000 records returning 300000 = records. It takes a while, but is acceptable.
- If you connect to a certain database several time during your = session performance gain is to be found in using seperate connect and = disconnect functions which stores $dbh_ora and $dbh_pg in the = global hash %_SHARED
- Carefully read http://www.postgresql.org/docs/8.2/interactive/plperl.htm= l and the perldoc on DBI (http://search.cpan.org/~timb/DBI/DBI.pm) It's all in there...som= ewhere.
- I suppose this could be done with pljava as well but I have never = tried.
 
Good luck
 
 


>>> Sean Davis <sdavis2@mail.nih.gov> = 2007-06-07 17:57 >>>
Loredana Curugiu wrote:
>
> =
> On 6/7/07, *Bart Degryse* <Bart.Degryse@indicator.be
> = <mailto:Bart.Degryse@indicator.be>> wrote:
>
> &= nbsp;   Personally I have no good experiences with neither = dblink nor dbi-link.
>     They perferm only = acceptably on very few records.
>     My = approach now is to use srf plperl functions. That approach
> &nb= sp;   requires you
>     to have a = reasonable knowledge of perl, but is in my experience the
> &nbs= p;   fastest
>     (or at least slow) = one and certainly the most flexible one.
>
>
> How = would you use tables from different databases in the same select
> = using perl?

You cannot.  You would need to write a function = that does the queries
within it and joins the results within the = function.

Sean
--=__Part22050076.2__=--