SELECT * FROM productorder T WHERE T.productorder_code IN
当前位置:   article > 正文

mybatis for循环遍历查询数据 以及 IN 查询数据 的耗时对比,数据量万级别以上

mybatis for循环

xml层:

  1. <select id="getInfoByIds" parameterType="string" resultType="com.micro.pojo.Product">
  2. SELECT
  3. *
  4. FROM productorder T
  5. WHERE T.productorder_code IN
  6. <foreach collection="ids" index="index" open="(" close=")" item="item" separator=",">
  7. <if test="(index % 1000) == 999"> NULL) OR T.productorder_code IN (</if>#{item}
  8. </foreach>
  9. </select>
  10. <select id="getInfoById" parameterType="java.lang.String" resultType="com.micro.pojo.Product">
  11. SELECT
  12. *
  13. FROM productorder T
  14. WHERE T.productorder_code = #{productId}
  15. </select>

mapper层:

  1. List<Object> getInfoByIds(@Param("ids") List<String> ids);
  2. Object getInfoById(@Param("productId") String productId);

service层:

  1. @Override
  2. public List<Object> getInfoByIds(List<String> ids) {
  3. return productMapper.getInfoByIds(ids);
  4. }
  5. @Override
  6. public Object getInfoById(String productId) {
  7. return productMapper.getInfoById(productId);
  8. }

controller层:

  1. @RequestMapping(value = "/getDiff", method = RequestMethod.GET)
  2. public Map<String, Object> getDiff() {
  3. Map<String, Object> result = new HashMap<>();
  4. long startTime = System.currentTimeMillis();
  5. for (int i = 1; i < 50000; i++) {
  6. productService.getInfoById("20180501234" + i);
  7. }
  8. long endTime = System.currentTimeMillis();
  9. result.put("costTime1", (endTime - startTime) + "ms");
  10. return result;
  11. }
  12. @RequestMapping(value = "/getDiff2", method = RequestMethod.GET)
  13. public Map<String, Object> getDiff2() {
  14. Map<String, Object> result = new HashMap<>();
  15. long startTime = System.currentTimeMillis();
  16. List<String> ids = new ArrayList<>();
  17. for (int i = 1; i < 50000; i++) {
  18. ids.add("20180501234" + i);
  19. }
  20. productService.getInfoByIds(ids);
  21. long endTime = System.currentTimeMillis();
  22. result.put("costTime2", (endTime - startTime) + "ms");
  23. return result;
  24. }

耗时对比:

 结论:显然 IN 查询 比 for循环多次查询要快很多!

引申:高效的找出两个List中的不同元素,建议把大的list转成map,然后遍历小list,通过key区大的map中获取;如:

  1. public static List<String> getDiff4(List<String> listA,List<String> listB){
  2. long startTime = System.currentTimeMillis();
  3. List<String> diff = new ArrayList<String>();
  4. List<String> maxList = listA;
  5. List<String> minList = listB;
  6. if (listB.size() > listA.size()) {
  7. maxList = listB;
  8. minList = listA;
  9. }
  10. Map<String, Integer> map = new HashMap<String, Integer>(maxList.size());
  11. for (String string : maxList) {
  12. map.put(string, 1);
  13. }
  14. for (String string : minList) {
  15. // 说明相同
  16. if (map.get(string) != null) {
  17. map.put(string, 2);
  18. continue;
  19. }
  20. // 说明不相同
  21. diff.add(string);
  22. }
  23. long endTime = System.currentTimeMillis();
  24. System.out.println(String.format("getDiff4消耗时间:%s",(endTime - startTime)));
  25. return diff;
  26. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/749012
推荐阅读
相关标签