当前使用版本(必须填写清楚,否则不予处理)
3.2.0
该问题是怎么引起的?(最新版上已修复的会直接close掉)
在msSql(sql server) mybatis-plus的插入速度比jdbc的批处理,快30倍,数据量5000条.可能是什么原因引起的
重现步骤
public class DoDBUtilInsert {
@Test
public void insert(){
Connection connection = JDBCUtil.getConnection();
try {
int num = 5000;
//sql
String sqlStr = "INSERT INTO ModelForInsert(id,name,info,age,depid,dep,cls,clsid) values(?,?,?,?,?,?,?,?)";
PreparedStatement statement = connection.prepareStatement(sqlStr);
List<ModelForInsert> list = ModelForInsert.listRandom(num);
//设值
for (ModelForInsert modelForInsert : list) {
statement.setInt(1,modelForInsert.getId());
statement.setString(2,modelForInsert.getName());
statement.setString(3,modelForInsert.getInfo());
statement.setInt(4,modelForInsert.getAge());
statement.setLong(5,modelForInsert.getDepid());
statement.setString(6,modelForInsert.getDep());
statement.setString(7,modelForInsert.getCls());
statement.setLong(8,modelForInsert.getClsid());
statement.addBatch();
}
//开始时间
Long begin = System.currentTimeMillis();
//执行
statement.executeBatch();
statement.close();
connection.commit();
connection.close();
Long end = System.currentTimeMillis();
System.out.println("使用jdbc插入数据"+num+"条,共用时"+(end-begin)+"毫秒");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
@RunWith(SpringRunner.class)
public class DoMybatisInsert {
@Resource
ModelForInsertService modelForInsertService;
@Test
public void insert(){
int num= 5000;
List<ModelForInsert> list = ModelForInsert.listRandom(num);
Long begin = System.currentTimeMillis();
//插入数据
modelForInsertService.saveBatch(list);
Long end = System.currentTimeMillis();
System.out.println("通过mybatis共插入"+num+"条数据,用时"+(end-begin)+"毫秒");
}
}
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.nutz.dao.entity.annotation.Table;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* @ClassName ModelForInsert
* @Description TODO
* @Author 朱矛宇
* @Date 2020/4/9 14:24
*/
@Data
@TableName("ModelForInsert")
@Table("ModelForInsert")
public class ModelForInsert {
private Integer id;
private String name ;
private String info;
private Integer age;
private Long depid;
private String dep;
private String cls;
private Long clsid;
public static ModelForInsert newRandom(){
Random random = new Random();
ModelForInsert modelForInsert = new ModelForInsert();
modelForInsert.setId(random.nextInt(9999999));
modelForInsert.setName("hello"+random.nextInt(100));
modelForInsert.setInfo("QWERTYUI欧帕斯的风格和接口连接恐惧开两张接口接口肯德基阿弗莱克加速度快骄傲就啥多久付款拉上");
modelForInsert.setAge(random.nextInt(100));
modelForInsert.setDepid(Long.valueOf(random.nextInt(10000)));
modelForInsert.setDep("hello");
modelForInsert.setCls("hello2222");
modelForInsert.setClsid(Long.valueOf(random.nextInt(10000)));
return modelForInsert;
}
public static List<ModelForInsert> listRandom(int num){
List<ModelForInsert> list = new ArrayList<>();
for(int i=0;i<num;i++){
list.add(ModelForInsert.newRandom());
}
return list;
}
}
@Mapper
public interface ModelForInsertDao extends BaseMapper<ModelForInsert> {
}
@Service
public class ModelForInsertService extends ServiceImpl<ModelForInsertDao,ModelForInsert> {
}
报错信息
通过mybatis共插入5000条数据,用时2300毫秒 使用jdbc插入数据5000条,共用时45700毫秒
Comment From: qmdx
你需要判断下一条 sql 方式可能是组装传输过程的耗时,更多可能因素应该是数据库对单行 sql 的执行耗时,主键外键约束判断耗时等, mp 之所以做成循环批量刷新提交也有这个考量因素。更多问题自己去探索,搞明白了可以来分享下