批量删除区别
delete() 和 deleteInBatch()
deleteAll() 和 deleteAllInBatch()
普通删除方法会根据唯一标识字段,生成多条 SQL 语句,而有 InBatch 的方法只会生成一条 SQL 语句
写入大量数据
@Transactional
public void save100WEntities() {
for (int i = 0; i < 1000; i++) {
respsitory.save(1000Entities);
entityManager.flush();
entityManager.clear();
}
}
读取大量数据
需要满足3个条件:
- ResultSetType 是
ResultSet.TYPE_FORWARD_ONLY
- ResultSetConcurrency 是
ResultSet.CONCUR_READ_ONLY
- FetchSize 是
Integer.MIN_VALUE
Statement statement = connection.createStatement(
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
statement.setFetchSize(Integer.MIN_VALUE);
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
@QueryHints(value = @QueryHint(name = FETCH_SIZE, value = "" + Integer.MIN_VALUE))
Stream<User> findByIdIsNotNull();
}
@Transactional
public void run() {
try (Stream<User> stream = userRepository.findByIdIsNotNull()) {
stream.forEach(user -> {
System.out.println(user);
entityManager.detach(user);
});
}
}