数据库操作如:插入操作,批处理与循环逐个插入性能比较

来源:互联网 发布:软件设计师教程 编辑:程序博客网 时间:2024/05/29 16:07

    数据库操作,批处理与单条处理性能比较:

 以dbutils工具为例,数据库为oracle数据库,采用c3p0框架。

 实验设计一张表,字段为12个字段,一条语句插入一个字段(发现插入一个字段与多个字段时间花费差不多)。

    实验代码如下。

        String sql = "insert into t_audit_msg (appkey) values (?)";        Long ts = System.currentTimeMillis();        Object[][] objs = new Object[1000][1];        for(int i=0;i<1000;i++) {            objs[i][0]="qwe";        }        try {            runner.batch(sql,objs);            System.out.println("batch 1000 insert spend: "+(System.currentTimeMillis()-ts)+"ms");            ts = System.currentTimeMillis();            for(int i=0;i<1000;i++) {                runner.update(sql,"qwe");            }        } catch (SQLException e) {            e.printStackTrace();        }        System.out.println("1000 insert spend: "+(System.currentTimeMillis()-ts)+"ms");

    实验结果如下:


    由此可见,批处理的执行速度远超单条逐个处理。

    在代码中,对于可批处理的语句,应尽可能采用批处理的手段。


阅读全文
0 0