MySQL數(shù)據(jù)庫10秒內(nèi)插入百萬條數(shù)據(jù)的實現(xiàn)
首先我們思考一個問題:
要插入如此龐大的數(shù)據(jù)到數(shù)據(jù)庫,正常情況一定會頻繁地進行訪問,什么樣的機器設(shè)備都吃不消。那么如何避免頻繁訪問數(shù)據(jù)庫,能否做到一次訪問,再執(zhí)行呢?
Java其實已經(jīng)給了我們答案。
這里就要用到兩個關(guān)鍵對象:Statement、PrepareStatement
我們來看一下二者的特性:

要用到的BaseDao工具類 (jar包 / Maven依賴) (Maven依賴代碼附在文末)(封裝以便于使用)

注:(重點)rewriteBatchedStatements=true,一次插入多條數(shù)據(jù),只插入一次??!
public class BaseDao { // 靜態(tài)工具類,用于創(chuàng)建數(shù)據(jù)庫連接對象和釋放資源,方便調(diào)用
// 導入驅(qū)動jar包或添加Maven依賴(這里使用的是Maven,Maven依賴代碼附在文末)
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 獲取數(shù)據(jù)庫連接對象
public static Connection getConn() {
Connection conn = null;
try {
// rewriteBatchedStatements=true,一次插入多條數(shù)據(jù),只插入一次
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/million-test?rewriteBatchedStatements=true", "root", "qwerdf");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return conn;
}
// 釋放資源
public static void closeAll(AutoCloseable... autoCloseables) {
for (AutoCloseable autoCloseable : autoCloseables) {
if (autoCloseable != null) {
try {
autoCloseable.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
接下來上關(guān)鍵代碼及注釋:
/* 因為數(shù)據(jù)庫的處理速度是非常驚人的 單次吞吐量很大 執(zhí)行效率極高
addBatch()把若干sql語句裝載到一起,然后一次送到數(shù)據(jù)庫執(zhí)行,執(zhí)行需要很短的時間
而preparedStatement.executeUpdate() 是一條一條發(fā)往數(shù)據(jù)庫執(zhí)行的 時間都消耗在數(shù)據(jù)庫連接的傳輸上面*/
public static void main(String[] args) {
long start = System.currentTimeMillis(); // 獲取系統(tǒng)當前時間,方法開始執(zhí)行前記錄
Connection conn = BaseDao.getConn(); // 調(diào)用剛剛寫好的用于獲取連接數(shù)據(jù)庫對象的靜態(tài)工具類
String sql = "insert into mymilliontest values(null,?,?,?,NOW())"; // 要執(zhí)行的sql語句
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql); // 獲取PreparedStatement對象
// 不斷產(chǎn)生sql
for (int i = 0; i < 1000000; i++) {
ps.setString(1, Math.ceil(Math.random() * 1000000) + "");
ps.setString(2, Math.ceil(Math.random() * 1000000) + "");
ps.setString(3, UUID.randomUUID().toString()); // UUID該類用于隨機生成一串不會重復的字符串
ps.addBatch(); // 將一組參數(shù)添加到此 PreparedStatement 對象的批處理命令中。
}
int[] ints = ps.executeBatch();// 將一批命令提交給數(shù)據(jù)庫來執(zhí)行,如果全部命令執(zhí)行成功,則返回更新計數(shù)組成的數(shù)組。
// 如果數(shù)組長度不為0,則說明sql語句成功執(zhí)行,即百萬條數(shù)據(jù)添加成功!
if (ints.length > 0) {
System.out.println("已成功添加一百萬條數(shù)據(jù)??!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
BaseDao.closeAll(conn, ps); // 調(diào)用剛剛寫好的靜態(tài)工具類釋放資源
}
long end = System.currentTimeMillis(); // 再次獲取系統(tǒng)時間
System.out.println("所用時長:" + (end - start) / 1000 + "秒"); // 兩個時間相減即為方法執(zhí)行所用時長
}
最后我們運行看一下效果:


嘿嘿,這里時長超過了10秒,設(shè)備差點意思,希望理解哈~

<!--連接數(shù)據(jù)庫所用到的mysql-connector-java依賴-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
PS : 添上線程后會更快,在后續(xù)的文章中會作示例。
到此這篇關(guān)于MySQL數(shù)據(jù)庫10秒內(nèi)插入百萬條數(shù)據(jù)的實現(xiàn)的文章就介紹到這了,更多相關(guān)MySQL 插入百萬條數(shù)據(jù) 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 一步步教你利用Mysql存儲過程造百萬級數(shù)據(jù)
- MySQL 百萬級數(shù)據(jù)的4種查詢優(yōu)化方式
- MySQL百萬級數(shù)據(jù)量分頁查詢方法及其優(yōu)化建議
- MySQL百萬級數(shù)據(jù)分頁查詢優(yōu)化方案
- java中JDBC實現(xiàn)往MySQL插入百萬級數(shù)據(jù)的實例代碼
- MySQL單表百萬數(shù)據(jù)記錄分頁性能優(yōu)化技巧
- MySQL使用MyFlash快速恢復誤刪除和修改的數(shù)據(jù)
- MySQL數(shù)據(jù)庫刪除數(shù)據(jù)后自增ID不連續(xù)的問題及解決
- MySQL BinLog如何恢復誤更新刪除數(shù)據(jù)
- 使用 SQL 快速刪除數(shù)百萬行數(shù)據(jù)的實踐記錄
相關(guān)文章
MySQL中使用innobackupex、xtrabackup進行大數(shù)據(jù)的備份和還原教程
這篇文章主要介紹了MySQL中使用innobackupex、xtrabackup進行大數(shù)據(jù)的備份和還原教程,xtrabackup用來對超過10G數(shù)據(jù)的Mysql進行備份和還原任務(wù),需要的朋友可以參考下2014-09-09
DDL數(shù)據(jù)庫與表的創(chuàng)建和管理深入講解使用教程
這篇文章主要介紹了DDL數(shù)據(jù)庫與表的創(chuàng)建和管理,系統(tǒng)架構(gòu)的層面來看,數(shù)據(jù)庫從大到小依次是數(shù)據(jù)庫服務(wù)器(上面安裝了DBMS和數(shù)據(jù)庫)、數(shù)據(jù)庫(也稱database或者schema)、數(shù)據(jù)表、數(shù)據(jù)表的行與列2023-04-04
MySQL數(shù)據(jù)庫的索引原理與慢SQL優(yōu)化的5大原則
這篇文章主要介紹了MySQL數(shù)據(jù)庫的索引原理與慢SQL優(yōu)化的5大原則,包括:建立索引的原則,慢查詢優(yōu)化基本步驟,慢查詢優(yōu)化案例,explain使用,需要的朋友可以參考下2023-02-02

