设为首页 加入收藏

TOP

MySQL大量数据入库的性能比较(二)
2015-12-01 16:03:09 来源: 作者: 【 】 浏览:30
Tags:MySQL 大量 数据 入库 性能 比较
on.prepareStatement(SQL);?
75.? ? ? ? int i = 0;?
76.? ? ? ? while (i < MAX) {?
77.? ? ? ? ? ? pt.setLong(1, 1 + (int) (Math.random() * 100000000));?
78.? ? ? ? ? ? pt.setLong(2, 1 + (int) (Math.random() * 100000000));?
79.? ? ? ? ? ? pt.setString(3, RandomStringUtils.randomAscii(200));?
80.? ? ? ? ? ? pt.setInt(4, 1);?
81.? ? ? ? ? ? pt.setInt(5, 1);?
82.? ? ? ? ? ? pt.setInt(6, 1);?
83.? ? ? ? ? ? pt.setInt(7, 1);?
84.? ? ? ? ? ? pt.addBatch();?
85.? ? ? ? ? ? if (i % batchSize == 1) {?
86.? ? ? ? ? ? ? ? pt.executeBatch();?
87.? ? ? ? ? ? ? ? con.commit();?
88.? ? ? ? ? ? }?
89.? ? ? ? ? ? i++;?
90.? ? ? ? }?
91.? ? ? ? pt.executeBatch();?
92.? ? ? ? con.commit();?
93.? ? ? ? con.close();?
94.? ? }?
95.? ?
96.? ? private static void testLoadFile(int batchSize)?
97.? ? ? ? ? ? throws ClassNotFoundException, SQLException, UnsupportedEncodingException {?
98.? ? ? ? String fieldsterminated = "\t\t";?
99.? ? ? ? String linesterminated = "\t\r\n";?
100.? ? ? ? String loadDataSql = "LOAD DATA LOCAL INFILE 'sql.csv' INTO TABLE chat_message FIELDS TERMINATED BY '"?
101.? ? ? ? ? ? ? ? + fieldsterminated + "'? LINES TERMINATED BY '" + linesterminated?
102.? ? ? ? ? ? ? ? + "' (src_userid,target_userid,message,s1,s2,s3,s4) ";?
103.? ? ? ? Connection con = getConnection();?
104.? ? ? ? con.setAutoCommit(false);?
105.? ? ? ? PreparedStatement pt = con.prepareStatement(loadDataSql);?
106.? ? ? ? com.mysql.jdbc.PreparedStatement mysqlStatement = null;?
107.? ? ? ? if (pt.isWrapperFor(com.mysql.jdbc.Statement.class)) {?
108.? ? ? ? ? ? mysqlStatement = pt.unwrap(com.mysql.jdbc.PreparedStatement.class);?
109.? ? ? ? }?
110.? ?
111.? ? ? ? int i = 0;?
112.? ? ? ? StringBuilder sb = new StringBuilder(10000);?
113.? ? ? ? while (i < MAX) {?
114.? ? ? ? ? ? sb.append(1 + (int) (Math.random() * 100000000));?
115.? ? ? ? ? ? sb.append(fieldsterminated);?
116.? ? ? ? ? ? sb.append(1 + (int) (Math.random() * 100000000));?
117.? ? ? ? ? ? sb.append(fieldsterminated);?
118.? ? ? ? ? ? sb.append(RandomStringUtils.randomAscii(200).replaceAll("\\\\", " "));?
119.? ? ? ? ? ? sb.append(fieldsterminated);?
120.? ? ? ? ? ? sb.append(1);?
121.? ? ? ? ? ? sb.append(fieldsterminated);?
122.? ? ? ? ? ? sb.append(1);?
123.? ? ? ? ? ? sb.append(fieldsterminated);?
124.? ? ? ? ? ? sb.append(1);?
125.? ? ? ? ? ? sb.append(fieldsterminated);?
126.? ? ? ? ? ? sb.append(1);?
127.? ? ? ? ? ? sb.append(linesterminated);?
128.? ? ? ? ? ? if (i % batchSize == 1) {?
129.? ? ? ? ? ? ? ? byte[] bytes = sb.toString().getBytes();?
130.? ? ? ? ? ? ? ? InputStream in = new ByteArrayInputStream(bytes);?
131.? ? ? ? ? ? ? ? mysqlStatement.setLocalInfileInputStream(in);?
132.? ? ? ? ? ? ? ? mysqlStatement.executeUpdate();?
133.? ? ? ? ? ? ? ? con.commit();?
134.? ? ? ? ? ? ? ? sb = new StringBuilder(10000);?
135.? ? ? ? ? ? }?
136.? ?
137.? ? ? ? ? ? i++;?
138.? ? ? ? }?
139.? ? ? ? byte[] bytes = sb.toString().getBytes();?
140.? ? ? ? InputStream in = new ByteArrayInputStream(bytes);?
141.? ? ? ? mysqlStatement.setLocalInfileInputStream(in);?
142.? ? ? ? mysqlStatement.executeUpdate();?
143.? ? ? ? con.commit();?
144.? ?
145.? ? ? ? con.close();?
146.? ? }?
147. }?


测试结果:


?


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇MySQL 5.6.23升级到MySQL 5.7.9 下一篇用命令过滤MySQL慢查询日志

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: