MYSQL BULK INSERT OPTIMIZATION &&CHINESE GARBLED CODE

because the reading efficiency of type longtext in mysql is not charming,I have to turn to another solution ,which is split the coordinates data and store them in slave table.but there’s another problem ,each coordinate store as one data,there will be ten million data,here’s the code before optimization…

    conn = new DBConnection().getConn();
    pst = conn.prepareStatement(stringBuffer.toString());
    pst.setTimestamp(++paramCount, timestamp);
    pst.addBatch();
    int[] result = pst.executeBatch();

each loop there will be 2MB data,which is nearly 50 thousand data, to be inserted,the speed is not charming either…nearly 6 minute…it’s painful waiting for the file to be parsed.

then I looked up on Internet,there’s lot of way to improve…
1.TokuDB I found this first,but it’s not very suitable for me.though the insertion will be faster,but it seems means the compromising on read speed.
http://blog.jobbole.com/109104/
2.then I found these blog,it’s very helpful…
first there’s some varibles of mysql you need to setup,here’s what I set:
innodb_flush_log_at_trx_commit =0
bulk_insert_buffer_size = 128M
Max_allowed_packet = 64M
if my.ini does’t contain these varibles ,just add them in the end will be ok.
after setting and restarted the mysql service.
you need change your code…

StringBuilder sb = new StringBuilder();
    sb.append(SQL_REPLACE_MAP_INFO_CB);
    for (int i = 0; i < coordinates.size(); i++) {
        try {
            Map<String, Object> coordinate = coordinates.get(i);
            sb.append("('" + coordinate.get("fk_id").toString() + "',"
                    + Integer.parseInt(coordinate.get("seq").toString()) + ","
                    + BigDecimal.valueOf(Double.parseDouble(coordinate.get("lat").toString())) + ","
                    + BigDecimal.valueOf(Double.parseDouble(coordinate.get("lng").toString())) + ","
                    + BigDecimal.valueOf(Double.parseDouble(coordinate.get("zh").toString())) + ","
                    + BigDecimal.valueOf(Double.parseDouble(coordinate.get("relativeDis").toString())) + ",'"
                    + timestamp + "')");
            if (i != coordinates.size() - 1) {
                sb.append(",");
            }
        } catch (Exception e) {
            e.printStackTrace();
            continue;
        }
    }
    pst = conn.prepareStatement(sb.toString());
    int result = pst.executeUpdate();

the difference is using insert into table () values (),(),(),(),()…()
instead of insert into table () values (?,?,?).
after optimization,same 2MB data ,I only need few seconds!!!
https://blog.csdn.net/u013488847/article/details/53819976
https://blog.csdn.net/u011277123/article/details/61914773

then I ran into chinese grabled code problem…then I checked code…

		@RequestMapping(value = "/mapAnalyse.json", produces = "text/html;charset=UTF-8")
		InputStreamReader reader = new InputStreamReader(multipartFile.getInputStream(), "UTF-8");

then I realized there’s one thing I haven’t check,that is the data resource…
when I get the request from jsp.I need to make sure jsp was encoded in UTF-8,in the same way,when I read data from a file,I need to make sure that the file was coded in UTF-8…then I found the file I uploaded is coded in ANSI…after I saved it in UTF-8 ,the problem solved…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值