tangguo

改善此方案性能的方法

android

我有一张地图,其中填充了大量数据(大约有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循环内执行上述操作而导致的性能问题。

更新:

情况是。我正在迭代一个包含大量数据的地图,

在迭代它时,我正在获取用户ID,并且我必须对用户ID进行计算。例如,考虑userid+some constants并应该在数据库表中对其进行更新。

并且还应该与我地图中的列表值一起添加到电子邮件内容中

所以我认为无法进行批量更新,我的理解正确吗?

我应该遵循这种方法吗?或有其他选择


阅读 267

收藏
2020-11-11

共1个答案

小编典典

for循环由于两个原因而花费时间。
1)个人电子邮件通过减少传输连接来
改善它2)个人提交通过以下方法来改善它

所以理想的情况是同时处理这两种情况,我建议批量处理1000次,然后再打数字

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();
    }
}
2020-11-11