java - 使用 JPA,方法需要 60 秒
问题描述
我正在使用 JPA,我的 Web 应用程序需要 60 秒来执行此方法,我想更快地执行它如何实现?
public boolean evaluateStudentTestPaper (long testPostID, long studentID, long howManyTimeWroteExam) {
Gson uday = new Gson();
Logger custLogger = Logger.getLogger("StudentDao.java");
// custLogger.info("evaluateTestPaper test paper for testPostID: " +
// testPostID);
long subjectID = 0;
// checking in table
EntityManagerFactory EMF = EntityManagerFactoryProvider.get();
EntityManager em = EMF.createEntityManager();
List<StudentExamResponse> studentExamResponses = null;
try {
studentExamResponses = em
.createQuery(
"SELECT o FROM StudentExamResponse o where o.studentId=:studentId And o.testPostID=:testPostID and o.howManyTimeWroteExam=:howManyTimeWroteExam")
.setParameter("studentId", studentID).setParameter("testPostID", testPostID)
.setParameter("howManyTimeWroteExam", howManyTimeWroteExam).getResultList();
System.out.println("studentExamResponses--------------------------------------------------"
+ uday.toJson(studentExamResponses) + "---------------------------------------");
} catch (Exception e) {
custLogger.info("exception at getting student details:" + e.toString());
studentExamResponses = null;
}
int studentExamResponseSize = studentExamResponses.size();
if (AppConstants.SHOWLOGS.equalsIgnoreCase("true")) {
custLogger.info("student questions list:" + studentExamResponseSize);
}
// Get all questions based on student id and test post id
List<ExamPaperRequest> examPaperRequestList = new ArrayList<ExamPaperRequest>();
List<Questions> questionsList = new ArrayList<Questions>();
// StudentExamResponse [] studentExamResponsesArgs =
// (StudentExamResponse[]) studentExamResponses.toArray();
// custLogger.info("Total questions to be evaluated: " +
// examPaperRequestList.size());
List<StudentTestResults> studentTestResultsList = new ArrayList<StudentTestResults>();
StudentTestResults studentTestResults = null;
StudentResults studentResults = null;
String subjectnames = "", subjectMarks = "";
int count = 0;
boolean lastIndex = false;
if (studentExamResponses != null && studentExamResponseSize > 0) {
// studentExamResponses.forEach(studentExamResponses->{
for (StudentExamResponse o : studentExamResponses.stream().parallel()) {
// 900 lines of coade inside which includes getting data from database Queries
}
}
解决方案
正如@Nikos Paraskevopoulos 所提到的,它可能应该是该循环内的~900 * N 数据库迭代for
。
我会说尽可能避免数据库迭代,特别是在这样的循环中。
您可以尝试详细说明当前的StudentExamResponse
sql 以包含更多子句 - 您for
主要在内部使用的子句,这甚至可能减少您迭代的项目数量。
推荐阅读
- android - 我们能否在仪器测试中测试静态广播接收器或广播接收器应仅在单元测试中测试
- kotlin - 如何使用计时器安排任务
- php - 如何将php文件包含到drupal 7自定义模块中
- excel - VB 运行时错误“1004”:无法获取 Addins 类的 Add 属性
- node.js - NestJS:用户管理处理不同类型的用户
- r - How to have percentage labels on a "bar chart of counts"
- oracle - SSIS Oracle 源 - DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER
- ios - 如何在 SwiftUI 中将手指的移动注册为绘图并检查边界?
- javascript - 使用具有嵌套对象的对象展平数组
- javascript - 如何检查在隐藏选项卡上检查了哪个值