zoukankan      html  css  js  c++  java
  • [MyBatis]再次向MySql一张表插入一千万条数据 批量插入 用时5m24s

    本例代码下载:https://files.cnblogs.com/files/xiandedanteng/InsertMillionComparison20191012.rar

    环境依然和原来一样。

    代码稍改了改:

    package com.hy.action;
    
    import java.io.Reader;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.List;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.apache.log4j.Logger;
    
    import com.hy.entity.Employee;
    import com.hy.mapper.EmpMapper;
    
    public class BatchInsert1001 {
    private static Logger logger = Logger.getLogger(SelectById.class);
        
        public static void main(String[] args) throws Exception{
            long startTime = System.currentTimeMillis();
            
            Reader reader=Resources.getResourceAsReader("mybatis-config.xml");
            
            SqlSessionFactory ssf=new SqlSessionFactoryBuilder().build(reader);
            reader.close();
            
            SqlSession session=ssf.openSession();
            
            try {
                EmpMapper mapper=session.getMapper(EmpMapper.class);
                String ctime="2017-11-01 00:00:01";
                int index=0;
                
                for(int i=0;i<10000;i++) {
                    List<Employee> emps=new ArrayList<Employee>();
    
                    for(int j=0;j<1000;j++) {
                        index++;
                        
                        Employee emp=new Employee("E"+String.valueOf(index),index % 100,ctime);
                        emps.add(emp);    
                        
                        ctime=timePastOneSecond(ctime);
                    }
                    
                    int changed=mapper.batchInsert(emps);
                    session.commit();
                    System.out.println("#"+i+" changed="+changed);
                    
                }
            }catch(Exception ex) {
                session.rollback();
                logger.error(ex);
            }finally {
                session.close();
                
                long endTime = System.currentTimeMillis();
                logger.info("Time elapsed:" + toDhmsStyle((endTime - startTime)/1000) + ".");
            }
        }
        
        public static String timePastOneSecond(String otime) {
            try {
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date dt=sdf.parse(otime);
                
                Calendar newTime = Calendar.getInstance();
                newTime.setTime(dt);
                newTime.add(Calendar.SECOND,1);
                
                Date dt1=newTime.getTime();
                String retval = sdf.format(dt1);
                
                return retval;
            }
            catch(Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
        
        // format seconds to day hour minute seconds style
        // Example 5000s will be formatted to 1h23m20s
        public static String toDhmsStyle(long allSeconds) {
            String DateTimes = null;
            
            long days = allSeconds / (60 * 60 * 24);
            long hours = (allSeconds % (60 * 60 * 24)) / (60 * 60);
            long minutes = (allSeconds % (60 * 60)) / 60;
            long seconds = allSeconds % 60;
            
            if (days > 0) {
                DateTimes = days + "d" + hours + "h" + minutes + "m" + seconds + "s";
            } else if (hours > 0) {
                DateTimes = hours + "h" + minutes + "m" + seconds + "s";
            } else if (minutes > 0) {
                DateTimes = minutes + "m" + seconds + "s";
            } else {
                DateTimes = seconds + "s";
            }
    
            return DateTimes;
        }
    }

    这把运行时间是5分24秒:

    #9988 changed=1000
    #9989 changed=1000
    #9990 changed=1000
    #9991 changed=1000
    #9992 changed=1000
    #9993 changed=1000
    #9994 changed=1000
    #9995 changed=1000
    #9996 changed=1000
    #9997 changed=1000
    #9998 changed=1000
    #9999 changed=1000
     INFO [main] - Time elapsed:5m24s.

    数据库结果都正常:

    接下来可以做个实验与MyBatis单条插对比一下。

  • 相关阅读:
    Red Hat Enterprise Linux 4 下安装 Oracle 10g [转]
    C# 轻松实现对窗体(Form)换肤[转]
    广州.NET俱乐部第一次活动安排暨动员书
    庆祝广州.net俱乐部成立暨迎接新成员暨讨论社区活动
    有感于广州.NET俱乐部的成立暨全体动员书
    如何在列表模板中增加一个计算列
    BI有感续
    BI有感
    列表属性查看器WebPart
    广州.NET第一次活动内容及演讲安排(本贴持续更新中)
  • 原文地址:https://www.cnblogs.com/heyang78/p/11663253.html
Copyright © 2011-2022 走看看