java 多表联查sql语句效率问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 多表联查sql语句效率问题相关的知识,希望对你有一定的参考价值。
select
---1
decode(Scope,'2','大型企业','3','中型企业','4','小型企业','5','微型企业',''),
--2
(SELECT nvl(COGNRESULT, nvl(COGNRESULT3,COGNRESULT4))
FROM EVALUATE_RECORD ER
WHERE ER.OBJECTTYPE = 'Customer'
AND ER.EVALUATEPERIOD = '01'
AND ER.EFFFLAG = '1'
and ER.INVALIDATEDATE > to_char(sysdate,'yyyy/mm/dd')
and ER.Objectno = ei.customerid and rownum = 1),
---3
(select decode(max(classifyresult),'A1','正常类1','A2','正常类2','B1',
'关注类1','B2','关注类2','B3','关注类3','C1','次级类1','C2','次级类2',
'D','可疑','E','损失','') from
cl_info cl where
classifyresult is not null and cl.customerid=ei.customerid)
from ent_info ei,customer_info ci where ei.customerid=ci.customerid and ci.customertype<>'0107'
这个sql语句查出3个字段,因为第三个字段查的表cl_info和ent_info是多对一关系靠customerid关联,导致整体效率变慢,去掉它就很快,不能用视图,索引也有不用想另外建,想提高效率怎么办
因为是给外围系统供数所以不能再另外加表,原表结构也不能变,存储过程不能用,甚至function都不能
什么?mybatis-plus的多表查询,你还在写sql语句?!CRUD2多表联查的三种方式
文章目录
书接上文,在上一篇,小名给大家演示了三种
MP
的单表查询方式,今天这篇文章的小名想给大家演示一下关于
MP
多表查询的相关操作。
1. 一篇朴实无华的MyBatis-Plus小白看完原地进阶的文章
2. 【CRUD】番外篇
3. 此专栏下其他文章
数据表
practice_user:
practice_teacher:
公用部分:
1. VO
@Data
public class TeacherAndUserVo extends PracticeTeacher{
@ApiModelProperty(value = "User名字")
private String name;
@ApiModelProperty(value = "User性别")
private String sex;
}
2. PracticeTeacherService
/**
* 通过sql多表查询
* @return
*/
List<TeacherAndUserVo> selectTeacherAndUserVoBySql();
/**
* 通过foreach多表查询
* @return
*/
List<TeacherAndUserVo> selectTeacherAndUserVoByForeach();
/**
* 通过stream多表查询
* @return
*/
List<TeacherAndUserVo> selectTeacherAndUserVoByStream();
一、SQL
1. PracticeTeacherMapper.xml
<select id="selectTeacherAndUserVoBySql" resultType="eamon.daily.practice.teacher.entity.vo.TeacherAndUserVo">
SELECT
`user`.`name`,
`user`.sex,
teacher.*
FROM
`practice_teacher` teacher
LEFT JOIN practice_user `user` ON teacher.user_id = `user`.id
where `user`.sex="女"
</select>
2. PracticeTeacherMapper.java
/**
* 通过sql多表查询
* @return
*/
List<TeacherAndUserVo> selectTeacherAndUserVoBySql();
3. PracticeTeacherServiceImpl.java
@Resource
PracticeTeacherMapper practiceTeacherMapper;
@Override
public List<TeacherAndUserVo> selectTeacherAndUserVoBySql() {
return practiceTeacherMapper.selectTeacherAndUserVoBySql();
}
4. 结果:
二、链式调用 lambda 式,lambdaQuery()+foreach
(不了解lambdaQuery()的小伙伴见:【CRUD1】简单单表查询)
1. PracticeTeacherServiceImpl.java
@Autowired
IPracticeTeacherService practiceTeacherService;
@Autowired
IPracticeUserService practiceUserService;
@Override
public List<TeacherAndUserVo> selectTeacherAndUserVoByForeach() {
//查询所有Teacher
List<PracticeTeacher> teachers = practiceTeacherService.lambdaQuery().list();
ArrayList<TeacherAndUserVo> teacherAndUserVos = new ArrayList<>();
for (PracticeTeacher teacher : teachers) {
//查询符合条件的User表中的女性
PracticeUser woman = practiceUserService.lambdaQuery()
.eq(PracticeUser::getSex, "女")
.eq(PracticeUser::getId, teacher.getUserId())
.one();
if(woman!=null){
//将当前teacher对象拷贝到teacherAndUserVo中
TeacherAndUserVo copy = CopyUtil.copy(teacher, TeacherAndUserVo.class);
//将User姓名及性别放到teacherAndUserVo中
copy.setName(woman.getName());
copy.setSex(woman.getSex());
teacherAndUserVos.add(copy);
}
}
return teacherAndUserVos;
}
2. 结果
三、stream
1. PracticeTeacherServiceImpl.java
@Override
public List<TeacherAndUserVo> selectTeacherAndUserVoByStream() {
//查询所有teacher对象
List<PracticeTeacher> teachers = practiceTeacherService.lambdaQuery().list();
//查询女生用户
List<PracticeUser> womans = practiceUserService.lambdaQuery().eq(PracticeUser::getSex, "女").list();
List<TeacherAndUserVo> teacherAndUserVos = womans.stream().map(
o -> {
//过滤符合要求的对象
TeacherAndUserVo vo = new TeacherAndUserVo();
for (PracticeTeacher teacher : teachers) {
if (teacher.getUserId().longValue() == o.getId()) {
vo = CopyUtil.copy(teacher, TeacherAndUserVo.class);
vo.setName(o.getName());
vo.setSex(o.getSex());
}
}
return vo;
})
.filter(x -> x.getName() != null)//过滤空对象
.collect(Collectors.toList());
return teacherAndUserVos;
}
这个例子不是很恰当,小名只是为了给大家演示一下lambda的用法。
2. 结果
如果觉得小名的文章帮助到了您,请关注小名的新专栏 MyBatis-Plus【CRUD】,支持一下小名😄,给小名的文章点赞👍、评论✍、收藏🤞谢谢大家啦~♥♥♥
以上是关于java 多表联查sql语句效率问题的主要内容,如果未能解决你的问题,请参考以下文章
MyBatis关于多表联查 关联关系之一--------一对多(单条sql语句查询)
sql语句多表联查,查询速度太慢,超过10s,由于是菜鸟,不知道怎样优化