PostgreSQL:有没有办法在空表中逐个插入我的数据以减少加载时间?

PostgreSQL:有没有办法在空表中逐个插入我的数据以减少加载时间?,sql,postgresql,loops,time,sql-insert,Sql,Postgresql,Loops,Time,Sql Insert,我有一个非常简单的查询,如下所示 drop table if exists table1; create table table1 select account, campaign_id, customer_id, type, min(date) as date_min from table2 where account in ('1','2') and campaign_id not in ('1','2','3') group by a

我有一个非常简单的查询,如下所示

drop table if exists table1;
create table table1
  select
    account,
    campaign_id,
    customer_id,
    type,
    min(date) as date_min
  from table2
  where account in ('1','2') and campaign_id not in ('1','2','3')
  group by account, campaign_id,customer_id,type;
然而,每次我运行它时,都会收到这样一条错误消息:“java.io.IOException:连接被远程主机关闭”。我相信这是因为数据太大了

有人建议我循环“insert into”语句,并将数据逐块添加到空表中

所以我试着逐年添加数据。以下代码仅适用于1年。我需要插入多次

insert into loop_dk (account, campaign_id, customer_id, type, date_min)
select account,
       campaign_id,
       customer_id,
       type,
       min(date) as date_min
from dk
where account in ('1', '2')
  and campaign_id not in ('1', '2', '3') and date between '2016-11-29' and '2017-11-29'
group by account, campaign_id, customer_id, type;

有人能告诉我循环“insert-into”语句是否好,或者建议我任何其他可以防止上述错误消息的解决方案吗


非常感谢你的帮助

“连接被远程主机关闭”可能表示您的语句超时太低。您可以使用例如
语句.setQueryTimeout(0)
来禁用它。或者服务器上定义了一个限制,然后尝试
set语句\u timeout=0
查看PostgreSQL服务器的日志文件,查看它关闭连接的原因。为什么要重复运行这样的查询?看起来你真正需要的是一个具体化的观点。谢谢大家的建议!我会处理它们。@Bergi我们使用的是Amazon红移,不幸的是,它不支持物化视图。