濮阳杆衣贸易有限公司

主頁 > 知識庫 > 88秒插入1000萬條數(shù)據(jù)到MySQL數(shù)據(jù)庫表的操作方法

88秒插入1000萬條數(shù)據(jù)到MySQL數(shù)據(jù)庫表的操作方法

熱門標(biāo)簽:高德地圖標(biāo)注模式 湘潭電銷機(jī)器人咨詢電話 電銷機(jī)器人針對的 高德地圖標(biāo)注中心個人注冊 外呼系統(tǒng)防封號違法嗎 寶應(yīng)電信400電話辦理費用 如何在高德地圖標(biāo)注新地址 外呼系統(tǒng)服務(wù) 400電話辦理都選易號網(wǎng)

我用到的數(shù)據(jù)庫為,mysql數(shù)據(jù)庫5.7版本的

首先自己準(zhǔn)備好數(shù)據(jù)庫表

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

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.

出現(xiàn)上面的錯誤是因為數(shù)據(jù)庫表的 max_allowed_packet 這個配置沒配置足夠大,因為默認(rèn)的為4M的,后來我調(diào)為100M就沒報錯了

set global max_allowed_packet = 100*1024*1024* 

記住,設(shè)置好后重新登錄數(shù)據(jù)庫才能看的設(shè)置后的值

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();
       // 設(shè)置事務(wù)為非自動提交
       conn.setAutoCommit( false );
       // 比起st,pst會更好些
       PreparedStatement pst = (PreparedStatement) conn.prepareStatement( "" ); //準(zhǔn)備執(zhí)行語句
       // 外層循環(huán),總提交事務(wù)次數(shù)
       for ( int i = 1 ; i = 100 ; i++) {
         suffix = new StringBuffer();
         // 第j次提交步長
         for ( int j = 1 ; j = 100000 ; j++) {
           // 構(gòu)建SQL后綴
           suffix.append( "('" + uutil.UUIDUtil.getUUID()+ "','" +i*j+ "','123456'" + ",'男'" + ",'教師'" + ",'www.bbk.com'" + ",'XX大學(xué)'" + ",'" + "2016-08-12 14:43:26" + "','備注'" + ")," );
         }
         // 構(gòu)建完整SQL
         String sql = prefix + suffix.substring( 0 , suffix.length() - 1 );
         // 添加執(zhí)行SQL
         pst.addBatch(sql);
         // 執(zhí)行操作
         pst.executeBatch();
         // 提交事務(wù)
         conn.commit();
         // 清空上一次添加的數(shù)據(jù)
         suffix = new StringBuffer();
       }
       // 頭等連接
       pst.close();
       conn.close();
     } catch (SQLException e) {
       e.printStackTrace();
     }
     // 結(jié)束時間
     Long end = new Date().getTime();
     // 耗時
     System.out.println( "1000萬條數(shù)據(jù)插入花費時間 : " + (end - begin) / 1000 + " s" );
     System.out.println( "插入完成" );
   }
}

總結(jié)

以上所述是小編給大家介紹的88秒插入1000萬條數(shù)據(jù)到MySQL數(shù)據(jù)庫表的操作方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

您可能感興趣的文章:
  • MySql 快速插入千萬級大數(shù)據(jù)的方法示例
  • java中JDBC實現(xiàn)往MySQL插入百萬級數(shù)據(jù)的實例代碼
  • mysql中迅速插入百萬條測試數(shù)據(jù)的方法
  • 教你如何6秒鐘往MySQL插入100萬條數(shù)據(jù)的實現(xiàn)

標(biāo)簽:蘭州 馬鞍山 南充 賀州 佛山 宿遷 黔南 黃山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《88秒插入1000萬條數(shù)據(jù)到MySQL數(shù)據(jù)庫表的操作方法》,本文關(guān)鍵詞  88秒,插入,1000,萬條,數(shù)據(jù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《88秒插入1000萬條數(shù)據(jù)到MySQL數(shù)據(jù)庫表的操作方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于88秒插入1000萬條數(shù)據(jù)到MySQL數(shù)據(jù)庫表的操作方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    双牌县| 张北县| 清徐县| 健康| 高清| 东丽区| 金溪县| 霍林郭勒市| 通州市| 商都县| 绥化市| 太仆寺旗| 阿瓦提县| 丹阳市| 武宁县| 武川县| 烟台市| 阳春市| 澎湖县| 韩城市| 衡阳市| 桓台县| 资兴市| 磐石市| 昌乐县| 陵水| 板桥市| 台南县| 图们市| 方城县| 永昌县| 灵宝市| 芮城县| 陈巴尔虎旗| 古交市| 乌拉特前旗| 永泰县| 商城县| 怀安县| 陕西省| 彰武县|