1. 采用sqlplus导入*.sql文件
(以10W条数据测试)
1.1. 生成sql脚本
写个简单的JAVA程序如下:
public class GenerateSQLFile {
public static void main(String[] args) throws Exception {
// 要操作的文件
File file = new File("d:" + File.separator + "demo.sql"); OutputStream out = null; // 声明字节输出流
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 + "');\r\n"; // 要输出的信息
byte b[] = str.getBytes(); // 将String变为byte数组
out.write(b); // 写入数据
}
out.close(); // 关闭
}
}
执行程序,生成的sql脚本如下所示:
insert into t_test(id,name) values(1,'hello world1');
insert into t_test(id,name) values(2,'hello world2');
insert into t_test(id,name) values(3,'hello world3');
... ...
1.2. 新建表,导入数据
以scott/tiger登录sqlplus;
新建t_test表:create table t_test(id int,name varchar2(255));
创建成功后执行:@D:\demo.sql,逐行插入数据。
1.3. 测试结果
以sqlplus执行sql脚本导入10W条数据的时间大约是5分钟。
2. 采用sql loader 工具导入
(以100W条测试数据)
2.1. 新建test.csv数据文件
格式如下:
1,hello1
2,hello2
3,hello3
... ...
十万条数据的csv文件同样由java小程序生成:
File file = new File("d:" + File.separator + "demo.txt"); // 要操作的文件
OutputStream out = null; // 声明字节输出流
out = new FileOutputStream(file, true); // 通过子类实例化
for (int i = 1; i <= 100000; i++) {
String str = i+","+"hello"+i+"\r\n"; // 要输出的信息
byte b[] = str.getBytes(); // 将String变为byte数组
out.write(b); // 写入数据
}
out.close(); // 关闭
2.2. 建立表格
create table test(id varchar2(255),name varchar2(255));
2.3. 建立控制文件test.ctl
load data
infile demo.csv
into table test
(
id char terminated by ',',
name char terminated by whitespace
)
PS: infile 指数据源文件 这里我们省略了默认的 discardfile result.dsc badfile result.bad
into table test 默认是INSERT,也可以into table resultxt APPEND为追加方式,或REPLACE
terminated by ',' 指用逗号分隔
terminated by whitespace 结尾以空白分隔
其它参数参考:d:\>sqlldr
2.4. 导入数据
将ctl文件和csv文件放置在D盘根目录下,命令行中运行:
D:\>sqlldr userid=scott/tiger control=test.ctl
2.5. 测试结果
急速,10W条数据转眼导入结束,100W条数据耗时10s。
<!--EndFragment-->