我正在使用 postgres_fdw 在两个数据库之间创建链接。然后我设置外部表并从外部表插入到我的实时表中。我注意到这需要相当长的时间,因为它们没有索引。
您可以在外部表上创建索引吗,它是标准的吗
CREATE INDEX ON foreign_table_name (column)?
我正在使用 postgres_fdw 在两个数据库之间创建链接。然后我设置外部表并从外部表插入到我的实时表中。我注意到这需要相当长的时间,因为它们没有索引。
您可以在外部表上创建索引吗,它是标准的吗
CREATE INDEX ON foreign_table_name (column)?
不,你会得到一个错误:
ERROR: cannot create index on foreign table "tablename"
********** Error **********
ERROR: cannot create index on foreign table "tablename"
SQL state: 42809
这是有道理的,因为每次查询表时,查询都会“穿越”网络并从原始数据库中检索数据(不会将数据存储到索引中)。
您可以做的是使用详细解释来获取正在另一端执行的查询,并相应地索引远程表。
explain verbose select * from schema.foreign_table
"Foreign Scan on schema.foreign_table (cost=25.00..1025.00 rows=1000 width=84)"
" Output: field1, field2, field3
" Remote server startup cost: 25"
" Remote query: SELECT field1, field2, field3 FROM schema.original_table
希望有帮助。祝你好运!