[英]How to do select and insert million records through java program faster
我正在尝试从redshift表中选择大约一百万条记录,然后需要将它们重新插入到redshift表中(经过一些操作)
但是,这花费了很长时间。 我等待了大约1个小时,程序才终止,但是没有运气。 控制台似乎也不会打印print statements
但是在打印少量语句后似乎卡住了。
尝试相同的100条记录,可以正常工作,大约需要2分钟。
这是我的代码的一部分:
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.setFetchSize(100);
ResultSet rsSelect = stmt.executeQuery("select * from table");
System.out.println("select done !");
String queryInsert = "insert into table"
+"(event_id,domain_userid,collector_tstamp,se_category,se_action,se_label,se_property)"
+"values(?,?,?,?,?,?,?)";
PreparedStatement preparedStatement = conn.prepareStatement(queryInsert);
final int batchSize = 10000;
int count = 0;
System.out.println("about to go into loop !");
while(rsSelect.next()){
String event_id = rsSelect.getString("event_id");
String domain_userid = rsSelect.getString("domain_userid");
Timestamp collector_tstamp = rsSelect.getTimestamp("collector_tstamp");
String se_category = rsSelect.getString("se_category");
String se_action = rsSelect.getString("se_action");
String se_label = rsSelect.getString("se_label");
String se_property = rsSelect.getString("se_property");
//some manipulations
preparedStatement.setString(1, event_id);
preparedStatement.setString(2, domain_userid);
preparedStatement.setTimestamp(3, collector_tstamp);
preparedStatement.setString(4, se_category);
preparedStatement.setString(5, se_action);
preparedStatement.setString(6, se_label);
preparedStatement.setString(7, se_property);
preparedStatement.addBatch();
if(++count % batchSize == 0){
preparedStatement.executeBatch();
System.out.println("batch execution!");
}
}
System.out.println("out of loop");
preparedStatement.executeBatch();
preparedStatement.close();
conn.commit();
conn.close();
我遇到了同样的问题,花费太长时间将数据从一个redshift表插入到另一个redshift表中(我使用了node.js)。 最初,我花了大约18分钟才能插入100万条记录。
我发现表中的数据没有根据排序键(时间戳)进行排序。 必须根据排序键对数据进行排序,并在where谓词中使用该sort键(如果您有where谓词)。
Run vacuum table to 100 percent
对数据进行排序。 完成操作后,请确保您根据排序键对数据进行排序。
完成此操作后,我获得了意想不到的结果。 3秒钟内插入1百万条记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.