加入收藏 | 设为首页 | 会员中心 | 我要投稿 4S站长网 (https://www.4s3.cn/)- 科技、混合云存储、数据迁移、云上网络、数据计算!
当前位置: 首页 > 数据库 > MySql > 正文

mysql 批量插入数据过多的解决方法

发布时间:2020-09-01 11:45:48 所属栏目:MySql 来源:互联网
导读:使用场景:测试时需要插入100w的数据,跑sql脚本插入非常慢。存储过程如下://DELIMITERDROP PROCEDURE if EXISTS createAmountCount;create PROCEDURE createAmountCount()BEGINDECLARE i int;set i=0;drop table if exists person ;create table person(id

使用场景:

测试时需要插入100w的数据,跑sql脚本插入非常慢。

存储过程如下:

i i () innodb charset i person (name,age) (CONCAT( ii

解决方案:

1. 使用sql 生成100w数据到txt文件中。

main(String[] args) BufferedWriter writer = BufferedWriter( FileWriter( File("D:/driver/data.txt"),( i=0;i<1000000;i++(i%10==0"赵"+(i/10)+"t"+ ()(Math.random()*100)+"n"(i%10==1"钱"+(i/10)+"t"+ ()(Math.random()*100)+"n"(i%10==2"孙"+(i/10)+"t"+ ()(Math.random()*100)+"n"(i%10==3"李"+(i/10)+"t"+ ()(Math.random()*100)+"n"(i%10==4"郑"+(i/10)+"t"+ ()(Math.random()*100)+"n"(i%10==5"吴"+(i/10)+"t"+ ()(Math.random()*100)+"n"(i%10==6"周"+(i/10)+"t"+ ()(Math.random()*100)+"n"(i%10==7"王"+(i/10)+"t"+ ()(Math.random()*100)+"n"(i%10==8"张"+(i/10)+"t"+ ()(Math.random()*100)+"n"(i%10==9"刘"+(i/10)+"t"+ ()(Math.random()*100)+"n"

2. 数据库中将数据导入表中:

innodb charset<span style="color: #0000ff;">load data local infile <span style="color: #ff0000;">'<span style="color: #ff0000;">D:/driver/data.txt<span style="color: #ff0000;">'
<span style="color: #0000ff;">into <span style="color: #0000ff;">table<span style="color: #000000;"> person(name,age);

<span style="color: #0000ff;">select <span style="color: #ff00ff;">count(<span style="color: #808080;">*) <span style="color: #0000ff;">from person;

耗费时间:

data local infile .067s

本方案的缺点:

1.权限。 非admin用户没有导入。

2. 写文件代码。对测试人员来说,写文件不仅仅限于java,可以使用任何语言实现之。

(编辑:4S站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读