8

外部テーブル (postgresql_fdw外部データ ラッパーを使用) があり、すべてのレコードをコピーするための最大 ID を見つける必要があります。実行するSELECT MAX(id) FROM foreign_tableと、インデックスを使用していないようです:

Aggregate  (cost=205.06..205.07 rows=1 width=4) (actual time=13999.535..13999.535 rows=1 loops=1)
  ->  Foreign Scan on foreign_table  (cost=100.00..197.75 rows=2925 width=4) (actual time=1.288..13632.768 rows=1849305 loops=1)
Planning time: 0.087 ms
Execution time: 14019.179 ms

SELECT MAX(id) FROM table「実際の」テーブルで同じクエリ ( ) を実行すると、インデックスが使用されます。

Result  (cost=0.45..0.46 rows=1 width=0) (actual time=0.164..0.165 rows=1 loops=1)
  InitPlan 1 (returns $0)
    ->  Limit  (cost=0.43..0.45 rows=1 width=4) (actual time=0.152..0.153 rows=1 loops=1)
       ->  Index Only Scan Backward using table_pkey on table  (cost=0.43..46102.55 rows=1821907 width=4) (actual time=0.143..0.143 rows=1 loops=1)
             Index Cond: (id IS NOT NULL)
             Heap Fetches: 1
Total runtime: 0.270 ms

外部テーブルを持つデータベースのバージョンは 9.4.4 で、「実際の」テーブルを持つデータベースのバージョンは 9.3.9 です。

最初のクエリでインデックスを使用する方法はありますか?

4

1 に答える 1

11

Postgres_fdw はインデックスにアクセスできません。リモートサーバーでビューを使用します。例:

create view test_max as
select max(val) max_val
from test;

ローカル サーバーで、リモート ビューのラッパーを定義します。

create foreign table back_test_max (
    max_val int
)
    server back_server
    options (schema_name 'public', table_name 'test_max');

選択する back_test_maxとリモート ビューが使用されるため、元のリモート テーブルのインデックスも使用されます。

于 2015-11-23T18:42:40.243 に答える