小编Zha*_*eng的帖子

如何使用索引加快 postgres 中的排序

我正在使用 postgres 9.4。

messages具有以下模式:消息属于FEED_ID,并且具有posted_at,还消息可以具有(在答复的情况)的父消息。

                    Table "public.messages"
            Column            |            Type             | Modifiers
------------------------------+-----------------------------+-----------
 message_id                   | character varying(255)      | not null
 feed_id                      | integer                     |
 parent_id                    | character varying(255)      |
 posted_at                    | timestamp without time zone |
 share_count                  | integer                     |
Indexes:
    "messages_pkey" PRIMARY KEY, btree (message_id)
    "index_messages_on_feed_id_posted_at" btree (feed_id, posted_at DESC NULLS LAST)
Run Code Online (Sandbox Code Playgroud)

我想返回由 排序的所有消息share_count,但对于每个parent_id,我只想返回一条消息。即,如果多条消息具有相同的parent_id,则仅posted_at返回最新的一条 ( )。在parent_id可以为空,以空消息parent_id都应该回报。

我使用的查询是:

WITH filtered_messages AS (SELECT * 
                           FROM messages
                           WHERE feed_id …
Run Code Online (Sandbox Code Playgroud)

postgresql performance index sorting postgresql-9.4 postgresql-performance

10
推荐指数
1
解决办法
2万
查看次数

postgres \copy 文件到 RDS 超时

Postgres 9.3 版,托管在 Amazon RDS 上。我有一个messages包含 10G 数据的表。我psql在 EC2 上使用连接了数据库。然后用

\COPY (select * from messages) TO '/tmp/messages.csv' WITH (FORMAT CSV, FORCE_QUOTE *)
Run Code Online (Sandbox Code Playgroud)

将所有消息复制到 EC2 上的文件中。

然后我尝试将文件复制回表:

\COPY messages FROM '/tmp/messages.csv' WITH (FORMAT CSV)
Run Code Online (Sandbox Code Playgroud)

大约 5 分钟后,我总是收到此错误。这个messages.csv文件大约10G。

connection not open
The connection to the server was lost. Attempting reset: Succeeded.
Run Code Online (Sandbox Code Playgroud)

我尝试了一个愚蠢的解决方案,通过将文件拆分为多个较小的主干来减少输入数据的大小。即每个 400MB。

split -l 1000000 messages.csv messages_
Run Code Online (Sandbox Code Playgroud)

这会创建多个较小的文件,每个文件 400MB。这工作正常。

但是有没有我可以更改的配置来保持大文件的连接?

我尝试设置tcp_keepalives_idle=7200andtcp_keepalives_interval=7200tcp_keepalives_count=5,但连接在 10-15 分钟后仍然丢失。

postgresql copy

5
推荐指数
0
解决办法
2096
查看次数