本文接Oracle SQL*Loader 使用指南(第一部分)
5, 设置行结束标志 (STR) , 在导入大对象时很有用!
LOAD DATA
INFILE 'c:\data\mydata.csv' "str '*****'"
REPLACE INTO TABLE book
fields terminated by "," optionally enclosed by '"'
(
book_title ,
book_price ,
book_pages ,
book_id
)
Mydata.csv 文件内容:
Oracle Essentials bbbbbbbbbbbbb
,3495,355,1 *****
SQL*Plus: The Definitive
Guide ,3995,502,2 *****
Oracle PL/SQL
Programming ,4495,87,3 *****
Oracle8 Design Tips ,1495,115,4 *****
例子:
SQL> truncate table book;
表被截断。
SQL> host sqlldr hr/hr control =c:\data\loader.ctl log=c:\data\log.log
SQL*Loader: Release 10.2.0.1.0 - Production on 星期四 8月 14 10:16:49 2008
Copyright (c) 1982, 2005, Oracle. All rights reserved.
达到提交点 - 逻辑记录计数 4
SQL> select * from book;
BOOK_ID BOOK_TITLE BOOK_PRICE BOOK_PAGES
---------- ----------------------------------- ---------- ----------
1 Oracle Essentials bbbbbbbbbbbbb 3495 355
2 SQL*Plus: The Definitive 3995 502
3 Oracle PL/SQL 4495 87
4 Oracle8 Design Tips 1495 115
6, 将多个数据flat 文件同时load 进数据库
LOAD DATA
INFILE 'c:\data\mydata.csv' "str '*****'"
INFILE 'c:\data\mydata2.csv' "str '*****'"
REPLACE INTO TABLE book
fields terminated by "," optionally enclosed by '"'
(
book_title ,
book_price ,
book_pages ,
book_id
)
Mydata.csv 数据内容如下:
Oracle Essentials bbbbbbbbbbbbb
,3495,355,1 *****
SQL*Plus: The Definitive
Guide ,3995,502,2 *****
Oracle PL/SQL
Programming ,4495,87,3 *****
Oracle8 Design Tips ,1495,115,4 *****
Mydata2.csv 数据内容如下:
新概念1
,3495,355,1 *****
新概念2
,3995,502,2 *****
新概念3
,4495,87,3 *****
新概念4 ,1495,115,4 *****
例子:
SQL> truncate table book;
表被截断。
SQL> host sqlldr hr/hr control =c:\data\loader.ctl log=c:\data\log.log
SQL*Loader: Release 10.2.0.1.0 - Production on 星期四 8月 14 10:32:55 2008
Copyright (c) 1982, 2005, Oracle. All rights reserved.
达到提交点 - 逻辑记录计数 4
达到提交点 - 逻辑记录计数 8
SQL> select * from book;
BOOK_ID BOOK_TITLE BOOK_PRICE BOOK_PAGES
---------- ----------------------------------- ---------- ----------
1 Oracle Essentials bbbbbbbbbbbbb 3495 355
2 SQL*Plus: The Definitive 3995 502
3 Oracle PL/SQL 4495 87
4 Oracle8 Design Tips 1495 115
1 新概念1 3495 355
2 新概念2 3995 502
3 新概念3 4495 87
4 新概念4 1495 115
已选择8行。
7, 如果文件名中包含有特殊字符,可以使用“\” 逃逸字符
如以下例子
INFILE 'mi_\'deci' 实际文件名为:mi_'deci
INFILE "mi_\"deci' 实际文件名为:mi_"deci
INFILE "mi_\\deci' 实际文件名为:mi_\deci
8,load 数据到表分区:
LOAD DATA
INFILE 'c:\data\mydata.csv' "str '*****'"
REPLACE INTO TABLE book PARTITION (p1)
fields terminated by "," optionally enclosed by '"'
(
book_title ,
book_price ,
book_pages ,
book_id
)
例子:
create table BOOK
(
BOOK_ID NUMBER,
BOOK_TITLE VARCHAR2(35),
BOOK_PRICE NUMBER,
BOOK_PAGES NUMBER
)
partition by range (book_id)(
partition p1 values less than (10),
partition p2 values less than (20))
SQL> host sqlldr hr/hr control =c:\data\loader.ctl log=c:\data\log.log
SQL*Loader: Release 10.2.0.1.0 - Production on 星期四 8月 14 11:01:13 2008
Copyright (c) 1982, 2005, Oracle. All rights reserved.
达到提交点 - 逻辑记录计数 4
SQL> select * from book partition (p1);
BOOK_ID BOOK_TITLE BOOK_PRICE BOOK_PAGES
---------- ----------------------------------- ---------- ----------
1 Oracle Essentials bbbbbbbbbbbbb 3495 355
2 SQL*Plus: The Definitive 3995 502
3 Oracle PL/SQL 4495 87
4 Oracle8 Design Tips 1495 115
SQL> select * from book partition (p2);
未选定行
SQL>
9, null if 空替代:
LOAD DATA
INFILE *
REPLACE INTO TABLE michigan_features
fields terminated by "," optionally enclosed by '"'
(
feature_name ,
short_feature_name ,
elevation
NULLIF elevation = ' 0',
feature_type
NULLIF feature_type = 'ppl',
county
)
begindata
MI ,Chatham,0,dl,Alger
MI ,Shingleton,821,al,Alger
MI, Rumely,0,ppl,Alger
MI ,Sundell,1049,,,
例子:
drop table michigan_features;
create table michigan_features(
feature_name varchar2(100),
short_feature_name varchar2(100),
elevation number,
feature_type varchar2(100),
county varchar2(100))
SQL> select feature_type from michigan_features;
FEATURE_TYPE
-----------------------------------------------------------
dl
al
SQL*Loader 数据的提交:
一般情况下是在导入数据文件数据后提交的。
也可以通过指定 ROWS= 参数来指定每次提交记录数。
提高 SQL*Loader 的性能:
1) 一个简单而容易忽略的问题是,没有对导入的表使用任何索引和/或约束(主键)。如果这样做,甚至在使用ROWS=参数时,会很明显降低数据库导入性能。
2) 可以添加 DIRECT=TRUE来提高导入数据的性能。当然,在很多情况下,不能使用此参数。
3) 通过指定 UNRECOVERABLE选项,可以关闭数据库的日志。这个选项只能和 direct 一起使用。
4) 可以同时运行多个导入任务.
5)可以使用readsize 参数加大读入区内存大小, 默认为64K 一般的系统最大支持到20M
6)SKIP_INDEX_MAINTENANCE = {TRUE | FALSE}
在DIRECT 方式load 数据的时候,设置是否维护索引可以通过 此参数来设置
7)SKIP_UNUSABLE_INDEXES = {TRUE | FALSE}
在DIRECT 方式load 数据的时候 如果有索引为unusable 是否继续load 数据
常规导入与direct导入方式的区别:
常规导入可以通过使用 INSERT语句来导入数据。Direct导入可以跳过数据库的相关逻辑(DIRECT=TRUE),而直接将数据导入到数据文件中。
常规导入的过程
STEP 1
Generate SQL commands for the data to be loaded.
STEP 2
Process the SQL commands (parse, fetch, execute).
STEP 3
Find partial data blocks, or get new extents.
STEP 4
If required blocks are not in the buffer cache, read
the data blocks from disk.
STEP 5
Fill the appropriate data blocks in the buffer cache.
STEP 6
Write data blocks to the disk.
Direct 导入过程
STEP 1
Format the input data into Oracle data blocks.
STEP 2
Get new extents for the table to be loaded.
STEP 3
Write the formatted data blocks to the disk.
使用direct 方式导入的一些限制:
Restrictions on Direct Path Loads
• Clustered tables
• Object columns (Oracle9i can handle these)
• LOB columns (Oracle9i can handle these)
• VARRAY columns
• Nested tables (Oracle9i can handle these when they are loaded separately)
• REF columns (Oracle9i can handle these)
• BFILE columns