首页 > 解决方案 > JPA 属性表达式与使用“In”

问题描述

我有 3 节课:

学院
id, name, ....

学生
id, name, college, ....

标记
id, subject, student

我有collegeObject学生名单。我可以通过两种方式查询学生的分数:

JPA 属性表达式
List<Marks> findByStudentCollege(College college).

在查询中使用
List<Marks> findByStudentIn(Set<Student> studentList)

从性能的角度来看,我需要帮助来了解哪个查询更适合使用。

标签: javamysqlspringspring-data-jpaquery-performance

解决方案


您正在执行两个不同的查询:

findByStudentCollege(College college) :这个将用于在“college”中的每个元素“student”中查找大学的分数。你没有在分数和大学之间建立联系,所以你可能会做一个查询,比如“select * from tags where student = (Select id from Student where college = (select from College))

findByStudentIn(Set studentList) :这个将用于查找列表中的分数(0个或多个“学生”元素,因此您需要在之前列出学生,或者使用它来查找焦点学生的分数。

如果您没有完整的学生名单,第二个可能是最好的选择。如果您让一所大学的所有学生使用第一个,因为您正在避免第一个学生列表。

无论如何,即使您使用 5k+ 行,您也不会看到这些查询之间的区别


推荐阅读