• mysql 批量插入与单条插入 的效率比较


    1、数据插入性能(单个插入和批量插入)
    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. public class Test {  
    2.   
    3.     private Long id;  
    4.   
    5.     private String test;  
    6.   
    7.     public Long getId() {  
    8.         return id;  
    9.     }  
    10.   
    11.     public void setId(Long id) {  
    12.         this.id = id;  
    13.     }  
    14.   
    15.     public String getTest() {  
    16.         return test;  
    17.     }  
    18.   
    19.     public void setTest(String test) {  
    20.         this.test = test;  
    21.     }  
    22. }  

    mapper.xml文件

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >  
    3. <mapper namespace="com.astrospace.test.mapper.TestMapper" >  
    4.     <resultMap id="test" type="com.astrospace.test.dmo.Test" >  
    5.         <id property="id" column="ID"/>  
    6.         <result property="test" column="TEST" />  
    7.     </resultMap>  
    8.   
    9.     <insert id="add" parameterType="com.astrospace.test.dmo.Test">  
    10.         INSERT INTO TEST(ID,TEST) VALUES(#{id},#{test});  
    11.     </insert>  
    12.   
    13.     <insert id="batchAdd" parameterType="java.util.List">  
    14.         INSERT INTO TEST(ID,TEST)  
    15.         VALUES  
    16.         <foreach collection="list" item="item" index="index" separator="," >  
    17.             (#{item.id},#{item.test})  
    18.         </foreach>  
    19.     </insert>  
    20.   
    21. </mapper>  

    调用add和batchAdd方法即可。

    不同数据量测试5次,结果如下:

    单独插入50000条数据平均耗时:233748ms
    批量插入50000条数据平均耗时:2590ms
    对比:效率差50倍
    单独插入10000条数据平均耗时:22036ms
    批量插入10000条数据平均耗时:3330ms
    对比:效率差6倍
    单独插入1000条数据平均耗时:3122ms
    批量插入1000条数据平均耗时:374ms
    对比:效率差8倍

    数据越多,效率愈发明显。

  • 相关阅读:
    P1067 多项式输出(模拟水题)
    A. The Fair Nut and Elevator(暴力)
    A. The Fair Nut and Elevator(暴力)
    Knight Tournament (set)
    jquery怎么添加多个类名
    jquery对类的操作,添加,删除,点击添加,再点击删除
    jquery操作css样式的方法
    jquery浅复制和深复制区别
    TS 三种函数的定义方式
    ES7及ES8新特性
  • 原文地址:https://www.cnblogs.com/toSeeMyDream/p/6846505.html
Copyright © 2020-2023  润新知