登录
首页 >  数据库 >  MySQL

怎么让MySQL迅速生成百万级别的测试数据?

来源:SegmentFault

时间:2023-02-16 17:30:32 151浏览 收藏

知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个数据库开发实战,手把手教大家学习《怎么让MySQL迅速生成百万级别的测试数据?》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!

背景

最近在跟着学习一套开源的电商项目,建了一个项目库所需的数据库。表是有了,但是数据没有。一个电商项目怎么能没有多一点的数据呢,于是决定自己一个表造个几十万数据(总共71个表)。对于假数据只有几点简单的要求:

  1. 创建时间要比现在时间小,并且在一定的范围随机

  2. 更新时间要比现在时间大,并且在一定的范围随机

  3. 数据不能是一成不变的,要有随机变化的特点

  4. 例如status这种char(1),数据只有0或者1的字段,不能出现非法数据

  5. ......

开始

创建临时表tmp_table

  1. 在数据库服务器上安装python环境

  2. 在数据库服务器上执行命令,生成内容为1~100000的文件(

  3. 执行sql语句

    SELECT
    
     TABLE_NAME AS '表名',
    
     COLUMN_NAME AS '列名',
    
     COLUMN_DEFAULT AS '默认值',
    
     COLUMN_TYPE AS 列类型
    
    FROM
    
     information_schema.`COLUMNS`
    
    WHERE
    
     TABLE_SCHEMA = 'mall'
    
    ORDER BY
    
     TABLE_NAME,
    
     ORDINAL_POSITION
    INTO OUTFILE '/var/lib/mysql-files/mallFour.text';

    其中TABLE_SCHEMA=后修改为自己的库名,

    INTO OUTFILE '/var/lib/mysql-files/mallFour.text'
    中的
    mallFour.text
    修改为自己的文件名称(但是路径不要修改)。

    下面我生成的数据文件:

    ums_role    id    N    bigint(20)
    ums_role    name    N    varchar(100)
    ums_role    description    N    varchar(500)
    ums_role    admin_count    N    int(11)
    ums_role    status    1    int(1)
    ums_role    sort    0    int(11)
    ums_role    create_user    N    varchar(50)
    ums_role    create_time    N    datetime
    ums_role    update_user    N    varchar(50)
    ums_role    update_time    N    datetime
    ums_role    is_delete    0    char(1)
    ums_role_permission_relation    id    N    bigint(20)
    ums_role_permission_relation    role_id    N    bigint(20)
    ums_role_permission_relation    permission_id    N    bigint(20)
    ums_role_permission_relation    create_user    N    varchar(50)
    ums_role_permission_relation    create_time    N    datetime
    ums_role_permission_relation    update_user    N    varchar(50)
    ums_role_permission_relation    update_time    N    datetime
    ums_role_permission_relation    is_delete    0    char(1)
    ......
    ......
    ......
  4. 将生成的表、字段信息文件mallFour.text,下载到本地

  5. 删除mallFour.text文件中的

    tmp_table
    这一行,并在文件最后加上一行
    文件结尾标识

编写Java代码,生成sql语句

package com.liboshuai.mall.tiny.utils;

import cn.hutool.core.io.FileUtil;
import com.liboshuai.mall.tiny.common.enums.AutoMysqlEnum;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: liboshuai
 * @Date: 2022-09-17 15:51
 * @Description: 随机生成mysql数据工具类
 */
public class AutoMysqlUtil {

    private static final String TMP_TABLE = "tmp_table";
    private static final String FILE_END_FLAG = "文件结尾标识";

    public static void main(String[] args) {
        // 刚才下载到本地的mysql表、字段数据文件地址
        List readLines = FileUtil.readLines("C:\Users\李博帅\Desktop\mall03.txt", "UTF-8");
        // 拼接 "INSERT INTO ? SELECT content FROM tmp_table" + tableName 部分
        List tableNameList = readLines.stream()
                .map(readLine -> readLine.split("\s+")[0])
                .collect(Collectors.toList());
        Map> tableNameMap = tableNameList.stream().collect(Collectors.groupingBy(String::toString));
        Set tableNameSet = tableNameMap.keySet();
        List truncateTableMysqlList = tableNameSet.stream()
                .filter(tableName -> !Objects.equals(tableName, TMP_TABLE) && !tableName.startsWith(FILE_END_FLAG))
                .map(tableName -> new StringBuilder("truncate table ")
                        .append(tableName).append(";"))
                .collect(Collectors.toList());
        List columnNameList = readLines.stream()
                .map(readLine -> readLine.split("\s+")[1])
                .collect(Collectors.toList());
        List typeList = readLines.stream()
                .map(readLine -> readLine.split("\s+")[3])
                .collect(Collectors.toList());
        List mysqlList = new ArrayList();
        String tableName = tableNameList.get(0);
        int flag = 0;
        for (int i = 0; i  columnNameTempList = new ArrayList();
                    List typeTempList = new ArrayList();
                    for (int j = flag; j  mysql.substring(0, mysql.length() - 2))
                .map(mysql -> new StringBuilder(mysql).append(" FROM tmp_table;"))
                .collect(Collectors.toList());
        truncateTableMysqlList.addAll(mysqlList);
        // mysql文件生成的地址
        FileUtil.writeLines(truncateTableMysqlList, "C:\Users\李博帅\Desktop\mallMysql.sql", "UTF-8");
    }
}

package com.liboshuai.mall.tiny.common.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * @Author: liboshuai
 * @Date: 2022-09-17 18:51
 * @Description: 自动生成mysql
 */
@Getter
@AllArgsConstructor
@SuppressWarnings("AlibabaEnumConstantsMustHaveComment")
public enum AutoMysqlEnum {
//    [date, int(2), int(1), varchar(128), varchar(8), char(1), int(11), varchar(16), varchar(32), bigint(11), varchar(50), datetime, bigint(20), varchar(64), varbinary(500), time, decimal(10,2), text, varchar(1000), varchar(100), varchar(255), varchar(200), int(3), varchar(500)]

    // 按照类型
    BIGINT("bigint", "FLOOR( Rand() * 100000), "),
    INT1("int(1)", "FLOOR(0 +  Rand() * 2), "),
    INT2("int(2)", "FLOOR( Rand() * 100), "),
    INT("int", "FLOOR( Rand() * 10000), "),
    DECIMAL("decimal", "FLOOR( Rand() * 100000), "),
    DATE("date", "ADDDATE(ADDDATE(ADDDATE('2018-01-01', INTERVAL FLOOR(RAND() * 8) YEAR),INTERVAL FLOOR(RAND() * 12) MONTH),INTERVAL FLOOR(RAND() * 31) DAY), "),
    DATETIME("datetime", "ADDDATE(ADDDATE(ADDDATE(ADDDATE(ADDDATE(ADDDATE('2018-01-01 00:00:00', INTERVAL FLOOR(RAND() * 8) YEAR),INTERVAL FLOOR(RAND() * 12) MONTH),INTERVAL FLOOR(RAND() * 31) DAY), INTERVAL FLOOR(RAND() * 24) HOUR),INTERVAL FLOOR(RAND() * 60) MINUTE),INTERVAL FLOOR(RAND() * 60) SECOND), "),
    TIME("time", "'00:00:00', "),
    VARCHAR("varchar", "NULL, "),
    CHAR("char", "'0', "),
    VARBINARY("varbinary", "NULL, "),
    TEXT("text", "NULL, "),

    // 按照列名
    ID("id", "id, "),
    CREATE_TIME("create_time", "ADDDATE(ADDDATE(ADDDATE(ADDDATE(ADDDATE(ADDDATE('2022-09-17 22:00:00', INTERVAL FLOOR(1 -(RAND() * 4)) YEAR),INTERVAL FLOOR(1 - (RAND() * 12)) MONTH),INTERVAL FLOOR(1 - (RAND() * 31)) DAY),INTERVAL FLOOR(1 - (RAND() * 24)) HOUR),INTERVAL FLOOR(1 - RAND() * 60) MINUTE),INTERVAL FLOOR(1 - (RAND() * 60)) SECOND), "),
    CREATE_USER("create_user", "admin"),
    UPDATE_TIME("update_time", "ADDDATE(ADDDATE(ADDDATE(ADDDATE(ADDDATE(ADDDATE('2022-09-17 22:00:00', INTERVAL FLOOR(1 + (RAND() * 4)) YEAR),INTERVAL FLOOR(1 + (RAND() * 12)) MONTH),INTERVAL FLOOR(1 + (RAND() * 31)) DAY),INTERVAL FLOOR(1 + (RAND() * 24)) HOUR),INTERVAL FLOOR(1 + RAND() * 60) MINUTE),INTERVAL FLOOR(1 + (RAND() * 60)) SECOND), "),
    UPDATE_USER("update_user", "lbs");


    private String key;
    private String value;

}

执行刚才生成的mysql语句文件
mallMysql.sql
,最终生成数据

......
......
......
truncate table pms_brand;
truncate table pms_comment;
truncate table ums_member;
truncate table ums_admin_login_log;
truncate table oms_cart_item;
truncate table sms_home_recommend_subject;
truncate table sms_home_new_product;
truncate table sms_flash_promotion_session;
truncate table cms_subject_product_relation;
truncate table pms_product_attribute_value;
truncate table oms_order_operate_history;
truncate table ums_member_login_log;
INSERT INTO cms_help SELECT id, FLOOR( Rand() * 100000), CONCAT( 'icon', id ), CONCAT( 'title', id ), FLOOR(0 +  Rand() * 2), FLOOR(0 +  Rand() * 2), CONCAT( 'content', id ), CONCAT( 'admin', id ), ADDDATE(ADDDATE(ADDDATE(ADDDATE(ADDDATE(ADDDATE('2022-09-17 22:00:00', INTERVAL FLOOR(1 -(RAND() * 4)) YEAR),INTERVAL FLOOR(1 - (RAND() * 12)) MONTH),INTERVAL FLOOR(1 - (RAND() * 31)) DAY),INTERVAL FLOOR(1 - (RAND() * 24)) HOUR),INTERVAL FLOOR(1 - RAND() * 60) MINUTE),INTERVAL FLOOR(1 - (RAND() * 60)) SECOND), CONCAT( 'lbs', id ), ADDDATE(ADDDATE(ADDDATE(ADDDATE(ADDDATE(ADDDATE('2022-09-17 22:00:00', INTERVAL FLOOR(1 + (RAND() * 4)) YEAR),INTERVAL FLOOR(1 + (RAND() * 12)) MONTH),INTERVAL FLOOR(1 + (RAND() * 31)) DAY),INTERVAL FLOOR(1 + (RAND() * 24)) HOUR),INTERVAL FLOOR(1 + RAND() * 60) MINUTE),INTERVAL FLOOR(1 + (RAND() * 60)) SECOND), '0' FROM tmp_table;
INSERT INTO cms_help_category SELECT id, CONCAT( 'name', id ), CONCAT( 'icon', id ), FLOOR( Rand() * 10000), FLOOR( Rand() * 100), FLOOR( Rand() * 10000), CONCAT( 'admin', id ), ADDDATE(ADDDATE(ADDDATE(ADDDATE(ADDDATE(ADDDATE('2022-09-17 22:00:00', INTERVAL FLOOR(1 -(RAND() * 4)) YEAR),INTERVAL FLOOR(1 - (RAND() * 12)) MONTH),INTERVAL FLOOR(1 - (RAND() * 31)) DAY),INTERVAL FLOOR(1 - (RAND() * 24)) HOUR),INTERVAL FLOOR(1 - RAND() * 60) MINUTE),INTERVAL FLOOR(1 - (RAND() * 60)) SECOND), CONCAT( 'lbs', id ), ADDDATE(ADDDATE(ADDDATE(ADDDATE(ADDDATE(ADDDATE('2022-09-17 22:00:00', INTERVAL FLOOR(1 + (RAND() * 4)) YEAR),INTERVAL FLOOR(1 + (RAND() * 12)) MONTH),INTERVAL FLOOR(1 + (RAND() * 31)) DAY),INTERVAL FLOOR(1 + (RAND() * 24)) HOUR),INTERVAL FLOOR(1 + RAND() * 60) MINUTE),INTERVAL FLOOR(1 + (RAND() * 60)) SECOND), '0' FROM tmp_table;
......
......
......

理论要掌握,实操不能落!以上关于《怎么让MySQL迅速生成百万级别的测试数据?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表