• 我是怎么在每秒内向一百张表分别插入一条数据的


    我是怎么在每秒内向一百张表分别插入一条数据的

    一、思想

    ①减少数据库连接,一百张表分别插入一条数据,一秒内执行一百次连接插入肯定是不合适的,所以必须要批量
    ②线程操作,将执行插入的方法放入线程内,避免主线程的等待

    二、配置

    现在讲一下mysql+mybatis的批量操作(其他数据库也都支持,实现配置各不相同,可自行查阅)

    1、开启mysql的批量操作。yml配置数据库中添加allowMultiQueries=true

    url: jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true

    2、mapper.java

    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" "http://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
    喜欢就关注我吧,我会努力更新的!

    在这里插入图片描述

  • 相关阅读:
    51 nod 1181 质数中的质数(质数筛法)
    Just oj 2018 C语言程序设计竞赛(高级组)F:Star(结构体排序+最小生成树)
    欧拉函数+费马小定理拓展
    ZOJ 3785 What day is that day?(数论:费马小定理)
    Just oj 2018 C语言程序设计竞赛(高级组)H: CBT?
    树链剖分(入门学习)
    bitset用法
    链式前向星
    Nearest Common Ancestors(LCA板子)
    LCA(最近公共祖先)
  • 原文地址:https://www.cnblogs.com/gaogushenling/p/14125467.html
Copyright © 2020-2023  润新知