Mybatis批量查询比较

您所在的位置:网站首页 mybatis传入list查询 Mybatis批量查询比较

Mybatis批量查询比较

2023-03-26 02:09| 来源: 网络整理| 查看: 265

Mybatis拼装

平时我们一般都是这种写法,传一个list对象进去foreach拼接

List listByProductIds(@Param("productIds")List productIds); select id,namefrom TB_PROD_PRODUCTwhere STATUS = 1and id in#{id} 手写代码拼装

优化后的写法,在代码组装好,然后直接传一个字符串进去拼接,直接把in的内容当做一个参数传进去,${}

List listByCombinationProductIds(@Param("productIds") String productIds); select id,namefrom TB_PROD_PRODUCTwhere STATUS = 1and id in (${productIds}); 效率比较

分别测试了10、100、1000、1W、10W、100W的量级 测试代码

@Autowired private ProductDao productDao; @Test public void test() {long number = 10;for (int power = 1; power productIds.add(i);}StopWatch stopWatch = new StopWatch("测试" + total + "个拼接");stopWatch.start("mybatis拼装");productDao.listByProductIds(productIds);stopWatch.stop();stopWatch.start("手写代码拼装");productDao.listByCombinationProductIds(Joiner.on(",").join(productIds));stopWatch.stop();System.out.println(stopWatch.prettyPrint());System.out.println();} }

运行结果

StopWatch '测试10个拼接': running time (millis) = 427 ----------------------------------------- ms % Task name ----------------------------------------- 00417 098% mybatis拼装 00010 002% 手写代码拼装StopWatch '测试100个拼接': running time (millis) = 21 ----------------------------------------- ms % Task name ----------------------------------------- 00012 057% mybatis拼装 00009 043% 手写代码拼装StopWatch '测试1000个拼接': running time (millis) = 39 ----------------------------------------- ms % Task name ----------------------------------------- 00026 067% mybatis拼装 00013 033% 手写代码拼装StopWatch '测试10000个拼接': running time (millis) = 218 ----------------------------------------- ms % Task name ----------------------------------------- 00160 073% mybatis拼装 00058 027% 手写代码拼装StopWatch '测试100000个拼接': running time (millis) = 1314 ----------------------------------------- ms % Task name ----------------------------------------- 00998 076% mybatis拼装 00316 024% 手写代码拼装StopWatch '测试1000000个拼接': running time (millis) = 13370 ----------------------------------------- ms % Task name ----------------------------------------- 09863 074% mybatis拼装 03507 026% 手写代码拼装

通过对比不同量级,手写代码拼装的方式,效率高

总结 方式效率mybatis拼装低手写代码拼接高 备注说明

mybatis拼装效率低的原因,实际上是foreach导致

select id,namefrom TB_PROD_PRODUCTwhere STATUS = 1and id in#{id}

实际上是先解析成

select id,name from TB_PROD_PRODUCT where STATUS = 1 and id in (#{__frch_id_0}, #{__frch_id_1}, #{__frch_id_2})

然后通过PreparedStatment 的 setXXX来进行赋值,就是还要循环解析 #{__frch_item_0} 之类的占位符,foreach 的集合越大,解析越慢。 所以,我们直接使用手写代码拼接,效率就快了很多。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3