教你88秒插入1000萬條數據到mysql數據庫表

我用到的數據庫爲,mysql數據庫5.7版本的

  • 首先自己準備好數據庫表

其實我在插入1000萬條數據的時候遇到了一些問題,現在先來解決他們,一開始我插入100萬條數據時候報錯,控制檯的信息如下:

com.mysql.jdbc.PacketTooBigException: Packet for query is too large (4232009 > 4194304). You can change this value on the server by setting the max_allowed_packet’ variable.

出現上面的錯誤是因爲數據庫表的 max_allowed_packet 這個配置沒配置足夠大,因爲默認的爲4M的,後來我調爲100M就沒報錯了

set global max_allowed_packet = 100*1024*1024*

記住,設置好後重新登錄數據庫才能看的設置後的值

show VARIABLES like '%max_allowed_packet%'

  • 代碼如下:
package insert; 
import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Date; 
import com.mysql.jdbc.PreparedStatement; 
public class InsertTest { 
     public static void main(String[] args) throws ClassNotFoundException, SQLException {         final String url = "jdbc:mysql://127.0.0.1/teacher" ; 
         final String name = "com.mysql.jdbc.Driver" ; 
         final String user = "root" ; 
         final String password = "123456" ; 
         Connection conn = null ; 
         Class.forName(name); //指定連接類型 
         conn = DriverManager.getConnection(url, user, password); //獲取連接 
         if (conn!= null ) {
             System.out.println( "獲取連接成功" );
             insert(conn);
         } else {
             System.out.println( "獲取連接失敗" );
         }
 
     }     public static void insert(Connection conn) {         // 開始時間
         Long begin = new Date().getTime();         // sql前綴
         String prefix = "INSERT INTO t_teacher (id,t_name,t_password,sex,description,pic_url,school_name,regist_date,remark) VALUES " ;         try {             // 保存sql後綴
             StringBuffer suffix = new StringBuffer();             // 設置事務爲非自動提交
             conn.setAutoCommit( false );             // 比起st,pst會更好些
             PreparedStatement  pst = (PreparedStatement) conn.prepareStatement( "" ); //準備執行語句
             // 外層循環,總提交事務次數
             for ( int i = 1 ; i <= 100 ; i++) {
                 suffix = new StringBuffer();                 // 第j次提交步長
                 for ( int j = 1 ; j <= 100000 ; j++) {                     // 構建SQL後綴
                     suffix.append( "('" + uutil.UUIDUtil.getUUID()+ "','" +i*j+ "','123456'" + ",'男'" + ",'教師'" + ",'www.bbk.com'" + ",'XX大學'" + ",'" + "2016-08-12 14:43:26" + "','備註'" + ")," );
                 }                 // 構建完整SQL
                 String sql = prefix + suffix.substring( 0 , suffix.length() - 1 );                 // 添加執行SQL
                 pst.addBatch(sql);                 // 執行操作
                 pst.executeBatch();                 // 提交事務
                 conn.commit();                 // 清空上一次添加的數據
                 suffix = new StringBuffer();
             }             // 頭等連接
             pst.close();
             conn.close();
         } catch (SQLException e) {
             e.printStackTrace();
         }         // 結束時間
         Long end = new Date().getTime();         // 耗時
         System.out.println( "1000萬條數據插入花費時間 : " + (end - begin) / 1000 + " s" );
         System.out.println( "插入完成" );
     }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章