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.mzph.cn/news/708483.shtml

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

相关文章

nginx 反向代理 与缓存功能

一 理论说明 (一)反向代理简介 反向代理:reverse proxy,指的是代理外网用户的请求到内部的指定的服务器,并将数据返回给用户的一种方式,这是用的比较多的一种方式。 即 代理服务机 Nginx 除了可以在企…

算法——滑动窗口之最大连续1的个数、将x减到0的最小操作数、水果成篮

3.最大连续1的个数 题目:. - 力扣(LeetCode) 题目要求的是给定一个二进制数组 nums 和一个整数 k,如果可以翻转最多 k 个 0 ,则返回 数组中连续 1 的最大个数 。 按照题目正面去做,还要替换0,很麻烦 反正我们最后要求的是最长…

YOLOv8改进 | 独家创新篇 | 结合SOTA思想利用双主干网络改进YOLOv8(全网独家创新,最重磅的更新)

一、本文介绍 本文给大家带来的改进机制是结合目前SOTAYOLOv9的思想利用双主干网络来改进YOLOv8(本专栏目前发布以来改进最大的内容,同时本文内容为我个人一手整理全网独家首发 | 就连V9官方不支持的模型宽度和深度修改我都均已提供,本文内容支持YOLOv8全系列模型从n到x均可…

刷题笔记 洛谷 P1162 填涂颜色

思路来自 大佬 hat.openai.com/c/9c30032e-5fb9-4677-8c15-9ea6530dc6db 题目链接 P1162 填涂颜色 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 思路 搜索 首先 在外面围上一圈0开始搜素 因为题目说将封闭区域内的0变成2 我们可以在外面进行搜索 把外面所有可以搜索…

Nginx----高性能的WEB服务端(四)

一、http 协议反向代理 1、反向代理:缓存功能 ​ proxy_cache zone_name | off; 默认off #指明调用的缓存,或关闭缓存机制;Context:http, server, location #zone_name 表示缓存的名称.需要由proxy_cache_path事先定义proxy_cache_key string; #缓存中…

【Redis】深入理解 Redis 常用数据类型源码及底层实现(5.详解List数据结构)

本文是深入理解 Redis 常用数据类型源码及底层实现系列的第5篇~前4篇可移步( ̄∇ ̄)/ 【Redis】深入理解 Redis 常用数据类型源码及底层实现(1.结构与源码概述)-CSDN博客 【Redis】深入理解 Redis 常用数据类型源码及底…

BeautifulSoup+xpath+re+css简单复习+新的scrapy的学习

1.BeautifulSoupsoup BeautifulSoup(html,html.parser)all_icosoup.find(class_"DivTable") 2.xpath trs resp.xpath("//tbody[idcpdata]/tr") hong tr.xpath("./td[classchartball01 or classchartball20]/text()").extract() 这个意思是找…

文件拖放到窗体事件

网上的实现1 实现结果 具体实现代码:注意需要使能允许拖拽 public partial class Form1 : Form {public Form1(){InitializeComponent();this.AllowDrop true; //允许拖拽}private void Form1_DragEnter(object sender, DragEventArgs e){this.Text DateTime.No…

一键安装|卸载 mysql 8.2.0 shell脚本

场景:为了在无网、外网 mysql 安装方便,这里分享一个自己编写得 shell脚本 这里以当前最新版 mysql 8.2.0;centos-7 二进制包下载: 下载地址 mysql_install.sh #!/bin/bash # 解压安装包 tar -xf mysql-8.2.0-linux-glibc2.17-x8…

TC3xx SMU、PMIC和Tranceiver的功能安全闭环

目录 1.TLF35584安全状态输出响应对象 1.1 响应ERR 收集到的错误信号 1.2 响应监控功能引发的ROT 1.3 响应看门狗引发的错误 1.4 环境过温引发的错误状态 1.5 为什么设计SSx? 2. 安全状态输出给谁 3.小结 在之前文章里,我们简述了TC3xx SMU如何…

尚硅谷(SpringCloudAlibaba微服务分布式)学习代码Eureka部分

1.项目结构 2.cloud2024 pom <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://maven.a…

面试笔记系列六之redis+kafka+zookeeper基础知识点整理及常见面试题

Redis redis持久化机制&#xff1a;RDB和AOF Redis 持久化 Redis 提供了不同级别的持久化方式: RDB持久化方式能够在指定的时间间隔能对你的数据进行快照存储. AOF持久化方式记录每次对服务器写的操作,当服务器重启的时候会重新执行这些命令来恢复原始的数据,AOF命令以redi…

IPD(集成产品开发)—核心思想

企业发展到一定阶段就会遇到管理瓶颈&#xff0c;IPD流程是一种高度结构化的产品开发流程&#xff0c;它集成了业界很多优秀的产品开发方法论&#xff0c;像搭积木一样的组合成一种非常有效的流程。如果我们能根据企业的规模和行业特点&#xff0c;对全流程的IPD进行合适的裁剪…

html2canvas + JsPDF.js 导出pdf分页时的问题

问题描述 前一段时间 实现了html2canvas jspdf.js 导出pdf的功能 项目当时没有测试做完就先搁置 最近项目要上线发现分页时问题 这篇文章记录一下之前的bug import html2canvas from html2canvas; import JsPDF from jspdf export function savePdf(el, title) {html2canva…

Google checkstyle实战

概述 CheckStyle检查代码是否符合制定的规范。CheckStyle检查是基于源码的&#xff0c;无需编译&#xff0c;执行速度快。 CheckStyle的主要流程是&#xff1a; 对Java文件进行词法语法分析&#xff0c;生成语法树。载入配置文件&#xff08;checkstyle-metadata.xml以及自定…

【ElfBoard】基于 Linux 的智能家居小项目

大家好&#xff0c;我是 Hello阿尔法&#xff0c;这段时间参与了保定飞凌嵌入式技术有限公司举办的 ElfBoard 共创社招募活动&#xff0c;并有幸成为了一名共创官&#xff0c;官方寄来了一块 ELF 1 开发板&#xff0c;开箱看这里 ELF 1 开箱初体验。 作为共创官&#xff0c;我…

数据可视化工具选择指南:山海鲸、Tableau与Power BI特点详解

在数据可视化的领域中&#xff0c;众多工具各有千秋。今天&#xff0c;我们将从客观的角度&#xff0c;对三款热门的数据可视化产品——山海鲸可视化、Tableau和Power BI进行深入的对比&#xff0c;帮助用户更全面地了解它们的特点&#xff0c;从而做出更明智的选择。 一、产品…

移动硬盘在电脑上显示不出来?三招教你轻松应对

随着网络的普及&#xff0c;数据的增多&#xff0c;对于数据储存的需要也是越来越多&#xff0c;相信许多人的手中都存在着一些如U盘、MP3、MP4、移动硬盘之类的储存设备&#xff0c;而在使用这些设备的时候&#xff0c;难免会遇到一些这样或那样的问题&#xff0c;比如移动硬盘…

微信小程序证书评级导致接口无法访问问题

微信小程序的ssl证书到期后&#xff0c; 更换了免费的ssl证书&#xff0c; 是在freessl网站申请的&#xff0c; 配置完了&#xff0c;后台可以访问https网页&#xff0c;但是小程序还是无法访问&#xff0c; 开始没有怀疑是https证书的问题&#xff0c; 调适了好长时间的代码&a…

Scala Intellij编译错误:idea报错xxxx“is already defined as”

今天写scala代码时,Idea报了这样的错误&#xff0c;如下图所示&#xff1a; 一般情况下原因分两种&#xff1a; 第一是我们定义的类或对象重复多次出现&#xff0c;编译器无法确定使用哪个定义。 这通常是由于以下几个原因导致的&#xff1a; 重复定义&#xff1a;在同一个文件…