数据库表结构:
逐条插入sql语句:
<insert id="insert" parameterType="com.Structure">
INSERT INTO
STRUCTURE(
id,
structureNAME,
PARENTID,
structureType,
description,
deptId,
propertyCompanyId,
sort,
communityId)
VALUES(
#{id,jdbcType=VARCHAR},
#{structureName,jdbcType=VARCHAR},
#{parentId,jdbcType=VARCHAR},
#{structureType,jdbcType=NUMERIC},
#{description,jdbcType=VARCHAR},
#{deptId,jdbcType=VARCHAR},
#{propertyCompanyId,jdbcType=VARCHAR},
#{sort,jdbcType=VARCHAR},
#{communityId,jdbcType=VARCHAR})
</insert>
批量插入sql语句:
<insert id="insertBatch" parameterType="java.util.List">
BEGIN
<foreach collection="list" item="item" index="index"
separator=";">
INSERT INTO
STRUCTURE(
id,
structureNAME,
PARENTID,
structureType,
description,
deptId,
propertyCompanyId,
sort,
communityId)
VALUES
(
#{item.id,jdbcType=VARCHAR},
#{item.structureName,jdbcType=VARCHAR},
#{item.parentId,jdbcType=VARCHAR},
#{item.structureType,jdbcType=NUMERIC},
#{item.description,jdbcType=VARCHAR},
#{item.deptId,jdbcType=VARCHAR},
#{item.propertyCompanyId,jdbcType=VARCHAR},
#{item.sort,jdbcType=VARCHAR},
#{item.communityId,jdbcType=VARCHAR})
</foreach>
;END ;
</insert>
性能测试结果:
数量级 | 批量插入耗时(ms) | 逐条插入耗时(ms) |
10 | 50 | 68 |
100 | 197 | 257 |
1600 | 1671 | 33384 |
没想到到千级别的数量级就已经有如此巨大的耗时差别。
不知道是否有写法,以及数据库类型上的影响,暂时做个记录供自己参考。