正文之前
众所周知,昨天跑了一百万条数据,结果活生生的耗了我
这么多时间!!沃日啊!!想死了好么!!本来四点多准备跑完就去健身,结果跑完都五点半了,所以只能先去吃饭,然后晚上去健身了。。。。那感觉还是不一样一些。。所以晚饭多吃了一丢丢,以确保健身完毕不会饿哭!😭
正文
今天听一位老哥的建议。在评论区!用了batch批处理操作。速度陡增!!
美滋滋好么!!
还有另外一位老哥建议我用框架,这个比较麻烦的样子?所以没搞了。。。不过还有个建议就是每次在语句上面动手脚,其实也起到了类似批处理的效果。不过我昨晚高了一波,发现在语句里面一次加太多的values的话,最后因为行数不确定,可能会导致出现null这种值。所以我就没搞了。今天看评论看到了batch这种弄法。所以就去学了一波。。搞完之后美滋滋!!
今天上传完后的记录总数!
运行时间减半了!美滋滋
下面是代码时间!!
import java.io.*;
import java.text.NumberFormat;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class WriteGearData {
protected Mysql_Connect mysql=new Mysql_Connect();
public void Delete(int max){
try {
mysql.Connect();
Statement statement = mysql.getStatement();
String DELETE = "delete from gear where id<"+max;
boolean delete_ok = statement.execute(DELETE);
if (delete_ok) {
System.out.println("Have Fun! Boys!\n\n");
}
statement.close();
mysql.Dis_Connect();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public String getInsertQuery(int id,Object[] name,Object[] value){
String insert="INSERT INTO gear(id";
for (int i=0;i<name.length;++i){
insert+=(","+name[i]);
}
insert+=") values("+id;
for (int i=0;i<value.length;++i){
insert+=(","+value[i]+"");
}
insert+=");";
return insert;
}
public void WriteData() throws IOException {
// Delete(100000);
int hz[] = new int [] {0,10,20,30,40,50,60,70,80,90};
int id = 1005311;
for (int x : hz) {
String filename = "/Users/zhangzhaobo/Documents/Graduation-Design/Data/Healthy Data/h30hz" + x + ".txt";
Scanner scanner=new Scanner(new FileInputStream(filename));
Object[] Name = new String[]{"Sensor1","Sensor2","Sensor3","Sensor4","HZ","category"};
int count = 0;
try {
mysql.Connect();
Statement statement = mysql.getStatement();
do{
//连续添加多条静态SQL
Object [] line = new Object[6];
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(1);
for (int i=0;i<4;++i){
line[i]=Float.parseFloat(nf.format(scanner.nextFloat()));
}
line[4] = x;
line[5] = 1;
String INSERT = getInsertQuery(id, Name, line);
statement.addBatch(INSERT);
id++;
count++;
//执行批量执行
if (count>10000) {
statement.executeBatch();
count = 0;
}
}while(scanner.hasNext());
statement.executeBatch();
statement.close();
mysql.Dis_Connect();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
正文之后
哎。。。为毛总是有人。。。会觉得,借一个程序员的电脑是个很简单的事情??
总得学会拒绝不是?!