前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >小白学习MySQL - 随机插入测试数据的工具

小白学习MySQL - 随机插入测试数据的工具

作者头像
bisal
发布2021-12-21 16:22:33
1.2K0
发布2021-12-21 16:22:33
举报

我们日常做一些MySQL测试的时候,经常要造数据,通常就写个循环插入数据的存储过程。前两天碰巧看文章说,mysql_random_data_load程序能向MySQL随机插入大量数据,于是了解一下。

mysql_random_data_load是个开源的项目,github路径如下,

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

从信息来看,作者是一个阿根廷的程序员,

089730b4593d7949d586375cac84fe07.png
089730b4593d7949d586375cac84fe07.png

0.1.12是最新的版本,

2dffb38e37d5fe077184dd09c83d174e.png
2dffb38e37d5fe077184dd09c83d174e.png

版本信息如下,

0.1.10

  • Fixed argument validations
  • Fixed ~/.my.cnf loading

0.1.10

  • Fixed connection parameters for MySQL 5.7 (set driver's AllowNativePasswords: true)

0.1.9

  • Added support for bunary and varbinary columns
  • By default, read connection params from ${HOME}/.my.cnf

0.1.8

  • Fixed error for triggers created with MySQL 5.6
  • Added Travis-CI
  • Code clean up

0.1.7

  • Support for MySQL 8.0
  • Added --print parameter
  • Added --version parameter
  • Removed qps parameter

0.1.6

  • Improved generation speed (up to 50% faster)
  • Improved support for TokuDB (Thanks Agustin Gallego)
  • Code refactored
  • Improved debug logging
  • Added Query Per Seconds support (experimental)

0.1.5

  • Fixed handling of NULL collation for index parser

0.1.4

  • Fixed handling of time columns
  • Improved support of GENERATED columns

0.1.3

  • Fixed handling of nulls

0.1.2

  • New table parser able to retrieve all the information for fields, indexes and foreign keys constraints.
  • Support for foreign keys constraints
  • Added some tests

0.1.1

  • Fixed random data generation

0.1.0

  • Initial version

可以下载源代码了解他的实现,Go语言写的,

7a1aa514881fceeb3c2ca13faca7e4bf.png
7a1aa514881fceeb3c2ca13faca7e4bf.png

看下这个文件,README.md,基本就可以知道怎么用了,作者提到了他为什么写这个程序,平时写了很多这种构造随机数的程序,不想再造轮子了,写个通用的,

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!  

他支持如下这些数据类型,

66cfa3ddba497ea59bacf0f4a6c75fc0.png
66cfa3ddba497ea59bacf0f4a6c75fc0.png

字符串类型的构造原则如下,根据长度不同,给出不同的规则

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.

如果字段允许NULL,随机产生10%的空值,

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

还支持外键模拟,

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 . 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 FROM . 1.2 If the number of rows is greater than max-fk-samples, samples are retrieved from the referenced table using this query:   SELECT FROM . WHERE RAND() <= LIMIT

mysql_random_data_load支持以下参数,

23f56fb1a13fde757eb8b533a3ada0c6.png
23f56fb1a13fde757eb8b533a3ada0c6.png

测试一下,创建一张表,

代码语言:javascript
复制
create table test(id int, c1 varchar(1));

执行指令,6秒插入10万数据,

代码语言:javascript
复制
-bash-4.2$ ./mysql_random_data_load -h127.0.0.1 -u用户名 -p密码 --max-threads=2(线程数) 数据库名称 表名 100000(插入数据量)
INFO[2021-12-17T14:31:41+08:00] Starting
   6s [====================================================================] 100%
   6s [====================================================================] 100%

看下效果,id就只有2条相同的,

1089332bb81cd85494aed3b4dd2c81ce.png
1089332bb81cd85494aed3b4dd2c81ce.png

c1列有20个值,数据比较分散,

563ff9c36c8f0a13006fdcfa36bc3bbe.png
563ff9c36c8f0a13006fdcfa36bc3bbe.png

还是能满足基本的测试数据要求的,操作简单,基本的数据类型都可以支持,空值、外键这些特殊的场景,也都能支持,不错的开源工具,推荐一下。

小白学习MySQL,

小白学习MySQL - varchar类型字段为什么经常定义成255?

小白学习MySQL - 变通创建索引的案例一则

小白学习MySQL - “投机取巧”统计表的记录数

小白学习MySQL - 一次慢SQL的定位

小白学习MySQL - TIMESTAMP类型字段非空和默认值属性的影响

小白学习MySQL - 聊聊数据备份的重要性

小白学习MySQL - InnoDB支持optimize table?

小白学习MySQL - table_open_cache的作用

小白学习MySQL - 表空间碎片整理方法

小白学习MySQL - 大小写敏感问题解惑

小白学习MySQL - only_full_group_by的校验规则

小白学习MySQL - max_allowed_packet

小白学习MySQL - mysqldump保证数据一致性的参数差异

小白学习MySQL - 查询会锁表?

小白学习MySQL - 索引键长度限制的问题

小白学习MySQL - MySQL会不会受到“高水位”的影响?

小白学习MySQL - 数据库软件和初始化安装

小白学习MySQL - 闲聊聊

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-12-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档