MySQL 存储过程批量插入总结

功能需求背景:今天接到产品经理核心业务表的数据压测功能,让我向核心业务表插入百万级的业务量数据,我首先想到的办法就是存储过程实现数据的批量


由于无法提供核心业务表,本文仅仅提供我刚刚自己创建的表bds_base_user 表做相关功能展示。

表DDL

CREATE TABLE `bds_base_user`(`id` varchar(64) NOT NULL,`age` int(11) DEFAULT'O',`name` varchar(64) NOT NULL,`amount` decimal(20,8) NOT NULL,PRIMARY KEY (id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

存储过程版本1.0 

create procedure proc_batch
begindeclare i int default 0;declare uid varchar(64) default declare uamount decimal(20,8) default 0;declare uage int default 0;declare uname varchar(64) default '';-- 定义SQL异常处理:主键重复declare continue handler for 1062 select 'duplicate key error encountered';-- 开启事务start transaction;-- 循环遍历--组装idselect concat('1',ceiling(rand() * 9000000000 + 1000000000)) into uid;--组装ageselect floor(18 + (rand() * 9)) into uage;--组装amountselect round((10 +(rand() * 1001)), 8) into uamount;--组装nameselect concat('测试账户',floor(rand()*11+10)) into uname;insert into bds _base_user(id, age, name, amount) values(uid, uage, uname, uamount);set i=i+1;if i % 1000 = 0 thencommit;end if;
end while;
end

我在自己的座位上正在正常执行向核心业务表的注入数据的存储过程,产品经理火急火燎的跑过来通知我,批量注入的数据质量必须考虑。

存储过程版本2.0

目标: 主要解决模拟数据的质量问题,主要体现在:姓名、电话号码、邮箱、公司名称、家庭\公司住址、字段限定取值范围(涉及字符串和整形)、金额、 时间、系统主键和默认值等相关问题。

解决办法:采用自定义函数实现

自定义函数总结

集合生成和判断

-- 生成随机数1-3
select round(rand()*(3-0) + 0) from dual;--指定集合随机获取
select el from(
select'新增' as el from dual
union all select '修改' from dual
union all select '删除' from dual
union all select '查询' from dual
)as temp
order by rand()
limit 1;-- 判断指定字符串是否在指定集合范围内
select ifnull(count(1), 0) from(
select'新增' as el from dual
union all select '修改' from dual
union all select '删除' from dual
union all select '查询' from dual
)as temp where el ='测试'
-- 生成随机电话号码
select  concat('1',substring(cast(3 + (rand() * 10) % 7 AS char(50)), 1, 1),right(left(trim(cast(rand() AS char(50))), 11), 9));
-- 生成随机邮箱
select CONCAT( FLOOR(UNIX_TIMESTAMP()+10000000000*rand()),'@',ELT(CEILING(RAND( ) * 4) ,  "qq.com","163.com","sina.com","gmail.com")) 
-- 生成随机姓名
select concat(substring('赵钱孙李周吴郑王冯陈诸卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮齐康伍余元卜顾孟平黄和穆萧尹姚邵堪汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董粱杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯咎管卢莫经房裘干解应宗丁宣贲邓郁单杭洪包诸左石崔吉钮龚',CEILING(190*rand()),1),substring('明国华建文平志伟东海强晓生光林小民永杰军金健一忠洪江福祥中正振勇耀春大宁亮宇兴宝少剑云学仁涛瑞飞鹏安亚泽世汉达卫利胜敏群波成荣新峰刚家龙德庆斌辉良玉俊立浩天宏子松克清长嘉红山贤阳乐锋智青跃元武广思雄锦威启昌铭维义宗英凯鸿森超坚旭政传康继翔栋仲权奇礼楠炜友年震鑫雷兵万星骏伦绍麟雨行才希彦兆贵源有景升惠臣慧开章润高佳虎根远力进泉茂毅富博霖顺信凡豪树和恩向道川彬柏磊敬书鸣芳培全炳基冠晖京欣廷哲保秋君劲轩帆若连勋祖锡吉崇钧田石奕发洲彪钢运伯满庭申湘皓承梓雪孟其潮冰怀鲁裕翰征谦航士尧标洁城寿枫革纯风化逸腾岳银鹤琳显焕来心凤睿勤延凌昊西羽百捷定琦圣佩麒虹如靖日咏会久昕黎桂玮燕可越彤雁孝宪萌颖艺夏桐月瑜沛诚夫声冬奎扬双坤镇楚水铁喜之迪泰方同滨邦先聪朝善非恒晋汝丹为晨乃秀岩辰洋然厚灿卓杨钰兰怡灵淇美琪亦晶舒菁真涵爽雅爱依静棋宜男蔚芝菲露娜珊雯淑曼萍珠诗璇琴素梅玲蕾艳紫珍丽仪梦倩伊茜妍碧芬儿岚婷菊妮媛莲娟一',CEILING(400*rand()),1),substring('明国华建文平志伟东海强晓生光林小民永杰军金健一忠洪江福祥中正振勇耀春大宁亮宇兴宝少剑云学仁涛瑞飞鹏安亚泽世汉达卫利胜敏群波成荣新峰刚家龙德庆斌辉良玉俊立浩天宏子松克清长嘉红山贤阳乐锋智青跃元武广思雄锦威启昌铭维义宗英凯鸿森超坚旭政传康继翔栋仲权奇礼楠炜友年震鑫雷兵万星骏伦绍麟雨行才希彦兆贵源有景升惠臣慧开章润高佳虎根远力进泉茂毅富博霖顺信凡豪树和恩向道川彬柏磊敬书鸣芳培全炳基冠晖京欣廷哲保秋君劲轩帆若连勋祖锡吉崇钧田石奕发洲彪钢运伯满庭申湘皓承梓雪孟其潮冰怀鲁裕翰征谦航士尧标洁城寿枫革纯风化逸腾岳银鹤琳显焕来心凤睿勤延凌昊西羽百捷定琦圣佩麒虹如靖日咏会久昕黎桂玮燕可越彤雁孝宪萌颖艺夏桐月瑜沛诚夫声冬奎扬双坤镇楚水铁喜之迪泰方同滨邦先聪朝善非恒晋汝丹为晨乃秀岩辰洋然厚灿卓杨钰兰怡灵淇美琪亦晶舒菁真涵爽雅爱依静棋宜男蔚芝菲露娜珊雯淑曼萍珠诗璇琴素梅玲蕾艳紫珍丽仪梦倩伊茜妍碧芬儿岚婷菊妮媛莲娟一',CEILING(400*rand()),1));
-- 生成随机省
SELECT ELT(CEILING(RAND( ) * 34) ,  "河北省","山西省","辽宁省","吉林省","黑龙江省","江苏省","浙江省","安徽省","福建省","江西省","山东省","河南省","湖北省","湖南省","广东省","海南省","四川省","贵州省","云南省","陕西省","甘肃省","青海省","台湾省","内蒙古自治区","广西壮族自治区","西藏自治区","宁夏回族自治区","新疆维吾尔自治区","北京市","上海市","天津市","重庆市","香港特别行政区","澳门特别行政区")
-- 生成随机市
SELECT ELT(CEILING(RAND( ) * 269) ,"石家庄","保定市","秦皇岛","唐山市","邯郸市","邢台市","沧州市","承德市","廊坊市","衡水市","张家口","太原市","大同市","阳泉市","长治市","临汾市","晋中市","运城市","晋城市","忻州市","朔州市","吕梁市","呼和浩特","呼伦贝尔","包头市","赤峰市","乌海市","通辽市","鄂尔多斯","乌兰察布","巴彦淖尔","盘锦市","鞍山市","抚顺市","本溪市","铁岭市","锦州市","丹东市","辽阳市","葫芦岛","阜新市","朝阳市","营口市","吉林市","通化市","白城市","四平市","辽源市","松原市","白山市","伊春市","牡丹江","大庆市","鸡西市","鹤岗市","绥化市","双鸭山","七台河","佳木斯","黑河市","齐齐哈尔市","无锡市","常州市","扬州市","徐州市","苏州市","连云港","盐城市","淮安市","宿迁市","镇江市","南通市","泰州市","绍兴市","温州市","湖州市","嘉兴市","台州市","金华市","舟山市","衢州市","丽水市","合肥市","芜湖市","亳州市","马鞍山","池州市","淮南市","淮北市","蚌埠市","巢湖市","安庆市","宿州市","宣城市","滁州市","黄山市","六安市","阜阳市","铜陵市","福州市","泉州市","漳州市","南平市","三明市","龙岩市","莆田市","宁德市","南昌市","赣州市","景德镇","九江市","萍乡市","新余市","抚州市","宜春市","上饶市","鹰潭市","吉安市","潍坊市","淄博市","威海市","枣庄市","泰安市","临沂市","东营市","济宁市","烟台市","菏泽市","日照市","德州市","聊城市","滨州市","莱芜市","郑州市","洛阳市","焦作市","商丘市","信阳市","新乡市","安阳市","开封市","漯河市","南阳市","鹤壁市","平顶山","濮阳市","许昌市","周口市","三门峡","驻马店","荆门市","咸宁市","襄樊市","荆州市","黄石市","宜昌市","随州市","鄂州市","孝感市","黄冈市","十堰市","长沙市","郴州市","娄底市","衡阳市","株洲市","湘潭市","岳阳市","常德市","邵阳市","益阳市","永州市","张家界","怀化市","江门市","佛山市","汕头市","湛江市","韶关市","中山市","珠海市","茂名市","肇庆市","阳江市","惠州市","潮州市","揭阳市","清远市","河源市","东莞市","汕尾市","云浮市","广西省:","南宁市","贺州市","柳州市","桂林市","梧州市","北海市","玉林市","钦州市","百色市","防城港","贵港市","河池市","崇左市","来宾市","海口市","三亚市","乐山市","雅安市","广安市","南充市","自贡市","泸州市","内江市","宜宾市","广元市","达州市","资阳市","绵阳市","眉山市","巴中市","攀枝花","遂宁市","德阳市","贵阳市","安顺市","遵义市","六盘水","昆明市","玉溪市","大理市","曲靖市","昭通市","保山市","丽江市","临沧市","拉萨市","阿里","咸阳市","榆林市","宝鸡市","铜川市","渭南市","汉中市","安康市","商洛市","延安市","兰州市","白银市","武威市","金昌市","平凉市","张掖市","嘉峪关","酒泉市","庆阳市","定西市","陇南市","天水市","西宁市","银川市","固原市","青铜峡市","石嘴山市","中卫市","乌鲁木齐","克拉玛依市")
-- 生成随机区/县
SELECT ELT(CEILING(RAND( ) * 23) ,  "朝阳区","海淀区","通州区","房山区","丰台区","昌平区","大兴区","顺义区","西城区","延庆县","石景山区","宣武区","怀柔区","崇文区","密云县","东城区","门头沟区","平谷区","江北区","渝北区","沙坪坝区","九龙坡区","万州区")
-- 生成随机1990 后30年
SELECT date( CONCAT(CEILING(30*RAND())+1990,'-',CEILING(12*RAND()),'-',CEILING(30*RAND())) )

创建生成主键函数:

delimiter //
create function fun_id()returns varchar(64)
beginreturn concat('10000',SUBSTR(md5(UUID()), 5));
end //
delimiter ;select fun_id() from dual;

MySQL 8 执行遇到的报错:

This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable)

造成原因:MySQL 服务中/my.ini 配置中log_bin_trust_function_creators  变量默认为关闭,需要设置为开启。

方式1临时方案,MySQL服务重启会失效)
可以通过SQL语句设置,SQL语句如下:
1)查询变量值

show variables like 'log_bin_trust_function_creators';

 
2)设置变量值

set global log_bin_trust_function_creators = 1;show variables like 'log_bin_trust_function_creators';


方式2永久方案,需要重启MySQL服务,配置才能生效)
在 my.cnf 配置文件中添加如下一行配置,然后重启MySQL服务

log_bin_trust_function_creators = 1

 创建随机生成家庭/公司地址函数

新增省、市、区/县和街道数据表。

-- bill.base_province definitionCREATE TABLE `base_province` (`id` int NOT NULL AUTO_INCREMENT,`code` varchar(100) NOT NULL,`name` varchar(100) NOT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
-- bill.base_city definitionCREATE TABLE `base_city` (`id` int NOT NULL AUTO_INCREMENT,`code` varchar(64) NOT NULL,`name` varchar(64) NOT NULL,`province_code` varchar(64) NOT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=343 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
-- bill.base_area definitionCREATE TABLE `base_area` (`id` int NOT NULL AUTO_INCREMENT,`code` varchar(64) NOT NULL,`name` varchar(64) NOT NULL,`city_code` varchar(64) NOT NULL,`province_code` varchar(64) NOT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2979 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
-- bill.base_street definitionCREATE TABLE `base_street` (`id` int NOT NULL AUTO_INCREMENT,`code` varchar(64) NOT NULL,`name` varchar(64) NOT NULL,`arae_code` varchar(64) NOT NULL,`city_code` varchar(64) NOT NULL,`province_code` varchar(64) NOT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=41353 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

数据来源请参考:Administrative-divisions-of-China

核心SQL:

SELECT (select p.name  from base_province p where p.code = t1.province_code) as provinceName,(select c.name  from base_city c where c.code = t1.city_code) as cityName,(select a.name  from base_area a where a.code = t1.arae_code) as areaName,t1.name FROM base_street AS t1 JOIN 
(SELECT ROUND(RAND() * ((SELECT MAX(id) FROM base_street)-(SELECT MIN(id) FROM base_street ))+(SELECT MIN(id) FROM base_street)) AS id) AS t2 WHERE t1.id >= t2.id  
ORDER BY t1.id LIMIT 1 ;

效果截图:

最后用随机函数生成号数 ,至此完成随机生成地址信息。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/266967.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

缓存穿透解决方案之布隆过滤器

布隆过滤器可以快速判断数据是否存在,避免从数据库中查询数据是否存在,减轻数据库的压力 布隆过滤器是由一个初值为0的bit数组和N个哈希函数,可以用来快速的判断某个数据是否存在 当我们想要标记某个数据是否存在时,布隆过滤器会…

FPGA之带有进位逻辑的加法运算

module ADDER( input [5:0]A, input [5:0]B,output[6:0]Q ); assign Q AB; endmodule 综合结果如下图所示: 使用了6个Lut,,6个LUT分布…

2023年全国职业院校技能大赛中职组大数据应用与服务赛项题库参考答案陆续更新中,敬请期待…

2023年全国职业院校技能大赛中职组大数据应用与服务赛项题库参考答案陆续更新中,敬请期待… 武汉唯众智创科技有限公司 2024 年 2 月 联系人:辜渝傧13037102709 题号:试题01 模块三:业务分析与可视化 (一&#xff0…

ONLYOFFICE 桌面编辑器 v8.0 更新内容详细攻略

文章目录 引言PDF 表单RTL 支持电子表格中的新增功能Moodle 集成用密码保护 PDF 文件从“开始”菜单快速创建文档本地界面主题下载安装桌面编辑工具总结 引言 官网链接: ONLYOFFICE 官方网址 ONLYOFFICE 桌面编辑器是一款免费的文档处理软件,适用于 Li…

ETL数据仓库的使用方式

一、ETL的过程 在 ETL 过程中,数据从源系统中抽取(Extract),经过各种转换(Transform)操作,最后加载(Load)到目标数据仓库中。以下是 ETL 数仓流程的基本步骤&#xff1a…

SLAM运动模型

经典的SLAM模型是由一个运动方程和一个观测方程构成的,如下图所示: 其中:x_k为机器人的状态,z为机器人的观测数据,u_k为控制数据,y_j观测点,w和v分别为运动噪声和观测噪声。f为运动方程&#xf…

【教程】移动互联网时代的APP上架流程和要点

目录 摘要 引言 正文 一、应用商店注册 二、准备APP材料 三、打包上传App 摘要 本文将介绍移动应用程序上架的基本流程和要点,包括应用商店注册、APP材料准备、打包上传App、APP审核以及发布APP的详细步骤。此外,还会提到利用appuploder工具简化i…

14:00面试,14:07就出来了,问的问题过于变态了。。。

我从一家小公司转投到另一家公司,期待着新的工作环境和机会。然而,新公司的加班文化让我有些始料未及。虽然薪资相对较高,但长时间的工作和缺乏休息使我身心俱疲。 就在我逐渐适应这种高强度的工作节奏时,公司突然宣布了一则令人…

Python实现链表:从基础到应用

一、引言 链表是一种常见的数据结构,它由一系列节点组成,每个节点包含数据和指向下一个节点的指针。链表在内存中的存储不是连续的,这使得它在插入和删除操作上具有较高的效率。本文将使用Python语言来实现一个简单的链表,并展示其…

kubectl 陈述式资源管理方法

陈述式资源管理方法 kubernetes 集群管理集群资源的唯一入口是通过相应的方法调用 apiserver 的接口 kubectl 是官方的CLI命令行工具,用于与 apiserver 进行通信,将用户在命令行输入的命令,组织并转化为 apiserver 能识别的信息,…

又挖到宝了!国人团队研发的AI视频工具PixVerse,这么好用居然还完全免费!(强烈推荐)

昨天发了一款国产免费的 AI 绘画工具 Dreamina 的介绍: 居然才发现!字节跳动旗下国产AI绘画工具Dreamina,这么好用居然还免费!(强烈推荐) 发现大家对国产 AI 工具还挺感兴趣的。今天继续帮大家挖国产的 A…

项目-SERVER模块-Buffer模块

Buffer模块 一、Buffer模块是什么?实现思想是什么?二、代码实现如何设计:1.成员变量:2.构造函数:3.获取地址和空间大小4.读写偏移向后移动5.扩容函数6.写入函数7.读取函数8.获取地址和空间大小9.获取地址和空间大小10.…

python中版本,操作系统等问题汇总

1. linux源码部署到windows 1.1ModuleNotFoundError: No module named pwd 这个问题&#xff0c;是因为源码是给linux的。这里在windows中&#xff0c;没有该命令。 解决方法之一&#xff0c;在相应的环境目录中&#xff0c;如图中<MetaGPTenv>虚拟环境中&#xff0c;在…

Linux信号【产生-保存-处理】

目录 前言&#xff1a; 1、进程信号基本概念 1.1、什么是信号&#xff1f; 1.2、信号的作用 2、键盘键入 2.1、ctrlc 终止前台进程 2.1.1、signal 注册执行动作 3、系统调用 3.1、kill 函数 3.2、模拟实现 myKill 3.3、raise 函数 3.4、abort 函数 4、软件条件信号…

消息队列+更新DB极易引发的DB并发修改bug

背景 我们在生产系统中和其他系统进行交互时一般都会通过消息队列来解耦生产者和消费者&#xff0c;然后通过每个使用方消费消息队列的消息的方式来完成消息的消费&#xff0c;并且一般来说我们消费消息后极有可能会操作DB&#xff0c;不过这种方式如果处理不够仔细&#xff0…

【C++从0到王者】第四十六站:图的深度优先与广度优先

文章目录 一、图的遍历二、广度优先遍历1.思想2.算法实现3.六度好友 三、深度优先遍历1.思想2.代码实现 四、其他问题 一、图的遍历 对于图而言&#xff0c;我们的遍历一般是遍历顶点&#xff0c;而不是边&#xff0c;因为边的遍历是比较简单的&#xff0c;就是邻接矩阵或者邻接…

electron+vue3全家桶+vite项目搭建【28】封装窗口工具类【2】窗口组,维护窗口关系

文章目录 引入实现效果思路主进程模块渲染进程模块测试效果 引入 demo项目地址 窗口工具类系列文章&#xff1a; 封装窗口工具类【1】雏形 我们思考一下窗口间的关系&#xff0c;窗口创建和销毁的一些动作&#xff0c;例如父子窗口&#xff0c;窗口组合等等&#xff0c;还有…

无字母数字rce总结(自增、取反、异或、或、临时文件上传)

目录 自增 取反 异或 或 临时文件上传 自增 自 PHP 8.3.0 起&#xff0c;此功能已软弃用 在 PHP 中&#xff0c;可以递增非数字字符串。该字符串必须是字母数字 ASCII 字符串。当到达字母 Z 且递增到下个字母时&#xff0c;将进位到左侧值。例如&#xff0c;$a Z; $a;将…

Day07:基础入门-抓包技术全局协议封包监听网卡模式APP小程序PC应用

目录 非HTTP/HTTPS协议抓包工具 WireShark 科来网络分析系统 WPE封包 思维导图 章节知识点&#xff1a; 应用架构&#xff1a;Web/APP/云应用/三方服务/负载均衡等 安全产品&#xff1a;CDN/WAF/IDS/IPS/蜜罐/防火墙/杀毒等 渗透命令&#xff1a;文件上传下载/端口服务/Sh…

强大而灵活的python装饰器

装饰器&#xff08;Decorators&#xff09; 一、概述 在Python中&#xff0c;装饰器是一种特殊类型的函数&#xff0c;它允许我们修改或增强其他函数的功能&#xff0c;而无需修改其源代码。装饰器在函数定义之后立即调用&#xff0c;并以函数对象作为参数。装饰器返回一个新…