mysql 批量插入與單條插入 的效率比較
阿新 • • 發佈:2017-05-12
dtd rop too lac int 技術 public urn 方法
1、數據插入性能(單個插入和批量插入)
[java] view plain copy- public class Test {
- private Long id;
- private String test;
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getTest() {
- return test;
- }
- public void setTest(String test) {
- this.test = test;
- }
- }
mapper.xml文件
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.astrospace.test.mapper.TestMapper" >
- <resultMap id="test" type="com.astrospace.test.dmo.Test" >
- <id property="id" column="ID"/>
- <result property="test" column="TEST" />
- </resultMap>
- <insert id="add" parameterType="com.astrospace.test.dmo.Test">
- INSERT INTO TEST(ID,TEST) VALUES(#{id},#{test});
- </insert>
- <insert id="batchAdd" parameterType="java.util.List">
- INSERT INTO TEST(ID,TEST)
- VALUES
- <foreach collection="list" item="item" index="index" separator="," >
- (#{item.id},#{item.test})
- </foreach>
- </insert>
- </mapper>
調用add和batchAdd方法即可。
不同數據量測試5次,結果如下:
單獨插入50000條數據平均耗時:233748ms
批量插入50000條數據平均耗時:2590ms
對比:效率差50倍
單獨插入10000條數據平均耗時:22036ms
批量插入10000條數據平均耗時:3330ms
對比:效率差6倍
單獨插入1000條數據平均耗時:3122ms
批量插入1000條數據平均耗時:374ms
對比:效率差8倍
數據越多,效率愈發明顯。
mysql 批量插入與單條插入 的效率比較