java - 使用 JPA,方法需要 60 秒

标签 java performance jpa

我正在使用 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 提到的,它可能应该是 for 循环内的 ~900 * N 数据库迭代。

我想说要尽可能避免数据库迭代,特别是在这样的循环内。

您可以尝试详细说明当前的 StudentExamResponse sql 以包含更多子句 - 主要是您在 for 中使用的子句,这甚至可能会减少您的项目数量迭代。

关于java - 使用 JPA,方法需要 60 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59247773/

相关文章:

java - 无法实例化类错误消息 - 在 testNG 中

c++ - 中值滤波器 OpenMP 优化

mysql - 带有 Group by 的 MAX() 函数非常慢

java - Mac OS X 上 Java 7 中的慢速 Java2D 双线性插值

java - 使用 JPA 一次加载和排序两个 OneToMany 关系

java - 如何抑制 Eclipse 警告 : Referenced identifier 'VIEWNAME:secondaryid' in attribute 'id' cannot be found

java - <jsp :attribute> 内的 jsp 打印变量

java - 无法在 JPQL 查询中使用 left join 和 join fetch 来获取数据,并以实体图作为提示

hibernate - JPA/hibernate 排序集合 @OrderBy 与 @Sort

java - 获取 byte[] 数组的 float[] View