talk is cheap, show me the code.
相关环境
K | V |
---|---|
OS | Windows 10 |
Spring Boot | 2.1.13.RELEASE |
mybatis-plus | 3.2.0 |
mysql-connector-java | 5.1.46 |
数据量 | 1128546 |
JVM参数 | -Xmx128m -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps |
xml
<select id="selectAllByStream" resultType="java.util.HashMap"
resultSetType="FORWARD_ONLY" fetchSize="-2147483648">
SELECT * FROM user
</select>
mapper
public interface UserMapper extends BaseMapper<User> {
void selectAllByStream(ResultHandler<HashMap> handler);
}
流式查询测试
@Component
public class TestRunner implements CommandLineRunner {
@Autowired
private UserMapper userMapper ;
@Override
public void run(String... args) throws Exception {
long s = System.currentTimeMillis();
userMapper.selectAllByStream(resultContext -> {
System.out.print(".");
});
System.out.println();
System.out.format("cost[%s]ms\n", System.currentTimeMillis() - s);
System.out.println(userMapper.selectCount(null));
System.out.println("done.");
}
}
控制台输出
................................................................................
cost[22575]ms
1128546
done.
指标检测
应用服务监控
堆内存使用稳定的保持在60M
以内。
简直就是稳得一批!
mysqld服务监控
可见,mysqld
服务的磁盘 IO 比较明显,所以 MySQL 流式查询是利用游标进行磁盘的读写,不会的对数据库服务器的内存造成压力。