比如:有两张表,用户表和部门表,查询用户名称和部门名称
//联表查询:
select user.username,dept.name from sys_user user,sys_dept dept where user.id=#{userId} and user.dept_id=dept.id
//分解后的单表查询,先查用户表,在查部门表
select username,dept_id from sys_user where id=#{userId}
select name from sys_dept where id=#{deptId}
次数 | 联表查询 | 单表多次查询 |
---|---|---|
第一次 | 783ms | 799ms |
第二次 | 754ms | 766ms |
第三次 | 829ms | 773ms |
在数据量不多的情况下,多表连接查询和多次单表查询的效率是差不多的。如果数据量足够大,多次单表查询的效率更高。
此时查询的是一条记录,两者相差甚小。所以对于联表查询,建议将其分解为多个单表查询,而且单表查询还有不少优点。👇
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_44932487/article/details/119513926
内容来源于网络,如有侵权,请联系作者删除!