寻找以下代码的优化

问题描述 投票:-1回答:1

我发现下面的代码错误,因为它降低了extjs3网格的性能,根据我的分析,我正在寻找在查询或代码级别进行优化的可能性,如果我们提取出该查询,则会有两个嵌套的内部查询在响应此外,while loop内部的代码试图查找唯一的ID速度很慢,我们不能在查询中使用distinct,还是不能使用联接而不是内部查询。

请向我建议最佳实践,以实现优化。

public boolean isSCACreditOverviewGridVisible(String sessionId) {
        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        boolean result = false;
        try {
            CommonUtility commUtil = new CommonUtility();

            List<String> hmIds = new ArrayList<String>();
            Map<String, String> tmStockMap = new TreeMap<String, String>();
            Set<String> setRecentCertificate = new HashSet<String>();

            String managerAccountId = sessionInfo.getMembershipAccount();

            String stockQuery = " select memberId , RootCertficateId from stockposition sp  where sp.stocktype = 'TR' and sp.memberId "
                + " IN  ( select hm2.accountId from "
                DATALINK
                + ".holdingmembers  hm2 "
                + " where hm2.holdingId = ( select holdingId from "
                DATALINK
                + ".holdingmembers  hm1 where hm1.accountId =  ? )) "
                + " order by sp.createdDate desc ";

            conn = getChildDBConnection();
            if (null != conn) {
                ps = conn.prepareStatement(stockQuery);
                ps.setString(1, managerAccountId);
                rs = ps.executeQuery();
                if (null != rs) {
                    while (rs.next()) {
                        String memberId = rs.getString("memberId");
                        String rootCertficateId = rs
                        .getString("RootCertficateId");
                        if (tmStockMap.containsKey(rootCertficateId)) {
                            continue;
                        }
                        hmIds.add(memberId);
                        tmStockMap.put(rootCertficateId, memberId);
                    }
                }
                rs.close();
                ps.close();

                if (null != hmIds && !hmIds.isEmpty()) {
                    String inIds = commUtil.getInStateParam(hmIds);
                    String mostRecentLicense = "Select RootCertificateId , memberaccountid  from "
                        + OctopusSchema.octopusSchema
                        + ".certificate c where  c.memberaccountid IN ("
                        + inIds
                        + ") and c.isrootcertificate=0 and c.certificationstatusid > 1 order by c.modifieddate desc";
                    ps = conn.prepareStatement(mostRecentLicense);
                    rs = ps.executeQuery();
                    if (null != rs) {
                        while (rs.next()) {
                            String rootCertficateId = rs
                            .getString("RootCertificateId");
                            String memberaccountid = rs
                            .getString("memberaccountid");
                            if (setRecentCertificate.contains(memberaccountid)) {
                                continue;
                            }
                            setRecentCertificate.add(memberaccountid);
                            if (tmStockMap.containsKey(rootCertficateId)) {
                                result = true;
                                break;
                            }
                        }
                    }
                    rs.close();
                    ps.close();
                } else {
                    result = false;
                }
            }
        } catch (Exception e) {
            LOGGER.error(e);
        } finally {
            closeDBReferences(conn, ps, null, rs);
        }
        return result;
    }

QUERY:

 select RootCertficateId,memberId from stockposition sp  where sp.stocktype = 'TR' and sp.memberId 
                  IN  ( select hm2.accountId from 
                DATALINK.holdingmembers  hm2 
                    where hm2.holdingId = ( select holdingId from 
                DATALINK.holdingmembers  hm1 where hm1.accountId =  '4937' )) 
                    order by sp.createdDate DESC; 

enter image description here

java sql jdbc
1个回答
0
投票

一种快速的方法是用EXISTS替换您的IN。如果内部查询返回很多行,则效率会高得多。这取决于您的子查询是否返回很多结果。

SQL Server IN vs. EXISTS Performance

© www.soinside.com 2019 - 2024. All rights reserved.