【问题标题】:Postgres Foreign Data Wrapper Aggregate function pushdownPostgres 外部数据包装器聚合函数下推
【发布时间】:2017-05-10 14:32:33
【问题描述】:

情况:

  • 我在 postgres 数据库 (db1) 中创建表 (foreign_table)
  • 我使用 postgres_fdw 在不同的 postgres 数据库 (db2) 中为 foreign_table 创建了一个外部日期包装器
  • 然后我从 db2 中执行“select count(*) from foreign_table”
  • 此查询以 100 行为一组(由 fetch_size 设置)将 foreign_table 的全部内容返回到 db1。

问题:

  • 这会导致查询速度非常慢,因为 foreign_table 有大约 1 亿行。

我的问题:

是否可以“下推”这个聚合函数,以便在远程 postgres 数据库上执行 count(*)?

【问题讨论】:

    标签: postgresql aggregate-functions foreign-data-wrapper


    【解决方案1】:

    如果您不想等待 Postgres 10,请使用以下解决方法:

    在国外数据库中创建视图:

    -- in db1:
    create view count_my_table as (
        select count(*) 
        from foreign_table);
    

    为本地数据库中的视图创建一个外表:

    -- in db2:
    create foreign table count_my_table (
        count bigint
    )
    server foreign_server
    options (table_name 'count_my_table');
    
    select count 
    from count_my_table;
    

    【讨论】:

      【解决方案2】:

      我将继续回答我自己的问题。

      外部表的聚合下推将出现在 postgres 10 中。

      详情请见https://www.enterprisedb.com/blog/postgresql-aggregate-push-down-postgresfdw

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多