• ORACLE导入大量数据的两种方式比较


     不管是开发还是测试,工作中经常需要去批量新增测试数据,但是大量数据的新增速度有时候让我们苦不堪言,下面通过两种方式完成oracle数据的批量新增,比较两种方式的效率。

    第一种方式:采用工具导入sql文件

    10w条数据为例,通过java程序生成insert语句,采用sqlplus进行导入

    1通过简单的JAVA程序生成sql脚本

    public class GenerateSQLFile {
        public static void main(String[] args) throws Exception {
          File file = new File("d:" + File.separator + "data.sql");  
          OutputStream out  = new FileOutputStream(file, true); 
          for (int i = 1; i <= 100000; i++) {
              String str = "insert into t_test(id,name) values(" + i
              + ",'hello world" + i + "');
    "; 
              byte b[] = str.getBytes(); 
              out.write(b); 
          }
          out.close();
        }
    }    

    执行程序,生成的sql脚本如下所示:

    insert into t_test_1(id,name) values(1,'hello world1');

    insert into t_test_1(id,name) values(2,'hello world2');

    insert into t_test_1(id,name) values(3,'hello world3');

    ... ...

    2新建表,导入数据

      以scott/tiger登录sqlplus;

      新建t_test表:create tablet_test_1(id int,name varchar2(255));

      创建表成功后执行:@D:data.sql,逐行插入数据

    3测试结果

      以sqlplus执行sql脚本导入10W条数据的时间大约是5分钟

    第二种方式:采用sql loader工具

    sql loader可以将文本格式存放的数据导入到oracle,是一个进行数据迁移的非常方便而且通用的工具。

    下面通过java程序生成csv文件,100万条测试数据为例,通过sql loader进行数据导入

    期望生成的csv数据文件格式如下:

    1,hello1

    2,hello2

    3,hello3

    ... ...

    1、生成数据文件

    100万条数据的csv文件同样由java程序生成:

      File file = new File("d:" + File.separator + "data.csv"); // 要操作的文件
        OutputStream out = null// 声明字节输出流
        out = new FileOutputStream(file, true); 
        for (int i = 1; i <= 100000; i++) {
            String str = i+","+"hello"+i+"
    ";
            byte b[] = str.getBytes(); 
            out.write(b); 
        }
    out.close(); 

     

    2创建表格t_test_2

      create table t_test_2(id varchar2(255),name varchar2(255));

    3建立控制文件test.ctl,脚本如下

    load data

    infile data.csv

    into table t_test_2     

    (

    id char terminated by ',',

    name char terminated by whitespace

    )

    参数说明: 

    Infile data.csv:  数据源文件 这里我们省略了默认的 discardfile   result.dsc   badfile   result.bad   
    into table t_test_2默认是INSERT,也可以into   table   resultxt   APPEND为追加方式,或REPLACE   
    terminated   by   ','指用逗号进行字段的分隔   
    terminated   by   whitespace 表示结尾以空白分隔  

    其它参数参考可使用命令d:>sqlldr   

    4导入数据:

    将test.ctl文件和data.csv文件放置在D盘根目录下,在命令行中运行:

       D:>sqlldr  userid=scott/tiger  control=test.ctl  

    5测试结果:通过sql loader进行数据导入,10万条数据毫秒级导入,100万条数据耗时10秒

  • 相关阅读:
    【2020省选Day1T1】LOJ3299 「联合省选 2020 A | B」冰火战士
    题解 CF1369 D,E,F Codeforces Round #652 (Div. 2)
    题解 LOJ3298 「BJOI2020」封印(SAM,数据结构)
    题解 nflsoj99 牛顿的烈焰激光剑(容斥,DP,数学)
    判断长度为3的等差数列(经典问题)
    树形图求和:一道经典矩阵知识题
    题解 LOJ2390 「JOISC 2017 Day 1」开荒者
    istio sidecar自动注入过程分析
    filebeat-kafka日志收集
    istio路由配置
  • 原文地址:https://www.cnblogs.com/happy521/p/9146177.html
Copyright © 2020-2023  润新知