# batchtest
**Repository Path**: zhangmrit/batchtest
## Basic Information
- **Project Name**: batchtest
- **Description**: mybatis批量新增和更新的几种方案的测试
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 1
- **Created**: 2019-06-28
- **Last Updated**: 2022-05-26
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
### 关于批量新增和更新的一些方案的测试
测试环境 
mysql 5.7
系统 windows 内存16g
bathc是mybatis自带的一种方式,操作简单,整合spring后并不常用,需配置`rewriteBatchedStatements=true`
循环values是在xml中`foreach`标签来拼接`values`,比较方便,通用mapper也集成了该方案(*主键必须自增*)
jdbc是老牌工具了,功能齐全
循环update是在xml种`foreach`标签来拼接sql,用`;`隔开,需配置`allowMultiQueries=true`
case when then 其中when...then...是sql中的"switch" 语法
| 数量 | mybatis BATCH | 循环values新增 | jdbc实现批量新增 | mybatis BATCH更新 | 循环update(;隔开)更新 | case when 更新 | jdbc实现批量更新 |
| :--- | ------------- | -------------- | ---------------- | ----------------- | --------------------- | -------------- | ---------------- |
| 50   | 56ms          | 58ms           | 49ms             | 1947ms            | 1874ms                | 432ms          | 393ms            |
| 500  | 359ms         | 90ms           | 142ms            | 14292ms           | 14963ms               | 447ms          | 302ms            |
| 5w   | 28256ms       | 1059ms         | 6319ms           | -                 | -                     | 字符超长       | 9558ms           |
| 10w  | -             | 3137ms         | 12812ms          | -                 | -                     | 字符超长       | 17570ms          |
批量新增推荐
```
	
		insert into test_user(user_name,remark)
		values
		
			(#{item.userName},#{item.remark})
		
	
```
批量更新推荐`case when then`,但要控制长度,分组处理,社会主义不能开倒车,jdbc就不推荐了,不过你大爷始终是你大爷
```
	
		update test_user
		
      		
        		
          			when user_name = (#{item.userName})
          			then #{item.remark}
        		
      		
    	
    	where user_name in
    	
      		#{item.userName}
    	
	 
```