SQL*Loader之CASE6

时间:2023-03-09 14:30:19
SQL*Loader之CASE6

CASE6

1. SQL脚本

[oracle@node3 ulcase]$ cat ulcase6.sql

set termout off
rem host write sys$output "Building case 6 demonstration tables. Please wait" drop table emp; create table emp
(empno number(4) not null,
ename char(10),
job char(9),
mgr number(4),
hiredate date,
sal number(7,2),
comm number(7,2),
deptno number(2)); create unique index empix on emp(empno); exit

2. 控制文件

[oracle@node3 ulcase]$ cat ulcase6.ctl

-- Copyright (c) 1991, 2004 Oracle.  All rights reserved.
-- NAME
-- ulcase6.ctl - SQL*Loader Case Study 6: Loading Data Using the
-- Direct Path Load Method
--
-- DESCRIPTION
-- This case study demonstrates the following:
-- Use of the direct path load method to load and index data.
--
-- How to specify the indexes for which the data is presorted.
--
-- Use of the NULLIF clause.
--
-- Loading all-blank numeric fields as NULL.
--
-- TO RUN THIS CASE STUDY:
-- 1. Before executing this control file, log in to SQL*Plus as
-- scott/tiger. Enter @ulcase6 to execute the SQL script for
-- this case study. This prepares and populates tables and
-- then returns you to the system prompt.
--
-- 2. At the system prompt, invoke the case study as follows:
-- sqlldr USERID=scott/tiger CONTROL=ulcase6.ctl LOG=ulcase6.log DIRECT=TRUE
--
-- NOTES ABOUT THIS CONTROL FILE
-- The SORTED INDEXES statement identifies the indexes on which
-- the data is sorted. This statement indicates that the datafile
-- is sorted on the columns in the empix index. It allows
-- SQL*Loader to optimize index creation by eliminating the sort
-- phase for this data when using the direct path load method.
--
-- The NULLIF...BLANKS clause specifies that the column should
-- be loaded as NULL if the field in the datafile consists of
-- all blanks.
--
LOAD DATA
INFILE 'ulcase6.dat'
REPLACE
INTO TABLE emp
SORTED INDEXES (empix)
(empno POSITION(1:4),
ename POSITION(6:15),
job POSITION(17:25),
mgr POSITION(27:30) NULLIF mgr=blanks,
sal POSITION(32:39) NULLIF sal=blanks,
comm POSITION(41:48) NULLIF comm=blanks,
deptno POSITION(50:51) NULLIF empno=blanks)

3. 数据文件

[oracle@node3 ulcase]$ cat ulcase6.dat

7499 ALLEN      SALESMAN  7698  1600.00   300.00 30
7566 JONES MANAGER 7839 3123.75 20
7654 MARTIN SALESMAN 7698 1312.50 1400.00 30
7658 CHAN ANALYST 7566 3450.00 20
7782 CLARK MANAGER 7839 2572.50 10
7839 KING PRESIDENT 5500.00 10
7934 MILLER CLERK 7782 920.00 10

执行后结果:

[oracle@node3 ulcase]$ sqlplus scott/tiger @ulcase6.sql

[oracle@node3 ulcase]$ sqlldr userid=scott/tiger control=ulcase6.ctl direct=yes

--注意,上述命令多了一个参数direct=yes,代表直接路径插入

SQL> select * from emp;

EMPNO ENAME     JOB         MGR  HIREDATE     SAL  COMM  DEPTNO
----- ---------- --------- ----- --------- ------- ----- ------
7499 ALLEN SALESMAN 7698 1600 300 30
7566 JONES MANAGER 7839 3124 20
7654 MARTIN SALESMAN 7698 1313 1400 30
7658 CHAN ANALYST 7566 3450 20
7782 CLARK MANAGER 7839 2573 10
7839 KING PRESIDENT 5500 10
7934 MILLER CLERK 7782 920 10 7 rows selected.

查看日志文件:

[oracle@node3 ulcase]$ vim ulcase6.log

SQL*Loader: Release 11.2.0.1.0 - Production on Fri Sep 19 02:49:21 2014

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Control File:   ulcase6.ctl
Data File: ulcase6.dat
Bad File: ulcase6.bad
Discard File: none specified (Allow all discards) Number to load: ALL
Number to skip: 0
Errors allowed: 50
Continuation: none specified
Path used: Direct Table EMP, loaded from every logical record.
Insert option in effect for this table: REPLACE Column Name Position Len Term Encl Datatype
------------------------------ ---------- ----- ---- ---- ---------------------
EMPNO 1:4 4 CHARACTER
ENAME 6:15 10 CHARACTER
JOB 17:25 9 CHARACTER
MGR 27:30 4 CHARACTER
NULL if MGR = BLANKS
SAL 32:39 8 CHARACTER
NULL if SAL = BLANKS
COMM 41:48 8 CHARACTER
NULL if COMM = BLANKS
DEPTNO 50:51 2 CHARACTER
NULL if EMPNO = BLANKS The following index(es) on table EMP were processed:
index SCOTT.EMPIX loaded successfully with 7 keys Table EMP:
7 Rows successfully loaded.
0 Rows not loaded due to data errors.
0 Rows not loaded because all WHEN clauses were failed.
0 Rows not loaded because all fields were null. Bind array size not used in direct path.
Column array rows : 5000
Stream buffer bytes: 256000
Read buffer bytes: 1048576 Total logical records skipped: 0
Total logical records read: 7
Total logical records rejected: 0
Total logical records discarded: 0
Total stream buffers loaded by SQL*Loader main thread: 1
Total stream buffers loaded by SQL*Loader load thread: 0 Run began on Fri Sep 19 02:49:21 2014
Run ended on Fri Sep 19 02:49:27 2014 Elapsed time was: 00:00:06.66
CPU time was: 00:00:01.27

注意:在本例中

1> SORTED INDEXES (empix)指明数据文件的数据是按照索引empix对应的列排过序的,只有在直接路径插入下有效,即sqlldr命令中需指明direct=yes,这样可极大提高插入效率。

2> NULLIF...BLANKS子句指明列对应数据文件的位置如果是空格的话,当null处理。