Hibernate 复杂业务左连接的正确姿势
来源:SegmentFault
时间:2023-02-23 09:44:44 180浏览 收藏
哈喽!今天心血来潮给大家带来了《Hibernate 复杂业务左连接的正确姿势》,想必大家应该对数据库都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到MySQL、jpa、hibernate,若是你正在学习数据库,千万别错过这篇文章~希望能帮助到你!
引言
在进行试题综合查询时,在和往常一样使用
return subjectRepository.findAll((Specification) (root, query, builder) -> { Predicate predicate = root.get("parent").isNull(); logger.debug("构造是否使用查询条件"); Predicate usedPredicate = root.get("subjectSpread").isNull(); logger.debug("根据试卷id构造查询条件"); if (paperId != null) { Predicate belongPredicate = builder.equal(root.join("subjectSpread").join("part").join("paper").get("id").as(Long.class), paperId); usedPredicate = builder.or(usedPredicate, belongPredicate); } logger.debug("连接谓语"); predicate = builder.and(predicate, usedPredicate); return predicate; }, pageable);
查询条件构造的逻辑看起来没问题,但经过测试,该接口只能查出来当前试卷的试题,无法查询出
spring: jpa: show-sql: true
SELECT subject0_.id AS id1_15_, subject0_.analysis AS analysis2_15_, subject0_.course_id AS course_i7_15_, subject0_.create_time AS create_t3_15_, subject0_.create_user_id AS create_u8_15_, subject0_.difficult AS difficul4_15_, subject0_.mark AS mark5_15_, subject0_.model_id AS model_id9_15_, subject0_.p_id AS p_id10_15_, subject0_.stem AS stem6_15_, subject0_.subject_spread_id AS subject11_15_ FROM SUBJECT subject0_ INNER JOIN subject_spread subjectspr1_ ON subject0_.subject_spread_id = subjectspr1_.id INNER JOIN part part2_ ON subjectspr1_.part_id = part2_.id INNER JOIN paper paper3_ ON part2_.paper_id = paper3_.id INNER JOIN course course4_ ON subject0_.course_id = course4_.id INNER JOIN model model5_ ON subject0_.model_id = model5_.id WHERE (subject0_.p_id IS NULL) AND ( subject0_.subject_spread_id IS NULL OR paper3_.id = 2 ) AND course4_.id = 1 AND model5_.id = 2 ORDER BY subject0_.id DESC
问题就出现在这几行
SUBJECT subject0_ INNER JOIN subject_spread subjectspr1_ ON subject0_.subject_spread_id = subjectspr1_.id INNER JOIN part part2_ ON subjectspr1_.part_id = part2_.id INNER JOIN paper paper3_ ON part2_.paper_id = paper3_.id INNER JOIN course course4_ ON subject0_.course_id = course4_.id
数据库连接
左连接、右连接、内连接区别,请看下图:
原因分析
再看如下
subject INNER JOIN subject_spread ON subject.subject_spread_id = subject_spread.id
SELECT subject.* FROM subject INNER JOIN course ON subject.course_id = course.id INNER JOIN model ON subject.model_id = model.id WHERE subject.p_id IS NULL AND subject.subject_spread_id IS NULL AND course.id = ? AND model.id = ? UNION SELECT subject.* FROM subject INNER JOIN subject_spread ON subject.subject_spread_id = subject_spread.id INNER JOIN part ON subject_spread.part_id = part.id INNER JOIN paper ON part.paper_id = paper.id INNER JOIN course ON subject.course_id = course.id INNER JOIN model ON subject.model_id = model.id WHERE subject.p_id IS NULL AND paper.id = ? AND course.id = ? AND model.id = ? ORDER BY id DESC LIMIT ?
2. 左连接(推荐)
请教潘老师后,发现其实并不需要这么麻烦,之前的查询错误是因为对
Predicate belongPredicate = builder.equal(root.join("subjectSpread", JoinType.LEFT).join("part", JoinType.LEFT).join("paper", JoinType.LEFT).get("id").as(Long.class), paperId);
性能对比
两者都能实现功能,我们对比一下在大量数据的环境下各自查询的性能。
构造大量数据的方法
之前构造大量数据一直使用
CREATE PROCEDURE BIG_DATA() BEGIN DECLARE i INT DEFAULT 0; WHILE i
控制变量
因为
SELECT subject.* FROM subject LEFT JOIN subject_spread ON subject.subject_spread_id = subject_spread.id LEFT JOIN part ON subject_spread.part_id = part.id LEFT JOIN paper ON part.paper_id = paper.id INNER JOIN course ON subject.course_id = course.id INNER JOIN model ON subject.model_id = model.id WHERE subject.p_id IS NULL AND ( subject.subject_spread_id IS NULL OR paper.id = 2 ) AND course.id = 1 AND model.id = 2 ORDER BY subject.id DESC
执行时间
SELECT subject.* FROM subject INNER JOIN course ON subject.course_id = course.id INNER JOIN model ON subject.model_id = model.id WHERE subject.p_id IS NULL AND subject.subject_spread_id IS NULL AND course.id = 1 AND model.id = 2 UNION SELECT subject.* FROM subject INNER JOIN subject_spread ON subject.subject_spread_id = subject_spread.id INNER JOIN part ON subject_spread.part_id = part.id INNER JOIN paper ON part.paper_id = paper.id INNER JOIN course ON subject.course_id = course.id INNER JOIN model ON subject.model_id = model.id WHERE subject.p_id IS NULL AND paper.id = 2 AND course.id = 1 AND model.id = 2 ORDER BY id DESC
执行时间
16.450秒。
性能对比
二者对比,
UNION花费的时间大约是
LEFT JOIN的两倍,数据库进行了两次条件查询。
总结
除非业务必要,否则,
SQL语句尽量不要采用
UNION等联合多语句查询结果的方式,多次查询意味着更多的时间花费。
今天关于《Hibernate 复杂业务左连接的正确姿势》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于mysql的内容请关注golang学习网公众号!
声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
499 收藏
-
244 收藏
-
235 收藏
-
157 收藏
-
101 收藏
最新阅读
更多>
-
368 收藏
-
475 收藏
-
266 收藏
-
273 收藏
-
283 收藏
-
210 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习