esolve
V2EX  ›  问与答

Java /mybatis/hibernate 将多条 mysql 语句放在一个 Dao 函数里会提升性能吗?

  •  
  •   esolve · Dec 21, 2017 · 1259 views
    This topic created in 3148 days ago, the information mentioned may be changed or developed.

    譬如 java 里的一个 service,里面有事务

    @Service
    class ShopService{
    
        @Transactional
        public void doTransaction(int type){      
             List<Customer> customerList = customerDao.getByType(type);
        	 couponDao.assign(customerList);	
        }
    }
    

    我的理解是:

    1 java 程序将 customerDao.getByType(type);对应的 mysql 语句通过数据库连接发给数据库

    2 数据库产生结果,将 customerList 发给 java 程序

    3 java 程序将 couponDao.assign(customerList);对应的 mysql 语句通过数据库连接发给数据库

    4 数据库产生结果,将结果发给 java 程序

    这样,如果中间结果 customerList 很大的话,网络传输性能损耗比较大

    假如如下做法:

    @Service
    class ShopService{
    
        @Transactional
        public void doTransaction(int type){      
             couponDao.assignCustomersByType(type);	
        }
    }
    

    couponDao.assignCustomersByType(type); 对应两条 mysql 语句,一次性发给数据库去执行,让中间结果在数据库里,这样性能会有显著提升吗?

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3662 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 04:28 · PVG 12:28 · LAX 21:28 · JFK 00:28
    ♥ Do have faith in what you're doing.