MyBatis+Spring SQL效率测试报告
1. 数据库结构

2. insert 测试
insert 的测试包括
1) 批量拼接values()插入
2) 有事务for循环插入
3) 无事务for循环插入
测试 SQL:
<!-- 普通 insert -->
<insert id="insert"
parameterType="com.qunar.mybatistask.bean.Post"
keyProperty="id">
<![CDATA[ insert into post (
title,
content,
author,
status,
created
) values (
#{title},
#{content},
#{author},
#{status},
#{created}
) ]]>
</insert> <!-- 拼接values insert -->
<insert id="batchInsert"
parameterType="java.util.List">
<![CDATA[
insert into post (
title,
content,
author,
status,
created
) values
]]>
<foreach collection="list" item="post" separator=",">
(
#{post.title},
#{post.content},
#{post.author},
#{post.status},
#{post.created}
)
</foreach>
</insert>
测试代码:
service
/**
* 批量拼接VALUES() insert
*
* @param postList
* @return
*/
@Override
@Transactional(propagation = Propagation.REQUIRED)
public int batchInsert(List<Post> postList) {
int singleNum = 1000;
int affectedRows = 0;
for (int i = 0; i < Math.ceil((double)(postList.size() / singleNum)); i++) {
affectedRows += sqlSession.insert("post.batchInsert", postList.subList(i * singleNum, (i + 1) * singleNum));
}
return affectedRows;
} /**
* 事务内循环insert
*
* @param postList
* @return
*/
@Override
@Transactional(propagation = Propagation.REQUIRED)
public int createList(List<Post> postList) {
int affectedRows = 0;
for (Post post : postList) {
affectedRows += sqlSession.insert("post.insert", post);
}
return affectedRows;
}
test case:
/**
* 批量插入效率测试
*
* Method: batchInsert(List<Post> postList)
*
*/
@Test
public void testBatchInsert() throws Exception {
List<Post> postList = Lists.newArrayList();
for (int i = 0; i < 10000; i++) {
Post post = new Post();
post.setAuthor("test");
post.setContent("test");
post.setCreated(new Date());
post.setTitle("test");
post.setStatus(PostStatus.NORMAL);
postList.add(post);
} // 批量拼接SQL插入
long start = System.nanoTime();
int affectedRows = postService.batchInsert(postList);
double duration = System.nanoTime() - start;
System.out.format("batch: %.2f\n", duration / 1.0e9);
System.out.println("affected rows: " + affectedRows); // 事务内循环插入
start = System.nanoTime();
affectedRows = postService.createList(postList);
duration = System.nanoTime() - start;
System.out.format("transaction: %.2f\n", duration / 1.0e9);
System.out.println("affected rows: " + affectedRows); // 无事务直接循环插入
start = System.nanoTime();
affectedRows = 0;
for (Post post : postList)
affectedRows += postService.create(post);
duration = System.nanoTime() - start;
System.out.format("simple: %.2f\n", duration / 1.0e9);
System.out.println("affected rows: " + affectedRows);
}
结果
batch: 1.44
affected rows: 10000
transaction: 2.87
affected rows: 10000
simple: 77.57
affected rows: 10000
总结:
排行
1) 使用拼接的手段,这种插入其实就是batch,只不过这是手动batch
2) 使用事务循环插入,相对于无事务快很多的原因大概是数据库连接和事务开启的次数
3) 无事务循环插入, 我想应该没人这么写
2. 单表循环查询与拼接in查询测试
SQL
<select id="selectById" parameterType="int" resultType="com.qunar.mybatistask.bean.Post">
<![CDATA[ select
id,
title,
content,
author,
status,
created
from
post
where
id = #{id} ]]>
</select> <!-- 拼接where in条件查询 -->
<select id="selectIn" parameterType="java.util.List" resultType="com.qunar.mybatistask.bean.Post">
<![CDATA[
select
id,
title,
content,
author,
status,
created
from
post
where
id in
]]>
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
Service
@Override
public Post selectById(int id) {
return sqlSession.selectOne("post.selectById", id);
} @Override
public List<Post> selectByIds(List<Integer> ids) {
List<Post> postList = Lists.newArrayList();
int singleNum = 1000;
int start;
int end;
for (int i = 0; i < Math.ceil(((double)ids.size() / (double)singleNum)); i++) {
start = i * singleNum;
end = (i + 1) * singleNum;
end = end > ids.size() ? ids.size() : end;
List<Post> result = sqlSession.selectList("post.selectIn", ids.subList(start, end));
postList.addAll(result);
}
return postList;
}
test case
/**
* 使用IN查询效率测试
*
* @throws Exception
*/
@Test
public void testInSelect() throws Exception {
List<Integer> ids = Lists.newArrayList();
for (int i = 1; i < 10000; i++) {
ids.add(i);
} // in 查询
long start = System.nanoTime();
List<Post> list = postService.selectByIds(ids);
double duration = System.nanoTime() - start;
System.out.format("in select: %.2f\n", duration / 1.0e9);
System.out.println("list size: " + list.size()); // 循环查询
list = Lists.newArrayList();
start = System.nanoTime();
for (int id : ids)
list.add(postService.selectById(id));
duration = System.nanoTime() - start;
System.out.format("simple select: %.2f\n", duration / 1.0e9);
System.out.println("list size: " + list.size());
}
结果
in select: 0.55
list size: 9999
simple select: 6.24
list size: 9999
总结:
我想应该没人会用for循环去做查询吧
3. 多表联结查询, join, form 2个table, in, exists 比较
SQL
<!-- 用于循环查询 -->
<select id="selectAll" resultType="com.qunar.mybatistask.bean.Comment">
<![CDATA[
select
cmt.id as id,
cmt.post_id as postId,
cmt.content as content
from
cmt
]]>
</select> <!-- join 查询 -->
<select id="selectJoin" resultType="com.qunar.mybatistask.bean.Comment">
<![CDATA[
select
cmt.id as id,
cmt.post_id as postId,
cmt.content as content
from
cmt
join
post
on
post.id = cmt.post_id
]]>
</select> <!-- from 2个table -->
<select id="selectTowTable" resultType="com.qunar.mybatistask.bean.Comment">
<![CDATA[
select
cmt.id as id,
cmt.post_id as postId,
cmt.content as content
from
cmt, post
where cmt.post_id = post.id
]]>
</select> <!-- in 联表查询 -->
<select id="selectIn" resultType="com.qunar.mybatistask.bean.Comment">
<![CDATA[
select
cmt.id as id,
cmt.post_id as postId,
cmt.content as content
from
cmt
where
cmt.post_id
in
(
select
post.id
from
post
)
]]>
</select> <!-- exists 联表查询 -->
<select id="selectExists" resultType="com.qunar.mybatistask.bean.Comment">
<![CDATA[
select
cmt.id as id,
cmt.post_id as postId,
cmt.content as content
from
cmt
where
exists
(
select
post.id
from
post
where
post.id = cmt.id
)
]]>
</select>
service
@Override
public List<Comment> selectTwoTable() {
return sqlSession.selectList("comment.selectTowTable");
} @Override
public List<Comment> selectJoin() {
return sqlSession.selectList("comment.selectJoin");
} @Override
public List<Comment> selectIn() {
return sqlSession.selectList("comment.selectIn");
} @Override
public List<Comment> selectExists() {
return sqlSession.selectList("comment.selectExists");
} @Override
public List<Comment> selectAll() {
return sqlSession.selectList("comment.selectAll");
}
test case
/**
* 测试JOIN查询效率
*
*/
@Test
public void testJoinSelect() {
// join 查询
long start = System.nanoTime();
List<Comment> list = commentService.selectJoin();
double duration = System.nanoTime() - start;
System.out.format("join select: %.2f\n", duration / 1.0e9);
System.out.println("list size: " + list.size()); // From 两个表查询
start = System.nanoTime();
list = commentService.selectTwoTable();
duration = System.nanoTime() - start;
System.out.format("2 table select: %.2f\n", duration / 1.0e9);
System.out.println("list size: " + list.size()); // in多表查询
start = System.nanoTime();
list = commentService.selectIn();
duration = System.nanoTime() - start;
System.out.format("in multi table select: %.2f\n", duration / 1.0e9);
System.out.println("list size: " + list.size()); // exists多表查询
start = System.nanoTime();
list = commentService.selectExists();
duration = System.nanoTime() - start;
System.out.format("exists multi table select: %.2f\n", duration / 1.0e9);
System.out.println("list size: " + list.size()); // 分次查询, 太慢了, 忽略这种方法的测试吧
// start = System.nanoTime();
// list = commentService.selectAll();
// for (Comment comment : list) {
// postService.selectById(comment.getPostId());
// }
// duration = System.nanoTime() - start;
// System.out.format("separate select: %.2f\n", duration / 1.0e9);
// System.out.println("list size: " + list.size());
}
结果
join select: 2.44
list size: 210000
2 table select: 2.26
list size: 210000
in multi table select: 2.03
list size: 210000
exists multi table select: 2.35
list size: 210000
总结:
21W条数据下效率都差不多,而且我们一般会使用limit去限制查询的条数,所以应该他们的效率差距应该很小,我通过观察explain发现实际上join和from 2个table的方式的查询的执行计划是一模一样的,而in和exists的执行计划也是一模一样的
这里的表结构相对简单,也基本能用上索引 post_id 和 post.id 这些primary, 具体更加复杂的情况也许会影响这几种查询方式的执行计划, 才会体现出他们直接的差距, 当然我也相信他们执行的效率很大程度上是决定于mysql的优化器的优化策略,而这个优化策略很难人为的去判断,所以也不好说
MyBatis+Spring SQL效率测试报告的更多相关文章
- Spring Boot 集成 MyBatis和 SQL Server实践
概 述 Spring Boot工程集成 MyBatis来实现 MySQL访问的示例我们见过很多,而最近用到了微软的 SQL Server数据库,于是本文则给出一个完整的 Spring Boot + M ...
- 【spring boot】【mybatis】spring boot中mybatis打印sql语句
spring boot中mybatis打印sql语句,怎么打印出来?[参考:https://www.cnblogs.com/sxdcgaq8080/p/9100178.html] 在applicati ...
- 【记录】spring/springboot 配置mybatis打印sql
======================springboot mybatis 打印sql========================================== 方式 一: ##### ...
- 集成框架 javaweb开发平台ssmy_m(生成代码) java struts2 mybatis spring maven jquery
网页地址 http://blog.csdn.net/lpy3654321/article/details/31841573 项目设想,在项目开发中,我们的开发者大多数时间都在反复开发 相同的keywo ...
- MyBatis学习(一)、MyBatis简介与配置MyBatis+Spring+MySql
一.MyBatis简介与配置MyBatis+Spring+MySql 1.1MyBatis简介 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的J ...
- MyBatis详解 与配置MyBatis+Spring+MySql
MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的JDBC代码.手工设置参数和结果集重获.MyBatis 只使用简单的XML 和注解来配置和映射基本 ...
- Maven+druid+MyBatis+Spring+Oracle+Dubbo开发环境搭建
1.开发工具使用: MyEclipse或Eclipse,数据库使用Oracle.需要用到的软件有Zookeeper(注册中心),Tomcat(Web容器)和Maven(包管理). 2.初始环境配置: ...
- mybatis 打印sql 语句
拦截器 package com.cares.asis.mybatis.interceptor; import java.text.DateFormat; import java.util.Date; ...
- 一、MyBatis简介与配置MyBatis+Spring+MySql
//备注:该博客引自:http://limingnihao.iteye.com/blog/106076 1.1MyBatis简介 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架 ...
随机推荐
- NHibernate 学习导航
http://www.cnblogs.com/lyj/archive/2008/10/30/1323099.html
- NET生成缩略图
1.添加一个html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <hea ...
- caffe 如何训练自己的数据图片
申明:此教程加工于caffe 如何训练自己的数据图片 一.准备数据 有条件的同学,可以去imagenet的官网http://www.image-net.org/download-images,下载im ...
- 实现linux和windows文件传输
其实这个题目有点大,这里介绍的只是linux和windows文件传输中的一种,但是这种方法却非常实用,那就是:ZModem协议具体是linux命令是:rz和sz但是其实它们是两个非常方便的工具. ...
- JAVA语言中的运算符和表达式
JAVA——运算符 按运算符要求的运算符个数可分为一元.二元.三元运算符: 一元运算符有一个操作数:如正数或者负数前面的“+”.“—”,和自增“++”.自减“- -”. 二元运算符有两个操作数:如除法 ...
- luoguP2303 [SDOI2012]Longge的问题 化式子
求\(\sum \limits_{i = 1}^n gcd(i, n)\) \(\sum \limits_{i = 1}^n gcd(i, n)\) \(=\sum \limits_{i = 1}^n ...
- CF696B Puzzles 期望
显然可以树形$dp$ 令$f[i]$表示$i$号节点的期望时间戳 不妨设$fa$有$k$个子节点,对于$i$的子节点$u$,它是第$j(1 \leqslant j \leqslant k)$个被访问的 ...
- HDU 3949 XOR 线性基
http://acm.hdu.edu.cn/showproblem.php?pid=3949 求异或第k小,结论是第k小就是 k二进制的第i位为1就把i位的线性基异或上去. 但是这道题和上一道线性基不 ...
- hihocoder 1509 异或排序
题面在这里! 考虑前后两个数 x,y,可以发现S只有在(x xor y)的最高有1位上的取值是要被确定的 (如果x==y那么没有限制),可以推一下什么情况下是1/0. 于是我们模拟一下这个操作,判一判 ...
- [CodeForces-585F]Digits of Number Pi
题目大意: 给你一个数字串s,一个序列范围l和r,(l和r的数字位数为d)求l到r中有多少个数,满足它的长度为d/2的子串,能够在s中被匹配. 思路: 首先将s中每一个长度为d/2的子串插入后缀自动机 ...