我有一张地图,其中填充了大量数据(约300,000条记录)
并如下迭代,
for (Map.Entry<String, List<ClassOBj>> entry : testMap
.entrySet()) {
// send email with map keys as email'id
// email content is populated from the list
// Perform a sql update to the column with the dyanamic value generated here with the email'id
}
如上所述,我担心在内部执行上述操作会导致性能问题。for
loop.
Update:
场景是。我正在迭代一个包含大量数据的地图,
在迭代它时,我得到了用户 ID,并且我必须计算用户 ID。例如,考虑userid+some constants
这应该在数据库表中更新。
并且还应该与我的地图中的列表值一起添加到电子邮件内容中
所以我认为批量更新是不可能的,我的理解正确吗?
我应该遵循这种方法吗?或采用任何其他想法
for 循环由于两个原因而花费时间。
1) 个人电子邮件通过减少传输连接来改进它
2)个人承诺通过以下方式改进它
所以理想的方法是同时处理这两个问题,我建议批量处理 1000 个,然后使用数字
Example
int BATCH_SIZE = 1000
conn = DriverManager.getConnection("username","password");
conn.setAutoCommit(false);
Statement stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
int count = 0;
Map<String, String> emails_map = new HashMap(BATCH_SIZE)<String, String>;
for (Map.Entry<String, List<ClassOBj>> entry : testMap
.entrySet()) {
String email = get_useremail();
String const_val = do_magic(); // this is how you are computing some constant
String body = construct_body();
count++;
String SQL = "YOUR UPDATE STATEMENT";
stmt.executeUpdate(SQL);
emails_map.put(email, body); // can create
if (count % BATCH_SIZE == 0) {
//commits all transcations
conn.commit();
//bulk send emails sending
//http://stackoverflow.com/questions/13287515/how-to-send-bulk-mails-using-javax-mail-api-efficiently-can-we-use-reuse-auth
bulk_emails_send(emails_map)
}
}
public void bulk_emails_send(Map<String, String> emails_map) {
// Get the default Session object through your setting
Session session = Session.getDefaultInstance(properties);
Transport t = session.getTransport();
t.connect();
try {
for (String email_id in emails_map) {
Message m = new MimeMessage(session);
//add to, from , subject, body
m.saveChanges();
t.sendMessage(m, m.getAllRecipients());
}
} finally {
t.close();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)