批量插入测试数据

方式1

public class Main2 {
    public static void main(String[] args) {
        ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring.xml");
        SessionFactory sf = (SessionFactory)context.getBean("sessionFactory");
        Session session = sf.openSession();
        String sql = "insert into import_test(name, description, price, create_time) values(\"白羊座\", \"白羊座(Aries),黄道十二星座之第一宫,位于双鱼座之东,金牛座之西,出生日期为阳历3月21日~4月20日。白羊座就像小孩子一样,直率、热情、冲动,但也十分的自我为中心和孩子气。\", 100, \"2017-02-28\")";
        Date start = new Date();
        for(int i=0;i<10000;i++){
            session.createSQLQuery(sql).executeUpdate();
        }
        Date end = new Date();
        System.out.println(end.getTime()-start.getTime());
    }
}

测出来是358066ms,差不多是6分钟。

 

方式2

运行SQL文件。记住选中SET AUTOCOMMIT=0

测出来的时间是0.888s。

一百万条数据用了118.811s。

我觉得第二种是比较快的了,就是写在sql语句,用分号隔开。如果有更快的方式,望分享。

posted on 2017-02-28 23:11  angelshelter  阅读(243)  评论(0编辑  收藏  举报

导航