我正在使用 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
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=7200
andtcp_keepalives_interval=7200
和tcp_keepalives_count=5
,但连接在 10-15 分钟后仍然丢失。