Postgresのdblinkの代わりにtemporary tableで対応

Postgresのdblinkを使って異なるDB間のテーブルをJOINすると、、、
ものすごく遅いことが判明。(fetchは利用できなかった)

検索条件を異なるDBにまたがって指定すると、
とても採用できないレベルの速度になってしまいます。
ていうか基本タイムアウト

//db1.test1
//db2.test2

select * from test1 a, test2 b 
where a.id = b.id and a.name = 'aaa' and b.age = '11';


ので、結局temporary tableを使用することにしました。

//db1.test1
//db2.test2

select * from test2 where age = '11'; //$data取得
create temporary table temp1 (id integer, age character(3));

foreach($data as $key => $val){
    $id = $val["id"];
    $age = $val["age"];
    insert into temp1 (id, age) value($id, $age);
}

create temporary table temp2 as ( 
    select b.age, a.name from temp1 b, test1 a where a.id = b.id
);

select from * temp2 where a.name = 'aaa';


かなり面倒ですがそれでも3倍以上早くなりました。

ただ、、、もっと他に良い方法ある気がする;



追記:
最終的には上記でも遅いから登録時に片方のテーブルにもう片方の一部のデータを登録するようにして、データベース間の連携をなくしました。
残念。