日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

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

發(fā)布時(shí)間:2025/4/5 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql 批量插入数据过多的解决方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

使用場(chǎng)景:

測(cè)試時(shí)需要插入100w的數(shù)據(jù),跑sql腳本插入非常慢。

存儲(chǔ)過(guò)程如下:

//DELIMITER DROP PROCEDURE if EXISTS createAmountCount; create PROCEDURE createAmountCount() BEGIN DECLARE i int; set i=0; drop table if exists person ; create table person( id int not null auto_increment, name varchar(40) not null, age int, primary key(id) )engine=innodb charset=gb2312; while i<10000 DO insert into person (name, age) values(CONCAT('usercode',i)); set i=i+1; end while; END; //DELIMITER CALL createAmountCount();

?

解決方案:

1. 使用sql 生成100w數(shù)據(jù)到txt文件中。

public static void main(String[] args) throws IOException {// TODO Auto-generated method stubBufferedWriter writer = new BufferedWriter(new FileWriter(new File("D:/driver/data.txt"), true));for(int i=0;i<1000000;i++){if(i%10==0){writer.write("趙"+(i/10)+"\t"+ (int)(Math.random()*100)+"\n");}if(i%10==1){writer.write("錢"+(i/10)+"\t"+ (int)(Math.random()*100)+"\n");}if(i%10==2){writer.write("孫"+(i/10)+"\t"+ (int)(Math.random()*100)+"\n");}if(i%10==3){writer.write("李"+(i/10)+"\t"+ (int)(Math.random()*100)+"\n");}if(i%10==4){writer.write("鄭"+(i/10)+"\t"+ (int)(Math.random()*100)+"\n");}if(i%10==5){writer.write("吳"+(i/10)+"\t"+ (int)(Math.random()*100)+"\n");}if(i%10==6){writer.write("周"+(i/10)+"\t"+ (int)(Math.random()*100)+"\n");}if(i%10==7){writer.write("王"+(i/10)+"\t"+ (int)(Math.random()*100)+"\n");}if(i%10==8){writer.write("張"+(i/10)+"\t"+ (int)(Math.random()*100)+"\n");}if(i%10==9){writer.write("劉"+(i/10)+"\t"+ (int)(Math.random()*100)+"\n");}}writer.close();}

2. 數(shù)據(jù)庫(kù)中將數(shù)據(jù)導(dǎo)入表中:

create table person( id int not null auto_increment, name varchar(40) not null, age int, primary key(id) )engine=innodb charset=gb2312;load data local infile 'D:/driver/data.txt' into table person(name,age);select count(*) from person;

耗費(fèi)時(shí)間:

[SQL]load data local infile 'D:/driver/data.txt' into table person(name,age); 受影響的行: 1000000 時(shí)間: 10.067s

本方案的缺點(diǎn):

1.權(quán)限。 非admin用戶沒(méi)有導(dǎo)入。

2. 寫(xiě)文件代碼。對(duì)測(cè)試人員來(lái)說(shuō),寫(xiě)文件不僅僅限于java,可以使用任何語(yǔ)言實(shí)現(xiàn)之。

?

轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/4460309.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的mysql 批量插入数据过多的解决方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。