88秒插入1000萬條數(shù)據(jù)到MySQL數(shù)據(jù)庫(kù)表的操作方法
我用到的數(shù)據(jù)庫(kù)為,mysql數(shù)據(jù)庫(kù)5.7版本的
首先自己準(zhǔn)備好數(shù)據(jù)庫(kù)表
其實(shí)我在插入1000萬條數(shù)據(jù)的時(shí)候遇到了一些問題,現(xiàn)在先來解決他們,一開始我插入100萬條數(shù)據(jù)時(shí)候報(bào)錯(cuò),控制臺(tái)的信息如下:
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)上面的錯(cuò)誤是因?yàn)閿?shù)據(jù)庫(kù)表的 max_allowed_packet 這個(gè)配置沒配置足夠大,因?yàn)槟J(rèn)的為4M的,后來我調(diào)為100M就沒報(bào)錯(cuò)了
set global max_allowed_packet = 100*1024*1024*
記住,設(shè)置好后重新登錄數(shù)據(jù)庫(kù)才能看的設(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) {
// 開始時(shí)間
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ù)為非自動(dòng)提交
conn.setAutoCommit( false );
// 比起st,pst會(huì)更好些
PreparedStatement pst = (PreparedStatement) conn.prepareStatement( "" ); //準(zhǔn)備執(zhí)行語句
// 外層循環(huán),總提交事務(wù)次數(shù)
for ( int i = 1 ; i <= 100 ; i++) {
suffix = new StringBuffer();
// 第j次提交步長(zhǎng)
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é)束時(shí)間
Long end = new Date().getTime();
// 耗時(shí)
System.out.println( "1000萬條數(shù)據(jù)插入花費(fèi)時(shí)間 : " + (end - begin) / 1000 + " s" );
System.out.println( "插入完成" );
}
}
總結(jié)
以上所述是小編給大家介紹的88秒插入1000萬條數(shù)據(jù)到MySQL數(shù)據(jù)庫(kù)表的操作方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
SELECT INTO 和 INSERT INTO SELECT 兩種表復(fù)制語句簡(jiǎn)單介紹
Insert是T-sql中常用語句,Insert INTO table(field1,field2,...) values(value1,value2,...)這種形式的在應(yīng)用程序開發(fā)中必不可少2012-11-11
MySQL生產(chǎn)庫(kù)Insert了2次同樣的記錄但是主鍵ID是不一樣的問題的分析過程
這篇文章主要介紹了MySQL生產(chǎn)庫(kù)Insert了2次同樣的記錄但是主鍵ID是不一樣的問題的分析過程,需要的朋友可以參考下2014-02-02
MySQL數(shù)據(jù)庫(kù)壓縮版本安裝與配置詳細(xì)教程
今天教各位小伙伴怎么安裝及配置Mysql數(shù)據(jù)庫(kù),文中有非常詳細(xì)的圖文解說及代碼示例,對(duì)剛?cè)腴Tmysql的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
Windows下mysql 8.0.12 安裝詳細(xì)教程
這篇文章主要為大家詳細(xì)介紹了Windows下mysql 8.0.12 安裝詳細(xì)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Mysql:The user specified as a definer (''xxx@''%'') does not
今天小編就為大家分享一篇關(guān)于Mysql:The user specified as a definer ('xxx@'%') does not exist的解決方案,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12

