MySQL数据生成工具mysql_random_data_load

news/2024/12/29 23:04:50/

在看MySQL文章的时候偶然发现生成数据的工具,此处直接将软件作者的文档贴了过来,说明了使用方式及下载地址

Random data generator for MySQL

Many times in my job I need to generate random data for a specific table in order to reproduce an issue.
After writing many random generators for every table, I decided to write a random data generator, able to get the table structure and generate random data for it.
Plase take into consideration that this is the first version and it doesn’t support all field types yet!

NOTICE
This is an early stage project.

Supported fields:

Field typeGenerated values
tinyint0 ~ 0xFF
smallint0 ~ 0XFFFF
mediumint0 ~ 0xFFFFFF
int - integer0 ~ 0xFFFFFFFF
bigint0 ~ 0xFFFFFFFFFFFFFFFF
float0 ~ 1e8
decimal(m,n)0 ~ 10^(m-n)
double0 ~ 1000
char(n)up to n random chars
varchar(n)up to n random chars
dateNOW() - 1 year ~ NOW()
datetimeNOW() - 1 year ~ NOW()
timestampNOW() - 1 year ~ NOW()
time00:00:00 ~ 23:59:59
yearCurrent year - 1 ~ current year
tinyblobup to 100 chars random paragraph
tinytextup to 100 chars random paragraph
blobup to 100 chars random paragraph
textup to 100 chars random paragraph
mediumblobup to 100 chars random paragraph
mediumtextup to 100 chars random paragraph
longblobup to 100 chars random paragraph
longtextup to 100 chars random paragraph
varbinaryup to 100 chars random paragraph
enumA random item from the valid items list
setA random item from the valid items list

How strings are generated

  • If field size < 10 the program generates a random “first name”
  • If the field size > 10 and < 30 the program generates a random “full name”
  • If the field size > 30 the program generates a “lorem ipsum” paragraph having up to 100 chars.

The program can detect if a field accepts NULLs and if it does, it will generate NULLs ramdomly (~ 10 % of the values).

Usage

mysql_random_data_load <database> <table> <number of rows> [options...]

Options

OptionDescription
–bulk-sizeNumber of rows per INSERT statement (Default: 1000)
–debugShow some debug information
–fk-samples-factorPercentage used to get random samples for foreign keys fields. Default 0.3
–hostHost name/ip
–max-fk-samplesMaximum number of samples for fields having foreign keys constarints. Default: 100
–max-retriesMaximum number of rows to retry in case of errors. See duplicated keys. Deafult: 100
–no-progressbarSkip showing the progress bar. Default: false
–passwordPassword
–portPort number
–PrintPrint queries to the standard output instead of inserting them into the db
–userUsername
–versionShow version and exit

Foreign keys support

If a field has Foreign Keys constraints, random-data-load will get up to --max-fk-samples random samples from the referenced tables in order to insert valid values for the field.
The number of samples to get follows this rules:
1. Get the aproximate number of rows in the referenced table using the rows field in:

EXPLAIN SELECT COUNT(*) FROM <referenced schema>.<referenced table>

1.1 If the number of rows is less than max-fk-samples, all rows are retrieved from the referenced table using this query:

SELECT <referenced field> FROM <referenced schema>.<referenced table>

1.2 If the number of rows is greater than max-fk-samples, samples are retrieved from the referenced table using this query:

SELECT <referenced field> FROM <referenced schema>.<referenced table> WHERE RAND() <= <fk-samples-factor> LIMIT <max-fk-samples>

Example

CREATE DATABASE IF NOT EXISTS test;CREATE TABLE `test`.`t3` (`id` int(11) NOT NULL AUTO_INCREMENT,`tcol01` tinyint(4) DEFAULT NULL,`tcol02` smallint(6) DEFAULT NULL,`tcol03` mediumint(9) DEFAULT NULL,`tcol04` int(11) DEFAULT NULL,`tcol05` bigint(20) DEFAULT NULL,`tcol06` float DEFAULT NULL,`tcol07` double DEFAULT NULL,`tcol08` decimal(10,2) DEFAULT NULL,`tcol09` date DEFAULT NULL,`tcol10` datetime DEFAULT NULL,`tcol11` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,`tcol12` time DEFAULT NULL,`tcol13` year(4) DEFAULT NULL,`tcol14` varchar(100) DEFAULT NULL,`tcol15` char(2) DEFAULT NULL,`tcol16` blob,`tcol17` text,`tcol18` mediumtext,`tcol19` mediumblob,`tcol20` longblob,`tcol21` longtext,`tcol22` mediumtext,`tcol23` varchar(3) DEFAULT NULL,`tcol24` varbinary(10) DEFAULT NULL,`tcol25` enum('a','b','c') DEFAULT NULL,`tcol26` set('red','green','blue') DEFAULT NULL,`tcol27` float(5,3) DEFAULT NULL,`tcol28` double(4,2) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB;

To generate 100K random rows, just run:

mysql_random_data_load test t3 100000 --user=root --password=root
mysql> select * from t3 limit 1\G
*************************** 1. row ***************************id: 1
tcol01: 10
tcol02: 173
tcol03: 1700
tcol04: 13498
tcol05: 33239373
tcol06: 44846.4
tcol07: 5300.23
tcol08: 11360967.75
tcol09: 2017-09-04
tcol10: 2016-11-02 23:11:25
tcol11: 2017-03-03 08:11:40
tcol12: 03:19:39
tcol13: 2017
tcol14: repellat maxime nostrum provident maiores ut quo voluptas.
tcol15: Th
tcol16: Walter
tcol17: quo repellat accusamus quidem odi
tcol18: esse laboriosam nobis libero aut dolores e
tcol19: Carlos Willia
tcol20: et nostrum iusto ipsa sunt recusa
tcol21: a accusantium laboriosam voluptas facilis.
tcol22: laudantium quo unde molestiae consequatur magnam.
tcol23: Pet
tcol24: Richard
tcol25: c
tcol26: green
tcol27: 47.430
tcol28: 6.12
1 row in set (0.00 sec)

效果良好
在这里插入图片描述

How to download the precompiled binaries

There are binaries available for each version for Linux and Darwin. You can find compiled binaries for each version in the releases tab:

https://github.com/Percona-Lab/mysql_random_data_load/releases


http://www.ppmy.cn/news/1147430.html

相关文章

MongoDB 笔记

1 insert 、create、save区别 insert: 主键不存在则正常插入&#xff1b;主键已存在&#xff0c;抛出DuplicateKeyException 异常 save: 主键不存在则正常插入&#xff1b;主键已存在则更新 insertMany&#xff1a;批量插入&#xff0c;等同于批量执行 insert create&#x…

uniapp+vue3+ts+uview-plus搭建项目步骤

创建项目 使用Vue3/Vite版&#xff0c;创建以 typescript 开发的工程 下载仓库 DCloud/uni-preset-vue - Gitee.com node版本&#xff1a;v16.18.0 npm版本&#xff1a; v8.19.2 依赖下载 解压之后&#xff0c;在vscode打开 通过终端运行 npm 命令下载依赖&#xff1a;npm ins…

linux-动态库和静态库制作和使用

【静态连接和动态连接】C/C编程中的两种有效链接策略_c 动态链接 静态链接_SecureCode的博客-CSDN博客 静、动态库概念和各自优点 静&#xff1a; 动&#xff1a; 动态库&#xff1a;只有一份&#xff0c;运行时具体代码行才加载使用&#xff08;相对慢&#xff09;&#xff1…

windows docker desktop配置加速地址

目录 为什么常见加速地址在docker desktop上配置 为什么 https://hub.docker.com 是官方的镜像仓库地址&#xff0c;但是它的服务器地址是在国外&#xff0c;有时候访问和下载的速度差强人意。不过好在&#xff0c;我们可以进行远程仓库的设置&#xff0c;将仓库镜像地址设置为…

Kubernetes使用OkHttp客户端进行网络负载均衡

在一次内部Java服务审计中&#xff0c;我们发现一些请求没有在Kubernetes&#xff08;K8s&#xff09;网络上正确地实现负载均衡。导致我们深入研究的问题是HTTP 5xx错误率的急剧上升&#xff0c;由于CPU使用率非常高&#xff0c;垃圾收集事件的数量很多以及超时&#xff0c;但…

使用IntelliJ Idea必备的插件!

趁手的工具让开发事半功倍&#xff0c;好用的IDEA插件让效率加倍。 今天给大家分享几个优秀的IDEA插件。 插件安装 首先得知道在IDEA哪安装插件&#xff1f; 点击File---->Settings---->找到Plugins标签&#xff0c;即可搜索想要的插件进行安装了。 现在来看下有哪些值…

Vue中的监视属性

一、监视属性的使用 &#xff08;一&#xff09;配置watch进行监视 当我们想要监视一个属性改变的时候就可以使用监视属性监视其变化并进行操作。 语法格式如下&#xff1a; watch:{ 监视属性名称 : { // 监视属性的配置项 } } 1. handler函数 当监视的属性发生变化时就调…

简单使用 Hugo 博客

之前用过 hugo&#xff0c;本次来分享一波&#xff0c;确实简单好用&#xff0c;可以持续使用&#xff0c;尤其是喜欢 GO语言的同学 hugo Hugo是一个用 Go语言 编写的静态网站生成器&#xff0c;可以快速地生成高效、安全和易于管理的静态网站。Hugo具有速度快、可定制性强、…