Java實(shí)現(xiàn)十秒向MySQL插入百萬條數(shù)據(jù)
mysql數(shù)據(jù)庫準(zhǔn)備
private String Driver = "com.mysql.cj.jdbc.Driver"; private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true"; private String user = "root"; private String password = "root"; Connection connection = null; PreparedStatement ps = null; ResultSet rs = null; //封裝與數(shù)據(jù)庫建立連接的類 public void coon() throws Exception{ Class.forName(Driver); connection = DriverManager.getConnection(url,user,password); } //封裝異常類 public void erro(){ try { if (rs!=null){ rs.close(); } if (ps!=null){ ps.close(); } if (connection!=null){ connection.close(); } } catch (Exception e) { e.printStackTrace(); } }
方式一:普通插入
package com.wt; import org.junit.Test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; /** * @Author wt * @Date 2022/11/14 21:17 * @PackageName:com.wt * @ClassName: TestAddBatch01 * @Description: TODO * @Version 1.0 */ public class TestAddBatch01 { private String Driver = "com.mysql.cj.jdbc.Driver"; private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai"; private String user = "root"; private String password = "root"; Connection connection = null; PreparedStatement ps = null; ResultSet rs = null; public void coon() throws Exception{ Class.forName(Driver); connection = DriverManager.getConnection(url,user,password); } public void erro(){ try { if (rs!=null){ rs.close(); } if (ps!=null){ ps.close(); } if (connection!=null){ connection.close(); } } catch (Exception e) { e.printStackTrace(); } } @Test public void ccc(){ long start = System.currentTimeMillis(); String sql = "insert into a(id, name) VALUES (?,null)"; try { coon(); ps = connection.prepareStatement(sql); for (int i = 1; i <= 1000000; i++) { ps.setObject(1, i);//填充sql語句種得占位符 ps.execute();//執(zhí)行sql語句 } } catch (Exception e) { e.printStackTrace(); } finally { erro(); } System.out.println("百萬條數(shù)據(jù)插入用時:" + (System.currentTimeMillis() - start)+"【單位:毫秒】"); } }
用時:62分鐘多
方式二:使用批處理插入
package com.wt; import org.junit.Test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; /** * @Author wt * @Date 2022/11/14 20:25 * @PackageName:com.wt.util * @ClassName: TestAddBatch * @Description: TODO * @Version 1.0 */ public class TestAddBatch { private String Driver = "com.mysql.cj.jdbc.Driver"; private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai"; private String user = "root"; private String password = "root"; Connection connection = null; PreparedStatement ps = null; ResultSet rs = null; public void coon() throws Exception{ Class.forName(Driver); connection = DriverManager.getConnection(url,user,password); } public void erro(){ try { if (rs!=null){ rs.close(); } if (ps!=null){ ps.close(); } if (connection!=null){ connection.close(); } } catch (Exception e) { e.printStackTrace(); } } @Test public void ccc(){ long start = System.currentTimeMillis(); String sql = "insert into a(id, name) VALUES (?,null)"; try { coon(); ps = connection.prepareStatement(sql); // connection.setAutoCommit(false);//取消自動提交 for (int i = 1; i <= 1000000; i++) { ps.setObject(1, i); ps.addBatch(); if (i % 1000 == 0) { ps.executeBatch(); ps.clearBatch(); } } ps.executeBatch(); ps.clearBatch(); // connection.commit();//所有語句都執(zhí)行完畢后才手動提交sql語句 } catch (Exception e) { e.printStackTrace(); } finally { erro(); } System.out.println("百萬條數(shù)據(jù)插入用時:" + (System.currentTimeMillis() - start)+"【單位:毫秒】"); } }
方式一、二總結(jié):到此可以看出其實(shí)其處理程序及批處理是沒有起作用的,為此我們使用方式三
方式三:通過連接配置url設(shè)置【&rewriteBatchedStatements=true】(設(shè)置重寫批處理語句)
url地址后注意添加【&rewriteBatchedStatements=true】
private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
方法三較于方法二的改變是只是url地址上的改變,其它沒有任何修改
package com.wt; import org.junit.Test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; /** * @Author wt * @Date 2022/11/14 20:25 * @PackageName:com.wt.util * @ClassName: TestAddBatch * @Description: TODO * @Version 1.0 */ public class TestAddBatch { private String Driver = "com.mysql.cj.jdbc.Driver"; private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true"; private String user = "root"; private String password = "root"; Connection connection = null; PreparedStatement ps = null; ResultSet rs = null; public void coon() throws Exception{ Class.forName(Driver); connection = DriverManager.getConnection(url,user,password); } public void erro(){ try { if (rs!=null){ rs.close(); } if (ps!=null){ ps.close(); } if (connection!=null){ connection.close(); } } catch (Exception e) { e.printStackTrace(); } } @Test public void ccc(){ long start = System.currentTimeMillis(); String sql = "insert into a(id, name) VALUES (?,null)"; try { coon(); ps = connection.prepareStatement(sql); for (int i = 1; i <= 1000000; i++) { ps.setObject(1, i); ps.addBatch(); if (i % 1000 == 0) { ps.executeBatch(); ps.clearBatch(); } } ps.executeBatch(); ps.clearBatch(); } catch (Exception e) { e.printStackTrace(); } finally { erro(); } System.out.println("百萬條數(shù)據(jù)插入用時:" + (System.currentTimeMillis() - start)+"【單位:毫秒】"); } }
用時:【10秒左右】
到此批處理語句才正是生效
注意
數(shù)據(jù)庫連接的url設(shè)置了【&rewriteBatchedStatements=true】時,java代碼種的sql語句不能有分號【;】號,否則批處理語句打包就會出現(xiàn)錯誤,導(dǎo)致后面的sql語句提交出現(xiàn)【BatchUpdateException】異常
方式四:通過數(shù)據(jù)庫連接取消自動提交,手動提交數(shù)據(jù)
package com.wt; import org.junit.Test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; /** * @Author wt * @Date 2022/11/14 20:25 * @PackageName:com.wt.util * @ClassName: TestAddBatch * @Description: TODO * @Version 1.0 */ public class TestAddBatch { private String Driver = "com.mysql.cj.jdbc.Driver"; private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true"; private String user = "root"; private String password = "root"; Connection connection = null; PreparedStatement ps = null; ResultSet rs = null; public void coon() throws Exception{ Class.forName(Driver); connection = DriverManager.getConnection(url,user,password); } public void erro(){ try { if (rs!=null){ rs.close(); } if (ps!=null){ ps.close(); } if (connection!=null){ connection.close(); } } catch (Exception e) { e.printStackTrace(); } } @Test public void ccc(){ long start = System.currentTimeMillis(); String sql = "insert into a(id, name) VALUES (?,null)"; try { coon(); ps = connection.prepareStatement(sql); connection.setAutoCommit(false);//取消自動提交 for (int i = 1; i <= 1000000; i++) { ps.setObject(1, i); ps.addBatch(); if (i % 1000 == 0) { ps.executeBatch(); ps.clearBatch(); } } ps.executeBatch(); ps.clearBatch(); connection.commit();//所有語句都執(zhí)行完畢后才手動提交sql語句 } catch (Exception e) { e.printStackTrace(); } finally { erro(); } System.out.println("百萬條數(shù)據(jù)插入用時:" + (System.currentTimeMillis() - start)+"【單位:毫秒】"); } }
用時:【9秒左右】
總結(jié):
1.使用批量提交數(shù)據(jù),url一定要設(shè)置允許重寫批量提交【rewriteBatchedStatements=true】,以及此時的sql語句一定不能有分號,否則有【BatchUpdateException】異常,
2.其他的就正常使用PreparedStatement ps;的以下三個方法即可
- ps.addBatch(); 將sql語句打包到一個容器中
- ps.executeBatch(); 將容器中的sql語句提交
- ps.clearBatch(); 清空容器,為下一次打包做準(zhǔn)備
以上就是Java實(shí)現(xiàn)十秒向MySQL插入百萬條數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于Java MySQL插入數(shù)據(jù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java編程之如何通過JSP實(shí)現(xiàn)頭像自定義上傳
之前做這個頭像上傳功能還是花了好多時間的,今天我將我的代碼分享給大家,下面這篇文章主要給大家介紹了關(guān)于Java編程之如何通過JSP實(shí)現(xiàn)頭像自定義上傳的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12Java基礎(chǔ)之static關(guān)鍵字的使用講解
這篇文章主要介紹了Java基礎(chǔ)之static關(guān)鍵字的使用講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07Spring?Boot?4.0對于Java開發(fā)的影響和前景
探索Spring?Boot?4.0如何徹底革新Java開發(fā),提升效率并開拓未來可能性!別錯過這篇緊湊的指南,它帶你領(lǐng)略Spring?Boot的強(qiáng)大魅力和潛力,準(zhǔn)備好了嗎?2024-02-02GateWay路由規(guī)則與動態(tài)路由詳細(xì)介紹
這篇文章主要介紹了GateWay路由規(guī)則與GateWay動態(tài)路由,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09Spring Security OAuth2實(shí)現(xiàn)使用JWT的示例代碼
這篇文章主要介紹了Spring Security OAuth2實(shí)現(xiàn)使用JWT的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09SpringBoot 利用MultipartFile上傳本地圖片生成圖片鏈接的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot 利用MultipartFile上傳本地圖片生成圖片鏈接的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03