亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長資訊網
    最全最豐富的資訊網站

    java向mysql數據庫批量插入大量數據

    java向mysql數據庫批量插入大量數據

    首先看下我們的目標:向mysql數據庫中批量插入10000條數據

    操作環(huán)境:Mysql和Java代碼都運行在我本地Windows電腦(i7處理器,4核,16G運行內存,64位操作系統(tǒng)

    1、JPA單線程執(zhí)行

    代碼省略,大概需要39S左右

    java向mysql數據庫批量插入大量數據

    2、JPA多線程執(zhí)行

    java向mysql數據庫批量插入大量數據

    大概需要37S左右,并沒有想象中的快很多

    (免費學習視頻分享:java視頻教程)

    原因: 多線程只是大大提高了程序處理數據的時間,并不會提高插入數據庫的時間,相反在我這邊JPA的框架下,多線程也就意味著多連接,反而更加消耗數據庫性能

    package com.example.demo.controller;  import com.example.demo.entity.Student; import com.example.demo.service.StudentServiceInterface; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;  import javax.xml.bind.ValidationException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;  @RestController @RequestMapping("/student") public class StudentController {      @Autowired     private StudentServiceInterface studentServiceInterface;      // 來使主線程等待線程池中的線程執(zhí)行完畢     private CountDownLatch threadsSignal;      // 每個線程處理的數據量     private static final int count = 1000;     // 我的電腦為4核 線程池大小設置為2N+1     private static ExecutorService execPool = Executors.newFixedThreadPool(9);          /**      * 多線程保存      *      * @return      * @throws ValidationException      */     @GetMapping()     public String saveStudentEnableThread() throws ValidationException {         Long begin = new Date().getTime();         // 需要插入數據庫的數據         List<Student> list = new ArrayList<>();         for (int i = 0; i < 10000; i++) {             Student student = new Student();             student.setName("張三");             student.setAge(10);             list.add(student);         }         try {             if (list.size() <= count) {                 threadsSignal = new CountDownLatch(1);                 execPool.submit(new InsertDate(list));             } else {                 List<List<Student>> lists = dealData(list, count);                 threadsSignal = new CountDownLatch(lists.size());                 for (List<Student> students : lists) {                     execPool.submit(new InsertDate(students));                 }             }             threadsSignal.await();         } catch (Exception e) {             System.out.println(e.toString() + " 錯誤所在行數:" + e.getStackTrace()[0].getLineNumber());         }         // 結束時間         Long end = new Date().getTime();         return "10000條數據插入花費時間 : " + (end - begin) / 1000 + " s";     }      /**      * 數據組裝      * 把每個線程要處理的數據 再組成一個List      * 我這邊就是把10000條數據 組成 10個1000條的集合      *      * @param target 數據源      * @param size   每個線程處理的數量      * @return      */     public static List<List<Student>> dealData(List<Student> target, int size) {         List<List<Student>> threadList = new ArrayList<List<Student>>();         // 獲取被拆分的數組個數         int arrSize = target.size() % size == 0 ? target.size() / size : target.size() / size + 1;         for (int i = 0; i < arrSize; i++) {             List<Student> students = new ArrayList<Student>();             //把指定索引數據放入到list中             for (int j = i * size; j <= size * (i + 1) - 1; j++) {                 if (j <= target.size() - 1) {                     students.add(target.get(j));                 }             }             threadList.add(students);         }         return threadList;     }      /**      * 內部類,開啟線程批量保存數據      */     class InsertDate extends Thread {         List<Student> list = new ArrayList<Student>();         public InsertDate(List<Student> students) {             list = students;         }         public void run() {             try {                 // 與數據庫交互                 studentServiceInterface.save(list);                 threadsSignal.countDown();             } catch (ValidationException e) {                 e.printStackTrace();             }         }     } }

    3、傳統(tǒng)JDBC插入

    java向mysql數據庫批量插入大量數據

    大概需要8S左右,相較于前兩種方式已經快很多了,代碼如下:

    package com.example.demo.controller;  import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;  import javax.xml.bind.ValidationException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.util.Date;  @RestController @RequestMapping("/student1") public class StudentController1 {      @GetMapping()     public String saveStudentEnableThread() throws ValidationException {         // 開始時間         Long begin = new Date().getTime();         Connection connection = null;         try {             connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db01?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true", "admin", "123456");//獲取連接             if (connection != null) {                 System.out.println("獲取連接成功");             } else {                 System.out.println("獲取連接失敗");             }             //這里必須設置為false,我們手動批量提交             connection.setAutoCommit(false);             //這里需要注意,SQL語句的格式必須是預處理的這種,就是values(?,?,...,?),否則批處理不起作用             PreparedStatement statement = connection.prepareStatement("insert into student(id,`name`,age) values(?,?,?)");             // 塞數據             for (int i = 0; i < 10000; i++) {                 statement.setInt(1, i+1);                 statement.setString(2, "張三");                 statement.setInt(3, 10);                 //將要執(zhí)行的SQL語句先添加進去,不執(zhí)行                 statement.addBatch();             }             // 提交要執(zhí)行的批處理,防止 JDBC 執(zhí)行事務處理             statement.executeBatch();             connection.commit();             // 關閉相關連接             statement.close();             connection.close();         } catch (Exception e) {             e.printStackTrace();         }         // 結束時間         Long end = new Date().getTime();         // 耗時         System.out.println("10000條數據插入花費時間 : " + (end - begin) / 1000 + " s");         return "10000條數據插入花費時間 : " + (end - begin) / 1000 + " s";     }  }

    4、最后檢查一下數據是否成功存庫,一共30000條,沒有丟數據

    java向mysql數據庫批量插入大量數據

    完成!

    贊(0)
    分享到: 更多 (0)
    網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號