美文网首页
数据库插入百万级数据

数据库插入百万级数据

作者: 1218fb85bdd6 | 来源:发表于2017-12-13 00:33 被阅读185次

    大家好,我是IT修真院北京分院第28期的学员曲艳行,一枚正直纯洁善良的java程序员

    今天给大家分享一下,修真院官网java任务1,深度思考中的知识点——spring IoC。

    1.背景介绍

    jdbc向mysql插入百万级数据,如果采用一般for循环插入数据,1w条大约8分钟,100w的话简直要了亲命。下面介绍事务提交和批量处理的方式,这两种方式可以大大提高效率,100W大概需要30秒。

    2.知识剖析

    1、jdbc的事务是默认自动提交,所以采用事务提交的方式需要关闭事务自动提交,之后手动提交。

    2、批量处理用到的addBatch()和executeBatch()方法对mysql的版本有要求,mysql 5.1.13及以上的版本。

    3、addBatch()是提交缓存,然后再由executeBatch()提交到数据库。

    3.常见问题

    采用批量处理的方法后没有达到应有的效果。

    4.解决方案

    1、在url的参数需要设置开启批量提交。

    String url ="jdbc:mysql://localhost:3306/test?useSSL=false&rewriteBatchedStatements=true";

    2、mysql的版本问题。

    5.编码实战

    importorg.junit.Before;

    importorg.junit.Test;

    importjava.sql.Connection;

    importjava.sql.DriverManager;

    importjava.sql.PreparedStatement;

    importjava.sql.SQLException;

    importjava.util.Date;

    importjava.util.Random;

    public classTest1 {

    public static voidtest(){

    String url ="jdbc:mysql://localhost:3306/test?useSSL=false&rewriteBatchedStatements=true";

    //        String url = "jdbc:mysql://localhost:3306/test?useSSL=false";

    String user ="root";

    String password ="123456";

    Connection conn=null;

    PreparedStatement ps=null;

    //      1、 注册驱动

    try{

    Class.forName("com.mysql.jdbc.Driver");

    //      2、获取连接

    conn = DriverManager.getConnection(url,user,password);

    //      3、定义sql语句

    String sql ="insert into student (name,age) values (?,?)";

    //      4、预编译

    ps = conn.prepareStatement(sql);

    Random random=newRandom();

    intstuAge;

    //      5、赋值

    //            记录开始时间

    //            long beginTime = new Date().getTime();

    longbeginTime= System.currentTimeMillis();

    //    -------------------------------------------------------

    conn.setAutoCommit(false);

    for(inti =0;i<1000;i++){

    stuAge = random.nextInt(10);

    ps.setString(1,"张三");

    ps.setInt(2,stuAge);

    //                插入

    //                ps.executeUpdate();

    //                批量插入

    ps.addBatch();

    }

    //            批量插入

    ps.executeBatch();

    //  ------------------------------------------------

    conn.commit();

    longendTime= System.currentTimeMillis();

    //            整个过程用时

    System.out.println("用时:"+(endTime-beginTime));

    }catch(Exception e) {

    e.printStackTrace();

    }finally{

    try{

    if(ps !=null){

    ps.close();

    }

    }catch(SQLException e) {

    e.printStackTrace();

    }

    try{

    if(conn !=null){

    conn.close();

    }

    }catch(SQLException e) {

    e.printStackTrace();

    }

    }

    }

    @Test

    public voidtest2(){

    for(inti =0;i<1;i++){

    Test1.test();

    }

    }

    }

    6.扩展思考

    为什么第一次连接mysql的时间会很久?

    7.参考文献

    https://www.cnblogs.com/fnz0/p/5713102.html

    https://zhidao.baidu.com/question/1765893556507822580.html

    8.更多讨论

    Q1:插入的数据是随机的还是相同的?

    答:插入的数据:name和age,其中age是10以内的随机数。

    9.鸣谢

    视频:https://v.qq.com/x/page/n05217ym13f.html

    PPT:https://ptteng.github.io/PPT/PPT-java/task1数据库插入百万级数据.html#/

    结束语:

    今天的分享就到这里啦,欢迎大家点赞、转发、留言、拍砖~

    相关文章

      网友评论

          本文标题:数据库插入百万级数据

          本文链接:https://www.haomeiwen.com/subject/suuoixtx.html