我有一项要求更新数据库中可用的300 000条记录。当我编写一个独立程序时,需要25-30分钟来更新5 000条记录。所以要完成所有记录,可能需要30个小时。然后我想我会写一个多线程程序。我创建了2个线程然后我开始更新,它花了相同的时间意味着30分钟的5k记录。
据我所知,我们使用该线程进行方法的并发访问,在这种情况下它不会加速更新。
对于上述场景,我需要做些什么来减少时间。什么是多线程的实际用途
class MyThread1 extends Thread{
public static String getTaskID(String PID)
{
String taskID = OurGenerator.orgPID(PID);
return taskID;
}
Connection con;
PreparedStatement pstmt;
BufferedReader bf;
MyThread1(Connection con,PreparedStatement pstmt){
this.con=con;
this.pstmt=pstmt;
try {
bf=new BufferedReader(new FileReader("D:/prod_review_sifid3.txt"));
}catch (IOException e)
{
System.out.println("IO Error Occurred: " + e.toString());
}
}
public void run(){
String line;
try{
while (( line = bf.readLine()) != null)
{
String taskID = getTaskID(line);
pstmt.setString(1,taskID);
pstmt.setString(2,line);
pstmt.executeUpdate();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
class MyThread2 extends Thread{
public static String getTaskID(String PID)
{
String taskID = OurGenerator.orgPID(PID);
return taskID;
}
Connection con;
PreparedStatement pstmt;
BufferedReader bf;
MyThread2(Connection con,PreparedStatement pstmt){
this.con=con;
this.pstmt=pstmt;
try {
bf=new BufferedReader(new FileReader("D:/sifid_review2.txt"));
}catch (IOException e)
{
System.out.println("IO Error Occurred: " + e.toString());
}
}
public void run(){
String line;
try{
while (( line = bf.readLine()) != null)
{
String taskID = getTaskID(line);
pstmt.setString(1,taskID);
pstmt.setString(2,line);
pstmt.executeUpdate();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public class SifuuidInsert {
public static void main(String ar[])throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("","","");
PreparedStatement pstmt=con.prepareStatement("update Taskdata set taskID=? where entryid=?");
MyThread1 first=new MyThread1(con,pstmt);
first.start();
MyThread2 second=new MyThread2(con,pstmt);
second.start();
}
}
答案 0 :(得分:0)
首先:使用批量更新添加查询并推送到集合的末尾,信息here (general)和here (for oracle
第二:你正在为一个新文件重复一个类实现,只需为接收文件路径的类添加一个参数。
class MyThrea extends Thread {
public static String getTaskID(String PID) {
String taskID = OurGenerator.orgPID(PID);
return taskID;
}
Connection con;
BufferedReader bf;
MyThread1(Connection con, String path) {
this.con = con;
this.pstmt = pstmt;
try {
bf = new BufferedReader(new FileReader(path));
} catch (IOException e) {
System.out.println("IO Error Occurred: " + e.toString());
}
}
public void run() {
PreparedStatement pstmt = con.prepareStatement("update Taskdata set taskID=? where entryid=?");
String line;
try {
while ((line = bf.readLine()) != null) {
String taskID = getTaskID(line);
pstmt.setString(1, taskID);
pstmt.setString(2, line);
pstmt.addBatch();
}
pstmt.executeBatch();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class SifuuidInsert {
public static void main(String ar[]) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("", "", "");
MyThread first = new MyThread1(con, "path_to_file_1");
first.start();
MyThread second = new MyThread1(con, "path_to_file_2");
second.start();
}
}
可以通过首先加载文件,然后执行更新来改进它。
答案 1 :(得分:0)
数据库更新的大部分工作都发生在服务器上。根据数据库和特定服务器的配置,可能有助于向服务器提供多个工作流 - 或者它可能会受到影响,或者根本不会改变性能。
然而,大多数情况下,除非每个线程都有自己与数据库的独立连接,否则它不会有帮助。
答案 2 :(得分:-1)
MultiTherading用于并发访问,而不是用于时间和性能目的。您应该通过更新一些类似的记录直接尝试批量更新