寻求以下代码的优化。

huangapple 未分类评论49阅读模式
英文:

Looking for the optimization of the below code

问题

以下是翻译好的内容:

我发现下面的代码有错误,因为它降低了extjs3网格的性能,我正在寻找在查询或代码层面进行优化的可能性。根据我的分析,如果我们提取出查询,那么有两个嵌套的内部查询响应速度较慢,此外,在“while循环”内部的代码试图查找唯一的ID,我们不能在查询中使用“distinct”或连接来替代内部查询。

请为我提供最佳实践,以实现优化。

  1. public boolean isSCACreditOverviewGridVisible(String sessionId) {
  2. Connection conn = null;
  3. ResultSet rs = null;
  4. PreparedStatement ps = null;
  5. boolean result = false;
  6. try {
  7. CommonUtility commUtil = new CommonUtility();
  8. List<String> hmIds = new ArrayList<String>();
  9. Map<String, String> tmStockMap = new TreeMap<String, String>();
  10. Set<String> setRecentCertificate = new HashSet<String>();
  11. String managerAccountId = sessionInfo.getMembershipAccount();
  12. String stockQuery = " select memberId , RootCertficateId from stockposition sp where sp.stocktype = 'TR' and sp.memberId "
  13. + " IN ( select hm2.accountId from "
  14. + DATALINK
  15. + ".holdingmembers hm2 "
  16. + " where hm2.holdingId = ( select holdingId from "
  17. + DATALINK
  18. + ".holdingmembers hm1 where hm1.accountId = ? )) "
  19. + " order by sp.createdDate desc ";
  20. conn = getChildDBConnection();
  21. if (null != conn) {
  22. ps = conn.prepareStatement(stockQuery);
  23. ps.setString(1, managerAccountId);
  24. rs = ps.executeQuery();
  25. if (null != rs) {
  26. while (rs.next()) {
  27. String memberId = rs.getString("memberId");
  28. String rootCertficateId = rs.getString("RootCertficateId");
  29. if (tmStockMap.containsKey(rootCertficateId)) {
  30. continue;
  31. }
  32. hmIds.add(memberId);
  33. tmStockMap.put(rootCertficateId, memberId);
  34. }
  35. }
  36. rs.close();
  37. ps.close();
  38. if (null != hmIds && !hmIds.isEmpty()) {
  39. String inIds = commUtil.getInStateParam(hmIds);
  40. String mostRecentLicense = "Select RootCertificateId , memberaccountid from "
  41. + OctopusSchema.octopusSchema
  42. + ".certificate c where c.memberaccountid IN ("
  43. + inIds
  44. + ") and c.isrootcertificate=0 and c.certificationstatusid > 1 order by c.modifieddate desc";
  45. ps = conn.prepareStatement(mostRecentLicense);
  46. rs = ps.executeQuery();
  47. if (null != rs) {
  48. while (rs.next()) {
  49. String rootCertficateId = rs.getString("RootCertificateId");
  50. String memberaccountid = rs.getString("memberaccountid");
  51. if (setRecentCertificate.contains(memberaccountid)) {
  52. continue;
  53. }
  54. setRecentCertificate.add(memberaccountid);
  55. if (tmStockMap.containsKey(rootCertficateId)) {
  56. result = true;
  57. break;
  58. }
  59. }
  60. }
  61. rs.close();
  62. ps.close();
  63. } else {
  64. result = false;
  65. }
  66. }
  67. } catch (Exception e) {
  68. LOGGER.error(e);
  69. } finally {
  70. closeDBReferences(conn, ps, null, rs);
  71. }
  72. return result;
  73. }

查询:

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

图片链接在这里:图片1图片2

英文:

I have found below code buggy as it degrades the performance of extjs3 grid, i am looking for possibilities of optimization at query or code level, as per my analysis, if we extract out the query there are two nested inner queries which are responding slow, in addition, the code inside while loop trying to find the unique id, can't we have distinct in query, or joins rather than inner queries.

Please suggest me the best practice to follow in order to achieve optimization.

  1. public boolean isSCACreditOverviewGridVisible(String sessionId) {
  2. Connection conn = null;
  3. ResultSet rs = null;
  4. PreparedStatement ps = null;
  5. boolean result = false;
  6. try {
  7. CommonUtility commUtil = new CommonUtility();
  8. List&lt;String&gt; hmIds = new ArrayList&lt;String&gt;();
  9. Map&lt;String, String&gt; tmStockMap = new TreeMap&lt;String, String&gt;();
  10. Set&lt;String&gt; setRecentCertificate = new HashSet&lt;String&gt;();
  11. String managerAccountId = sessionInfo.getMembershipAccount();
  12. String stockQuery = &quot; select memberId , RootCertficateId from stockposition sp where sp.stocktype = &#39;TR&#39; and sp.memberId &quot;
  13. + &quot; IN ( select hm2.accountId from &quot;
  14. DATALINK
  15. + &quot;.holdingmembers hm2 &quot;
  16. + &quot; where hm2.holdingId = ( select holdingId from &quot;
  17. DATALINK
  18. + &quot;.holdingmembers hm1 where hm1.accountId = ? )) &quot;
  19. + &quot; order by sp.createdDate desc &quot;;
  20. conn = getChildDBConnection();
  21. if (null != conn) {
  22. ps = conn.prepareStatement(stockQuery);
  23. ps.setString(1, managerAccountId);
  24. rs = ps.executeQuery();
  25. if (null != rs) {
  26. while (rs.next()) {
  27. String memberId = rs.getString(&quot;memberId&quot;);
  28. String rootCertficateId = rs
  29. .getString(&quot;RootCertficateId&quot;);
  30. if (tmStockMap.containsKey(rootCertficateId)) {
  31. continue;
  32. }
  33. hmIds.add(memberId);
  34. tmStockMap.put(rootCertficateId, memberId);
  35. }
  36. }
  37. rs.close();
  38. ps.close();
  39. if (null != hmIds &amp;&amp; !hmIds.isEmpty()) {
  40. String inIds = commUtil.getInStateParam(hmIds);
  41. String mostRecentLicense = &quot;Select RootCertificateId , memberaccountid from &quot;
  42. + OctopusSchema.octopusSchema
  43. + &quot;.certificate c where c.memberaccountid IN (&quot;
  44. + inIds
  45. + &quot;) and c.isrootcertificate=0 and c.certificationstatusid &gt; 1 order by c.modifieddate desc&quot;;
  46. ps = conn.prepareStatement(mostRecentLicense);
  47. rs = ps.executeQuery();
  48. if (null != rs) {
  49. while (rs.next()) {
  50. String rootCertficateId = rs
  51. .getString(&quot;RootCertificateId&quot;);
  52. String memberaccountid = rs
  53. .getString(&quot;memberaccountid&quot;);
  54. if (setRecentCertificate.contains(memberaccountid)) {
  55. continue;
  56. }
  57. setRecentCertificate.add(memberaccountid);
  58. if (tmStockMap.containsKey(rootCertficateId)) {
  59. result = true;
  60. break;
  61. }
  62. }
  63. }
  64. rs.close();
  65. ps.close();
  66. } else {
  67. result = false;
  68. }
  69. }
  70. } catch (Exception e) {
  71. LOGGER.error(e);
  72. } finally {
  73. closeDBReferences(conn, ps, null, rs);
  74. }
  75. return result;
  76. }

QUERY:

  1. select RootCertficateId,memberId from stockposition sp where sp.stocktype = &#39;TR&#39; and sp.memberId
  2. IN ( select hm2.accountId from
  3. DATALINK.holdingmembers hm2
  4. where hm2.holdingId = ( select holdingId from
  5. DATALINK.holdingmembers hm1 where hm1.accountId = &#39;4937&#39; ))
  6. order by sp.createdDate DESC;

寻求以下代码的优化。

寻求以下代码的优化。

答案1

得分: 0

一个快速的方法是将你的IN替换为EXISTS。如果你的内部查询返回了很多行,这会更加高效。这取决于你的子查询是否返回了许多结果。

链接:https://stackoverflow.com/questions/2065329/sql-server-in-vs-exists-performance

英文:

One quick approach would be a substition of your IN by EXISTS. If your inner queryes return a lot of rows, it would be a lot more efficient. It depends if your subquery returns a lot of results.

https://stackoverflow.com/questions/2065329/sql-server-in-vs-exists-performance

huangapple
  • 本文由 发表于 2020年4月6日 15:20:48
  • 转载请务必保留本文链接:https://java.coder-hub.com/61054766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定