hibernate与jdbc在插入数据上的速度对比

测试数据库:ORACLE 11g
先测试一下,数据自己生成唯一ID的情况

domain:
@Entity
@Table(name = "test")
@GenericGenerator(name = "SEQ_Name", strategy = "sequence", parameters = {@Parameter(name = "sequence", value = "seq_test")})
public class Test {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_Name")
    @Column
    private Long id;


    @Column
    private int value;


    @Column
    private String type;


    public Test() {
    }


    public Test(int value) {
        this.value = value;
    }


    public Test(Long id, int value) {
        this.id = id;
        this.value = value;
    }


    public Test(int value, String type) {
        this.value = value;
        this.type = type;
    }


    public void setValue(int value) {
        this.value = value;
    }


    public int getValue() {
        return value;
    }


    public String getType() {
        return type;
    }


    public void setType(String type) {
        this.type = type;
    }
}

hibernate:
        long start = System.currentTimeMillis();
        for (int i = 0; i < DATA_COUNT; i++) {
            getHibernateTemplate().save(new Test(12345678, getType()));
        }
        System.out.println("saveBatch\t\t" + (System.currentTimeMillis() - start));

jdbc:
		PreparedStatement statement = connection.prepareStatement("insert into test (type, value) values (?, ?)");
                for (int i = 0; i < DATA_COUNT; i++) {
                    statement.setString(1, getType());
                    statement.setInt(2, 12345678);
                    statement.addBatch();
                }
                statement.executeBatch();

测试1000条数据在两种方式的运行时间(毫秒):
hibernate 1500
jdbc 20

从上图可以看出,速度相差75倍。

结论:
当使用数据库生成ID时,JDBC在数据插入方面较HIBERNATE有较大优势。




接下来测试一下,自己生成唯一ID的情况
domain:
@Entity
@Table(name = "test2")
public class Test2 {
    @Id
    @Column(name = "id")
    private Long id;


    @Column
    private int value;


    @Column
    private String type;


    public Test2(long id, int value, String type) {
        this.id = id;
        this.value = value;
        this.type = type;
    }


    public void setValue(int value) {
        this.value = value;
    }


    public int getValue() {
        return value;
    }


    public String getType() {
        return type;
    }


    public void setType(String type) {
        this.type = type;
    }


}

hibernate:
        long start = System.currentTimeMillis();
        for (int i = 0; i < DATA_COUNT; i++) {
            getHibernateTemplate().save(new Test2(IdGenerator.getId(), 12345678, getType()));
        }
        System.out.println("saveBatch2\t\t" + (System.currentTimeMillis() - start));

jdbc:

                PreparedStatement statement = connection.prepareStatement("insert into test2 (type, value, id) values (?, ?, ?)");
                for (int i = 0; i < DATA_COUNT; i++) {
                    statement.setString(1, getType());
                    statement.setInt(2, 12345678);
                    statement.setLong(3, IdGenerator.getId());
                    statement.addBatch();
                }
                statement.executeBatch();

测试1000条数据在两种方式的运行时间(毫秒):
hibernate 65
jdbc 25


从上图可以看出,速度相差3倍。


结论:
当使用自己生成ID时,JDBC在数据插入方面较HIBERNATE的优势并不是很明显。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值