• 一 批量插入数据(使用批处理


     批量插入数据(使用批处理,存储过程,和不使用批处理)的性能对比

     

    1、 测试准备

    建表,建序列,建触发器,建视图

     

    使用的是Oracle数据库

    SQL语句

     

    --创建主人表

    -- id;主人编号

    -- name;主人的名字

    --password;密码

    --money;元宝

     

    create table master

    (

    id number(5) primary key not null,

    name varchar2(20),

    password varchar2(20),

    money  number(10)  

     

    )

    --创建序列

     

    create  sequence masterinsequ

    increment by 1

    start with 10000

    nomaxvalue

    nocycle

    cache 10;

     

    --插入测试数据

    insert into master(id,name,password,money) values(masterinsequ.nextval,'pengjia1','123123',0);

    insert into master(id,name,password,money) values(masterinsequ.nextval,'pengjia','123123',0);

     

    --查看数据

    select id,name,password,money from master

     

    select * from master

     

    /*

    id;神兽的编号

    masterId;所属的世界之主编号

    name;神兽的昵称

    health;健康值

    lover;亲密度

    type;类型

    grade;// 神兽青龙所特有的

    */

    ?

    drop table Dragon;

    create table dragon(

    id number(5) primary key not null,

    masterid number(5) ,

    constraint fk_masterid_id foreign key (masterid) references master(id),

    --创建外键约束

    --masterid number(5) references master(id)

    name varchar2(10) ,

    health number(3),

    lover number(3),

    type varchar2(10),

    grade varchar(10)

    )

     

    select * from dragon

     

    insert into(id,masterid,name,health,lover,type,grade) values()

     

    --查询表

    select * from dragon

     

    --删除表

    drop table   dragon

    select * from dragon

     

    insert into dragon (id,masterid,name,health,lover,type,grade)

    values(10002,10002,'sdfgiu',100,0,'青龙','一爪青龙')

     

    --主键没有自动增长

    --创建序列和触发器

     

    create  sequence sq_dragon_id

    increment by 1

    start with 1

    nomaxvalue 

    nocycle

    nocache;

     

    create or replace trigger tr_dragon_insert

    before insert on dragon for each row

    begin

    select sq_dragon_id.nextval into :new.id from dual;

    end;

     

    insert into dragon (masterid,name,health,lover,type,grade)

    values(10002,'sdfgiu',100,0,'青龙','一爪青龙')

     

    delete from dragon where id in(10001,10002)

    --查询记录条数

    select count(*) from dragon

     

    --清空数据

    truncate table dragon

     

     

    --创建存储过程

    create or replace procedure pro_insert_dragon

    is

    i number(8,0) :=0;  --定义变量不需要declare

    begin

      loop

        i:=i+1;

        insert into dragon (masterid,name,health,lover,type,grade)values(10002,'青青',100,0,'青龙','九爪青龙');

        exit when i>10000;

      end loop;

      commit;

    end;

     

     

    begin 

    pro_insert_dragon;

    end;

     

    PRO_INSERT_DRAGON

     

    2、 测试代码

     

    关键代码

    @Test

    public void test3() throws Exception{

    long startTime=System.currentTimeMillis();

    //测试使用批处理插入一万条数据的性能

    Connection conn=BaseDao.getConn();

    String sql="insert into dragon (masterid,name,health,lover,type,grade)"

    + "values(?,?,?,?,?,?)";

    PreparedStatement pstmt = conn.prepareStatement(sql);

    for(int i=0;i<10000;i++) {

    Object[] params= {10002,"青青"+i,100,0,"青龙","九爪青龙"};

    BaseDao.setParams(pstmt, params);

    pstmt.addBatch();

    if(i%1000==0) {

    pstmt.executeBatch();

    pstmt.clearBatch();

    }

    }

    pstmt.executeBatch();

    pstmt.clearBatch();

    BaseDao.closeAll(conn, pstmt);

     

    long endTime=System.currentTimeMillis();

    System.out.println("使用Batch批量插入所花费的时间(ms):"+(endTime-startTime));

    }

     

     

    @Test

    public void test4() throws Exception{

    long startTime=System.currentTimeMillis();

    //测试使用批处理插入一万条数据的性能

    Connection conn=BaseDao.getConn();

    String sql="insert into dragon (masterid,name,health,lover,type,grade)"

    + "values(?,?,?,?,?,?)";

    PreparedStatement pstmt = conn.prepareStatement(sql);

    for(int i=0;i<10000;i++) {

    Object[] params= {10002,"青青"+i,100,0,"青龙","九爪青龙"};

    BaseDao.setParams(pstmt, params);

    pstmt.executeUpdate();

    }

     

    BaseDao.closeAll(conn, pstmt);

     

    long endTime=System.currentTimeMillis();

    System.out.println("不使用批量插入所花费的时间(ms):"+(endTime-startTime));

    }

     

     

    public void test5() throws Exception {

    // 测试使用存储过程

    long startTime = System.currentTimeMillis();

    String sql = "call PRO_INSERT_DRAGON()";

    //连接对象

    Connection conn = BaseDao.getConn();

    // 执行存储过程的对象

    CallableStatement cStmt = conn.prepareCall(sql);

    //执行存储过程

    cStmt.execute();

    //关闭资源

    BaseDao.closeAll(conn, cStmt);

    long endTime = System.currentTimeMillis();

    System.out.println("使用存储过程批量插入所花费的时间(ms):" + (endTime - startTime));

    }

     

    3、 测试结果

    使用Batch批量插入所花费的时间(ms)3014

    使用存储过程批量插入所花费的时间(ms)3073

    使用批量插入所花费的时间(ms) 17093

    使用batch和存储过程批量插入的效率在同一数量级,且比不使用批量插入的效率高很多

     

    <wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">





  • 相关阅读:
    动态规划(下):如何求得状态转移方程并进行编程实现?
    SQL注入
    动态规划(上):如何实现基于编辑距离的查询推荐?
    XSS
    组合
    访问控制
    排列
    基于离散对数的签名方案
    基于RSA的签名方案
    centos7基于Hadoop的集群安装配置Hive
  • 原文地址:https://www.cnblogs.com/myblogpengjia/p/9682506.html
Copyright © 2020-2023  润新知