我创建了一种在计算机上可以正常运行(大约需要4秒钟)的方法。但是,最终用户将在远程桌面环境中使用该方法,在该环境中,相同的请求需要25到50秒才能完成。如何优化该程序?

private void compareAndPopulateArrays(List<String> listOfGenIdsFromXml,
        List<String> listOfGenIdsFromDB, String dburl)
        throws ClassNotFoundException, SQLException {
    mdbAccessor = new MDBAccessor();
    for (int x = 0; x < listOfGenIdsFromXml.size(); x++) {
        Boolean matching_id_found = false;
        for (int y = 0; y < listOfGenIdsFromDB.size(); y++) {
            if (listOfGenIdsFromXml.get(x)
                    .equals(listOfGenIdsFromDB.get(y)) || equalsLanguageCodeIgnore(listOfGenIdsFromXml.get(x),listOfGenIdsFromDB.get(y))) {
                addNewMatchingRecognition(listOfGenIdsFromXml,
                        listOfGenIdsFromDB, dburl, x, y);
                matching_id_found = true;
            }
        }
        if (!(matching_id_found == true)) {
            newRecognitions.add(new NewRecognition(listOfGenIdsFromXml
                    .get(x)));
        }
    }
}
    private void addNewMatchingRecognition(List<String> listOfGenIdsFromXml,
        List<String> listOfGenIdsFromDB, String dburl, int x, int y)
        throws ClassNotFoundException, SQLException {
    String gen_id_Xml = listOfGenIdsFromXml.get(x);
    String gen_id_DB = listOfGenIdsFromDB.get(y);
    int issue_id = mdbAccessor.getIssueId(gen_id_DB, dburl);
    String issue_expression = mdbAccessor.getIssueExpression(gen_id_DB,
            dburl);
    String issue_detail = mdbAccessor.getIssueDetails(gen_id_DB, dburl);
    matchingRecognitions.add(new MatchingRecognition(gen_id_Xml, gen_id_DB,
            issue_id, issue_detail, issue_expression));
}


并且所有的mdbAccessor方法看起来都与以下类似:

public int getIssueId(String gen_id, String dburl) throws ClassNotFoundException,
        SQLException {
    Connection connection = setupConnection(dburl);
    Statement statement = connection.createStatement();
    ResultSet resultSet = statement
            .executeQuery("SELECT issue_id FROM es_it WHERE gen_id='&&"
                    + gen_id + "' OR gen_id='&" + gen_id + "'");

    if (resultSet.next()){
        int getint = resultSet.getInt(1);
        resultSet.close();
        connection.close();
        return getint;
    }else{
        resultSet.close();
        connection.close();
        return -1;
    }
}


equalsLanguageCodeIgnore:

    public boolean equalsLanguageCodeIgnore(String gen_id, String gen_id_DB) {
    if (genIdsAreEqualMinusLanguageCode(gen_id, gen_id_DB)) {
        return true;
    } else {
        return false;
    }
}

private boolean genIdsAreEqualMinusLanguageCode(String gen_id,
        String gen_id_DB) {
    return gen_id_DB.contains("P-XX-")
            && gen_id.substring(5).equals(gen_id_DB.substring(5));
}


新增和改进的MDBAccessor类:

public class MDBAccessor {
private Connection connection;
private Statement statement;

public void setupConnection(String dburl)
        throws ClassNotFoundException, SQLException {
        connection = DriverManager
            .getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq="
                    + dburl);
        statement = connection.createStatement();
}

public void closeConnection() throws SQLException{
    connection.close();
}
////
public int getIssueId(String gen_id) throws ClassNotFoundException,
            SQLException {
        ResultSet resultSet = statement
                .executeQuery("SELECT issue_id FROM es_it WHERE gen_id='&&"
                        + gen_id + "' OR gen_id='&" + gen_id + "'");

        if (resultSet.next()){
            int getint = resultSet.getInt(1);
            resultSet.close();
            return getint;
        }else{
            resultSet.close();
            return -1;
        }
    }

最佳答案

获取项目一次,然后传递项目,而不是列表和索引。这将限制在列表中完成的查找数量,我预计这会很大。根据您的Java版本,您可能需要使用for-each构造以提高可读性。
我希望您可以将数据库访问合并到单个查询中,这样可以节省时间。

int issue_id = mdbAccessor.getIssueId(gen_id_DB,dburl);
 字符串issue_expression = mdbAccessor.getIssueExpression(gen_id_DB,
            dburl);
 字符串issue_detail = mdbAccessor.getIssueDetails(gen_id_DB,dburl);
您似乎正在打开和关闭每个查询的数据库。将其打开一次,然后在功能结束时将其关闭,因为打开和关闭数据库连接非常昂贵(尤其是对Access IIRC而言)。您可能希望使连接对象成为您的MDBAccessor类的成员。请记住使用try finally构造以确保其已关闭。




建议重构以提高可读性

private void compareAndPopulateArrays(List<String> xmlGenIds,
        List<String> dbGenIds, String dbUrl)
        throws ClassNotFoundException, SQLException {
    //Better yet move it into an init method or the class constructor
    mdbAccessor = new MDBAccessor(dbUrl);
    for (String currXmlId : xmlGenIds) {
        Boolean matchingIdFound = false;
        for (String currDbId : dbGenIds) {
            if (currXmlId.equals(currDbId) ||
                    equalsLanguageCodeIgnore(currXmlId,currDbId)) {
                addNewMatchingRecognition(currDbId, currXmlId);
                matchingIdFound = true;
            }
        }
        if (!matchingIdFound) {
            newRecognitions.add(new NewRecognition(currDbId));
        }
    }
}

关于java - 如何优化此嵌套的for循环?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5080513/

10-10 10:53