绝对干货,教你4分钟插入1000万条数据到mysql数据库表,快快进来
我用到的數(shù)據(jù)庫(kù)為,mysql數(shù)據(jù)庫(kù)5.7版本的
1.首先自己準(zhǔn)備好數(shù)據(jù)庫(kù)表
其實(shí)我在插入1000萬(wàn)條數(shù)據(jù)的時(shí)候遇到了一些問(wèn)題,現(xiàn)在先來(lái)解決他們,一開(kāi)始我插入100萬(wàn)條數(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è)配置沒(méi)配置足夠大,因?yàn)槟J(rèn)的為4M的,后來(lái)我調(diào)為100M就沒(méi)報(bào)錯(cuò)了
set global max_allowed_packet = 100*1024*1024*記住,設(shè)置好后重新登錄數(shù)據(jù)庫(kù)才能看的設(shè)置后的值show VARIABLES like '%max_allowed_packet%' Java的jdbc代碼如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 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);//指定連接類(lèi)型? ????????conn = DriverManager.getConnection(url, user, password);//獲取連接? ????????if (conn!=null) { ????????????System.out.println("獲取連接成功"); ????????????insert(conn); ????????}else { ????????????System.out.println("獲取連接失敗"); ????????} ????} ????public static void insert(Connection conn) { ????????// 開(kāi)始時(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í)行語(yǔ)句 ????????????// 外層循環(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萬(wàn)條數(shù)據(jù)插入花費(fèi)時(shí)間 : " + (end - begin) / 1000 + " s"); ????????System.out.println("插入完成"); ????} } |
結(jié)果:
哈哈,1000萬(wàn)條數(shù)據(jù)288秒完成,是不是感覺(jué)超級(jí)牛逼,我每次插入10萬(wàn)條數(shù)據(jù)就提交一次事務(wù),如果是一條一條差的話估計(jì)要好幾個(gè)小時(shí),網(wǎng)上有人試過(guò)時(shí)間浪費(fèi)在數(shù)據(jù)庫(kù)的連接上了,后來(lái)我想測(cè)試插入100萬(wàn)條數(shù)據(jù)時(shí)候數(shù)據(jù)庫(kù)引擎的差別
首先把代碼中最外層的循環(huán)i=100改為i=10;
1.數(shù)據(jù)庫(kù)引擎為MyISAM時(shí):27s,這個(gè)我忘了截圖了。因?yàn)槲液髞?lái)測(cè)試InnoDB的時(shí)候我換了一個(gè)表來(lái)測(cè)試,因?yàn)槲野l(fā)現(xiàn)改不了數(shù)據(jù)庫(kù)表的引擎。。。。所以只能換一個(gè)表來(lái)測(cè)試
1.數(shù)據(jù)庫(kù)引擎為InnoDB時(shí):
?用了77秒,比MyISAW慢了3倍左右,估計(jì)要是1000萬(wàn)條數(shù)據(jù)
轉(zhuǎn)載于:https://www.cnblogs.com/lcword/p/9562553.html
總結(jié)
以上是生活随笔為你收集整理的绝对干货,教你4分钟插入1000万条数据到mysql数据库表,快快进来的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 操作系统文件加密
- 下一篇: Python 爬虫实现天气查询(可视化界