我是怎麼在每秒內向一百張表分別插入一條數據的

我是怎麼在每秒內向一百張表分別插入一條數據的

一、思想

①減少數據庫連接,一百張表分別插入一條數據,一秒內執行一百次連接插入肯定是不合適的,所以必須要批量
②線程操作,將執行插入的方法放入線程內,避免主線程的等待

二、配置

現在講一下mysql+mybatis的批量操作(其他數據庫也都支持,實現配置各不相同,可自行查閱)

1、開啟mysql的批量操作。yml配置數據庫中添加allowMultiQueries=true

1 url: jdbc:mysql://localhost:3306/數據庫名?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true

2、mapper.java

1 Integer insertEntityDataList(@Param("list") List<EntityData> list);

3、mapper.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "//mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 <mapper namespace="com...mapper.EntityDataMapper">
 4     <insert id="insertEntityDataList" parameterType="java.util.List" useGeneratedKeys="true" >
 5         <foreach collection="list" item="item" index="index" separator=";">
 6             CREATE TABLE If Not Exists `數據庫名`.`dev_${item.動態表名}`(
 7             `id` int(11) NOT NULL AUTO_INCREMENT,
 8             -- 省略
 9             `cread_time` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '創建時間',
10             `update_time` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '修改時間',
11             `del_state` int(1) NULL DEFAULT 0 COMMENT '刪除狀態  0 刪除  1 反之',
12             PRIMARY KEY (id)
13             ) ENGINE = InnoDB DEFAULT CHARSET=utf8;
14 
15             insert into dev_${item.動態表名}
16             <trim prefix="(" suffix=")" suffixOverrides="," >
17                 <if test="item.屬性!= null" >
18                     表字段,
19                 </if>
20                      cread_time,
21             </trim>
22             <trim prefix="values (" suffix=")" suffixOverrides="," >
23                 <if test="item.屬性 != null" >
24                     #{item.屬性,jdbcType=DOUBLE},
25                 </if>
26                   now(),
27             </trim>
28         </foreach>
29     </insert>
30 </mapper>

View Code

三、須得一提,提交的mysql數據量如若太大(一般默認大小就夠,不夠的話可以去設置一下),程序提示如下:

1 Packet for query is too large (5,200,001 > 4,194,304). You can change this value on the server by setting the 'max_allowed_packet' variable.
2 ; Packet for query is too large (5,200,001 > 4,194,304). You can change this value on the server by setting the 'max_allowed_packet' variable.;
3  nested exception is com.mysql.cj.jdbc.exceptions.PacketTooBigException: Packet for query is too large (5,200,001 > 4,194,304). 
4  You can change this value on the server by setting the 'max_allowed_packet' variable.

View Code

翻譯

1 查詢數據包太大(5,200,001 > 4,194,304)。您可以通過設置「max_allowed_packet」變量在服務器上更改該值。
2 ;查詢包太大(5,200,001 > 4,194,304)。您可以通過設置變量'max_allowed_packet'在服務器上更改該值。
3 嵌套異常是com.mysql.cj.jdbc.exceptions。PacketTooBigException:用於查詢的數據包太大(5,200,001 > 4,194,304)。
4 您可以通過設置「max_allowed_packet」變量在服務器上更改該值。

View Code

四、oracle批量

實際的業務系統裏面oracle數據庫也用的非常的多,當然,oracle數據庫不需要做特殊的配置,但是相應的sql要做變化。

1 <update id="updateAllAvailable">
2     <foreach collection="skuOptionList" item="item" index="index" open="begin" close="end;" separator=";">
3       update t_xxx
4       <set>
5         old_id = #{item.oldId}
6       </set>
7       where id = #{item.id}
8     </foreach>
9   </update>

View Code

 

喜歡就關注我吧,我會努力更新的!

在這裡插入圖片描述