Is it better to use multiple Dao combinations or a link query in the business?

< H2 > problem description < / H2 >

if there is an interface for querying user details
the user base table is associated with a lot of other information about the user, it is better to query all the queries now, whether to use multiple dao to combine in the service, or to directly link the query

. < H2 > sample code < / H2 >

user Table (user_base)
user Information Table 1 (user_info_1)
user Information Table 2 (user_info_2)
user Information Table 3 (user_info_3)
.
user Information Table n (user_info_n)

multiple dao queries returned

@Service
public class UserDetailServiceImpl implements UserService{
    
    @Autowired
    private UserBaseDao userBaseDao;
    @Autowired
    private UserInfoDao1 userInfoDao1;
    @Autowired
    private UserInfoDao2 userInfoDao2;
    
    @Override
    public UserInfoVo getDetail(String userId){
        UserInfoVo userInfoVo = new UserInfoVo ();
        
        // dao
        UserBase userBase = userBaseDao.getByUserId(userId);
        UserInfo1 userInfo1 = userInfoDao1.getByUserId(userId);
        UserInfo2 userInfo2 = userInfoDao2.getByUserId(userId);
        
        // VOset
        userInfoVo.set(dao1);
        ...
        ... 
        userInfoVo.set(daoN);
         
        return userInfoVo ;
    }
    
}

Link query

@Service
public class UserDetailServiceImpl implements UserService{
    
    @Autowired
    private UserBaseDao userBaseDao;
    
    @Override
    public UserInfoVo getDetail(String userId){
        
        // daoVO
        UserInfoVo userInfoVo = userBaseDao.getDetail(userId);    
        
        // sql
        String sql = 
        "select "" from user_base
        left join user_info_1 on user_base.user_id = user_info_1.user_id
        left join user_info_2 on user_base.user_id = user_info_2.user_id
        ...
        left join user_info_n on user_base.user_id = user_info_n.user_id
        where user_base.user_id = :userId";
        
        // set
    
        return userInfoVo ;
    }
    
}
< H2 > related questions < / H2 >

use the first combination of multiple Dao to return data. Although the various set in the Service layer is more complex, it feels less coupled. Each part of the dao is very independent, and it can also be reused in other places

.

using the second method, the processing of the Service layer is much easier except for very long sql statements that you have to write by hand. But the reusability is not enough

I hope to give relevant answers to the choice of these two ways based on the relevant programming ideas, or there are other design and implementation methods. Thank you very much

Apr.01,2021

can use it, specific analysis of the specific situation.

in the first way, the dao layer code can be reused, but because splitting into multiple sql, sends multiple sql for mysql to execute.
the second method is sent at once, which can be chosen when there are few join tables, or the first when there are many join tables.

A lot of data does not need to be obtained entirely from the database, such as querying some data from the database, and then fetching other data from the cache according to the queried data.

there is a military rule in the database of my company that clearly states:
forbids the use of associated queries with more than three tables


both must be available, and each has its own usage scenario. In this case, it is suitable to use sql link to query.
because sql link queries require only one database IO, while multiple dao, requires multiple database IO, it is time-consuming to know that database IO.
so, the sql link query, which gives the calculation to the database, has a relatively higher performance.


use the first kind of data, but not the second kind of data

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3562c-2bf81.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3562c-2bf81.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?