【问题标题】:strategies to get data from diff database from diff tables从不同表中获取不同数据库数据的策略
【发布时间】:2013-04-16 21:59:45
【问题描述】:

问题

  • 我有两个数据库 user_works 数据库和定价数据库。
  • 我想加入两个表 user_table 这是在 user_works 使用 user_id 在定价数据库中的数据库和rent_table

到目前为止我做了什么

  • 我第一次调用 user_works db。
  • 从 user_table 中获取 id。
  • 然后将 id 传递给定价数据库中的rent_table

例子:

$user_id = select user_id fron user_table WHERE name=abc

将 $user_id 传递给 dbObject user_works db 并获取用户 ID

$rent = select user_id,rent from rent_table WHERE user_id IN ( $user_ids )

将 $rent 传递给定价数据库的 dbObject 我想在 php 应用程序中执行此操作

有没有更好的方法来做到这一点。定价数据库真的很重吗?比如使用临时表等。

【问题讨论】:

  • 我正在使用 postgresql
  • 什么是“diff 数据库”?

标签: sql postgresql optimization query-optimization


【解决方案1】:

使用定价数据库连接中的 dblink

select r.*
from
    dblink(
        'dbname=user_works password=password user=user',
        $$ select user_id from user_table where name = 'abc' $$
    ) as u(user_id int)
    inner join
    rent_table r on r.user_id = u.user_id

您需要以超级用户身份创建 dblink 扩展:

create extension dblink;

或者从远程数据库创建一个本地视图:

create view user_table as
select *
from
    dblink(
        'dbname=user_works password=password user=user',
        $$ select user_id, name from user_table $$
    ) as t(user_id int, name text)
;

现在像本地一样查询它:

select * from user_table where name = 'abc';

【讨论】:

  • 我可以使用像临时表这样的东西,可以创建它然后对其进行连接。这可能吗???
  • @Abhishek 已编辑。你可以用同样的方法创建一个临时表,但我想视图会更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多