MySQL數(shù)據(jù)庫10秒內(nèi)插入百萬條數(shù)據(jù)的實現(xiàn)
首先我們思考一個問題:
要插入如此龐大的數(shù)據(jù)到數(shù)據(jù)庫,正常情況一定會頻繁地進(jìn)行訪問,什么樣的機器設(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)用
// 導(dǎ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)當(dā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該類用于隨機生成一串不會重復(fù)的字符串
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)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信