使用场景:

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

存储过程如下:

//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数据到txt文件中。

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedWriter 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. 数据库中将数据导入表中:

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;

耗费时间:

[SQL]load data local infile 'D:/driver/data.txt'
into table person(name,age);
受影响的行: 1000000
时间: 10.067s

本方案的缺点:

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

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

mysql 批量插入数据过多的解决方法的更多相关文章

  1. MySQL批量插入数据的几种方法

    最近公司要求测试数据库的性能,就上网查了一些批量插入数据的代码,发现有好几种不同的用法,插入同样数据的耗时也有区别 别的先不说,先上一段代码与君共享 方法一: package com.bigdata; ...

  2. SQLServer 批量插入数据的两种方法

    SQLServer 批量插入数据的两种方法-发布:dxy 字体:[增加 减小] 类型:转载 在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Ins ...

  3. SQL 2005批量插入数据的二种方法

    SQL 2005批量插入数据的二种方法 Posted on 2010-07-22 18:13 moss_tan_jun 阅读(2635) 评论(2) 编辑 收藏 在SQL Server 中插入一条数据 ...

  4. [转]mysql导入导出数据中文乱码解决方法小结

    本文章总结了mysql导入导出数据中文乱码解决方法,出现中文乱码一般情况是导入导入时编码的设置问题,我们只要把编码调整一致即可解决此方法,下面是搜索到的一些方法总结,方便需要的朋友. linux系统中 ...

  5. python使用MySQLdb向mySQL批量插入数据的方法

    该功能通过调用mySQLdb python库中的 cursor.executemany()函数完成批量处理. 今天用这个函数完成了批量插入 例程: def test_insertDB(options) ...

  6. MyBatis 批量插入数据的 3 种方法!

    批量插入功能是我们日常工作中比较常见的业务功能之一,之前我也写过一篇关于<MyBatis Plus 批量数据插入功能,yyds!>的文章,但评论区的反馈不是很好,主要有两个问题:第一,对 ...

  7. mysql批量插入数据的基类

    自己设计的一个mysql数据库批量添加数据的基类.用于批量向mysql数据库添加数据,子类实现起来很简单,自测性能也还不错. 1.基类实现-BatchAddBase using System.Coll ...

  8. SQL Server 批量插入数据的两种方法

    在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍 SQL Server支持的两种批 ...

  9. SQL Server 批量插入数据的两种方法(转)

    此文原创自CSDN TJVictor专栏:http://blog.csdn.net/tjvictor/archive/2009/07/18/4360030.aspx 在SQL Server 中插入一条 ...

随机推荐

  1. 将矩阵转化为LibSvm需要的格式

    function svmtransform(A)[m,n]=size(A); fid = fopen('A.txt','w');%写入文件路径for i=1:m    temp1 = A(i,2:n) ...

  2. 怎么用PHP在HTML中生成PDF文件

    原文:Generate PDF from html using PHP 译文:使用PHP在html中生成PDF 译者:dwqs 利用PHP编码生成PDF文件是一个非常耗时的工作.在早期,开发者使用PH ...

  3. css优先级判断

    概念 浏览器是通过判断优先级,来决定到底哪些属性值是与元素最相关的,从而应用到该元素上.优先级是由选择器组成的匹配规则决定的. 如何计算? 优先级是根据由每种选择器类型构成的级联字串计算而成的. 它不 ...

  4. [转] Web前端优化之 内容篇

    原文网址: http://lunax.info/archives/3090.html Yahoo! 的 Exceptional Performance team 在 Web 前端方面作出了卓越的贡献. ...

  5. Java 编程要点之并发(Concurrency)详解

    计算机用户想当然地认为他们的系统在一个时间可以做多件事.他们认为,他们可以工作在一个字处理器,而其他应用程序在下载文件,管理打印队列和音频流.即使是单一的应用程序通常也是被期望在一个时间来做多件事.例 ...

  6. javascript !!作用

    javaScript中使用!!表示取得boolean值,具体作用如下 var value= !!test[1]; 取变量的Boolean值, 相当于 var value = test[1]?true: ...

  7. 用JS查看修改CSS样式(cssText,attribute('style'),currentStyle,getComputedStyle)

    CSS样式定义方法 大家都知道,在为HTML设置样式的时候,通常有三种方法:内联样式,内部样式表,外部样式表. 1.内联样式: 内联样式表就是在HTML元素中的行内直接添加style属性. <d ...

  8. codeforces 631A Interview

    A. Interview time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  9. json网页预览插件

  10. C#学习笔记(十六):Attribute

    Attribute可以为类或方法添加一些附加的信息,我们可以看看MSDN对Attribute的描述: 公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注 ...