在外表PostgreSQL上创建索引

ura*_*aza 6 postgresql indexing postgres-fdw

我使用postgres_fdw在两个数据库之间创建链接.然后我设置外部表并从外表执行一些插入到我的实时表.我注意到它需要相当长的时间,因为它们没有索引.

你可以在外表上创建一个索引,它是标准的吗?

CREATE INDEX ON foreign_table_name (column)?  
Run Code Online (Sandbox Code Playgroud)

小智 7

不,您会得到一个错误:

ERROR:  cannot create index on foreign table "tablename"
********** Error **********

ERROR: cannot create index on foreign table "tablename"
SQL state: 42809
Run Code Online (Sandbox Code Playgroud)

这很有意义,因为查询将在每次查询表时“遍历”网络并从原始数据库中检索数据(不会将数据存储到索引中)。

您可以做的是使用verbose解释来获取另一侧正在执行的查询,并相应地索引远程表。

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
Run Code Online (Sandbox Code Playgroud)

希望能有所帮助。祝好运!