Sie sind auf Seite 1von 35

Basic Concepts

Sql *Loader

It is a high speed data loading utility


supplied by Oracle that loads data from
external files into table in an Oracle
Database.
It can accepts data in variety of formats, can
perform transformation, filtering and can
load into multiple tables from multiple files
in same loading session
2

Requirements- For Sql *Loader

Tables to be loaded must already exists in the


Database. SQL *Loader never creates tables, it
loads existing tables.
Table may be empty or may already contain data.
Privileges:
INSERT privilege
DELETE privilege, when using REPLACE or
TRUNCATE insert option
3

Files used in Sql *Loader

Control File - .ctl file


Data File - .dat file
Log File - .log file
Bad File - .bad file
Discard File - .dis file

Control File
It controls the Behavior of Sql *Loader
Source of Data to be loaded
Destination of Data to be loaded
Filtering of Data before Loading
Transformation of Data before Loading
Relating the Data file fields to table
columns
5

Log File

It is a record of SQL *Loaders activities during a load


session
Control, Data, Bad, Discard file Name.
Values of several command line parameters.
Detailed breakdown of the fields & datatypes in data file
that was loaded.
Error Message for records that cause error.
Message indicating when records have been discarded.
Summary of the Load (no: of records read from file, no: of
rows rejected because of errors, no: of rows discarded &
elapsed time of load)
6

Data File Contains the data to be loaded, it is


optional
Discard File Contains the data that are discarded
by when clause in control file, it is optional.
Bad File Contains the data which are not loaded
due to some errors, it is not optional, if any one
error occurred, SQL *Loader will create the bad
file and write the offending input records into it.

SQL *Loader Overview


Control File

Input Data File

Log
File

SQL *Loader

Bad
File

Discard
File
DataBase

Loading Method
Basically there are 3 Loading Methods
Conventional Path Load
Direct Path Load
External Table Load( from Oracle 9i).

Conventional Path Load

Bind Array (Row Insert) is created by Sql


*Loader based on field specification in
control file

Data from the Bind Array are then insert


into the table by the DB server, if data
satisfies with the corresponding column
datatype
10

Direct Path Load

Parses the data according to the field specification


in Control File
Converts the data to Column datatype (not to field
datatype in .ctl file) and builds Column array.
Column array is passed to block formatter
Newly formatted blocks are directly written
directly to the database file bypassing most SQL
Processing
PARALLEL loading is possible.
11

PARALLEL Loading

Setup has to done, as the text file must be


broken into several smaller files
We can run several SQLLDR session in
parallel for each broken data files
It will increase the performance, reduce the
time for loading

12

Advantage & Disadvantage of


Direct Path Load

Advantage :
Very High Speed
Disadvantage :
Cannot Load Object Types,
Collection Types (Nested Table,
VARRAY),LOB(CLOB,NCLOB,BLOB,
BFILE)
13

Field Conversion
DATA
FILE

Field 1

b b b

a a a
CHAR(5)

BIND
ARRAY

Field 2

Ctrl File Spec

aaa

CHAR(5)

SQL
LOADER

bbb
Table

DATABASE

Column 1

Column 2

a a a_ _

bbb

CHAR(5)

Column Data Type

SERVER

VARCHAR(5)

14

Record Filtering
Record in
Data file
Database

Read in

Control
file

SQL *Loader
Field Processing
Accepted

Discard Discarded
File

SQL *Loader
When-clause
Evaluation

Rej
ec

ted

Bad
File
Selected

Re
je

Inserted
ct e

Database
Server

15

16

Mapping between data fields in


control file and columns of the table
SQL> desc sampledata;
Name
Null Type
------------------------------- -------- ---N1
VARCHAR2(25)
N2
VARCHAR2(25)
LOAD DATA
INFILE '/u01/xxvis/data1.dat'
replace
into table sampledata
fields terminated by X'09'
(
n2,
n1
)

12
7891
1234

3456
0111
3333

SQL> select * from sampledata;


N1
N2
------------------------- ---3456
12
0111
7891
3333
1234

17

Data with in Control File


control1.ctl
load data
$ sqlldr control=control1.ctl userid=apps/secretone
infile *
into table sql_ldr1 REPLACE
(
n position(1:2)
)
Sql_ldr1
begindata
N
3
3
4
4
5
5
6
6
7
7

18

Data in DATA file


data2.dat
111 222 666
333 444 666
555 666 666
777 888 666
999 000 666

$ sqlldr control=control2.ctl userid=apps/secretone

control2.ctl
load data
infile 'data2.dat'
insert
into table sql_ldr2 replace
(
n1 position(1:3),
n2 position(5:7),
n3 position(9:11)
)

Sql_ldr2:
N1 N2 N3
111 222 666
333 444 666
555 666 666
777 888 666
999 000 666

19

Appending to a table
Sql_ldr3 ( Before Loading):
--------------------------------N1
N2
N3
------ --------- --------111
222
666
333
444
666
555
666
666
777
888
666
999
0
666

data3.dat
111 222 666
333 444 666
555 666 666
777 888 666
999 000 666
control3.ctl
load data
infile 'data3.dat'
insert
into table sql_ldr3 append
(
n1 position(1:3),
n2 position(5:7),
n3 position(9:11)
)

Sql_ldr3 ( After Loading):


--------------------------------N1
N2
N3
------ --------- --------111
222
666
333
444
666
555
666
666
777
888
666
999
0
666
111
222
666
333
444
666
555
666
666
777
888
666
999
0
666

20

Loading the Selected data


data4.dat
111 222
333 444
111 222
333 444
111 222

$ sqlldr control=control4.ctl userid=apps/secretone

$ sqlldr control=control4.ctl userid=apps/secretone dicard =


data4.dis
control4.ctl
load data
infile 'data4.dat'
insert
into table sql_ldr4
when n2 = '222'
(
n1 position(1:3),
n2 position(5:7)
)

Sql_ldr4:
N1
N2
--------- --------111
222
111
222
111
222

data4.dis
333 444
333 444

21

Field Separator
data5.dat
1111;2222;
3333;4444;
1111;2222;
3333;4444;
1111;2222;

$ sqlldr control=control5.ctl userid=apps/secretone

control5.ctl
load data
infile 'data5.dat'
insert
into table sql_ldr5 replace
when n2='2222'
fields terminated by ';'
(
n1 integer external,
n2 integer external
)

Sql_ldr5
N1
N2
-------- --------1111 2222
1111 2222
1111 2222

22

Loading Multiple Data Files


data91.dat
11 22
33 44
55 66
data92.dat
77 88
99 00
11 11

control9.ctl
load data
$
infile 'data91.dat'
infile 'data92.dat'
insert into
table sql_ldr9 replace
(
n1 position(1:2),
n2 position(4:5)
)

sqlldr control=control2.ctl userid=apps/secretone

Sql_ldr9
N1
11
33
55
77
99
11

N2
22
44
66
88
0
11

23

Loading into Multiple Tables


data10.dat
11 22
33 44
55 66

$ sqlldr control=control10.ctl userid=apps/secretone

control10.ctl
load data
infile 'data10.dat'
insert into
table sql_ldr101 replace
(n1 position(1:2),
n2 position(4:5))
into
table sql_ldr102 replace
(n1 position(1:2),
n2 position(4:5))

Sql_ldr101

Sql_ldr102

N1
11
33
55

N1
11
33
55

N2
22
44
66

N2
22
44
66

24

Loading from Multiple Data files


to Multiple Tables
control11.ctl
$ sqlldr control=control11.ctl userid=apps/secretone
load data
infile 'data111.dat'
infile 'data112.dat'
insert into
Sql_ldr111
Sql_ldr101
data112.dat
table sql_ldr111 replace
77 88
N1
N2
(n1
position(1:2),
11
22
99 11
N1
N2
33
44
n2 position(4:5))
99 99
11
22
55
66
into
77
88
33
44
99
11
table sql_ldr112 replace
55
66
99
99
(n1 position(1:2),
77
88
n2 position(4:5))
99
11
99
99

data111.dat
11 22
33 44
55 66

25

Skipping records
data2.dat
111 222 666
333 444 666
555 666 666
777 888 666
999 000 666

$ sqlldr control=control2.ctl userid=apps/secretone skip=2

control2.ctl
load data
infile 'data2.dat'
insert
into table sql_ldr2 replace
(
n1 position(1:3),
n2 position(5:7),
n3 position(9:11)
)

Sql_ldr2
N1 N2 N3
555 666 666
777 888 666
999 000 666

26

Using LOAD option


data14.dat
aabbb
$ sqlldr control=control14.ctl userid=apps/secretone LOAD = 2
ccddd
eefff
control14.ctl
load data
infile 'data14.dat'
into table sql_ldr14
truncate/replace
(
a1 char(2),
a2 char(3)
)

Sql_ldr14:
A1

A2

aa
cc

bbb
ddd

27

Fixed Record Format


data12.dat
12,3456,7891,0111,2

$ sqlldr control=control12.ctl userid=apps/secretone

control12.dat
load data
infile 'data12.dat' "fix 5"
insert
into table sql_ldr12 replace
fields terminated by ','
(
n1 integer external,
n2 integer external
)

Sql_ldr12:
N1
12
56
91

N2
34
78
1

28

Variable Record Format


Desc sql_ldr1
a1 Varchar2(10)
a2 Varchar2(10)

data1.dat
07aaa,bbb08ccc,dddd05ee,ff
control1.ctl
load data
$ sqlldr control=control1.ctl userid=apps/secretone
infile data1.dat var 02
Sql_ldr11:
into table sql_ldr1
fields terminated by ,
A1
A2
(
aaa
bbb
a1 char,
ccc
dddd
a2 char
ee
ff
);

29

Stream Record Format


data2.dat
aaa,bbb|ccc,dddd|eeee,ffff|

Desc sql_ldr1
a1 Varchar2(10)
a2 Varchar2(10)

Control2.ctl
$ sqlldr control=control2.ctl userid=apps/secretone
load data
Sql_ldr12:
infile data2.dat str |
into table sql_ldr2
A1
A2
fields terminated by ,
aaa
bbb
(
ccc
dddd
a1 char,
eeee
ffff
a2 char
)

30

Command Line Parameters

BAD - Specifies the file in which all bad data is kept. The default
filename is the control filename with a .bad extension.

BINDSIZE - Size of the bind array in bytes. System dependent.

CONTROL - Specifies the name of control file.

DATA - Specifies the file that contains all data to be loaded.

DIRECT - If set to TRUE Direct Path Load is used. Otherwise


Conventional Path will be used. Default value is FALSE.

DISCARD - Specifies the file where all discarded data is kept

31

Command Line Parameters

DISCARDMAX The maximum no: of invalid records, which is not


satisfying the when clause in control file, that may be encountered
before SQL *Loader session is stop. Default value is ALL.

ERRORS Specifies how many total errors can be encountered before


SQL *Loader session is to stop. Default value is 50.

LOAD Specifies the maximum no: of records to load before


stopping. Default Value is ALL.

LOG Specifies the log files name, where information on success or


failure of the Loading session is reported. The Default filename is
control_filename.log.

32

Command Line Parameters

PARALLEL If set to TRUE, loads are performed in parallel where


possible. Default value is FALSE

PARFILE An additional file that contains more parameter


specification.

ROWS No: of rows to put in the path bind array, for Conventional
path load. For Direct path load, ROWS specifies the no: of rows to
read before a data save is performed. Default value is 64 in
Conventional Path.

SKIP No: of record to skip before starting the load. This parameter is
important for restarting a load process after stopping an earlier session.
Useful in Recovery from failure.

33

Command Line Parameters

USERID Specifies the username and password for the user conducting the SQL
*Loader session
SILENT Allows to suppress various header and feedback messages that SQL
*Loader normally displays during a Load session.

Keyword for use with SILENT parameter

DISCARDS Suppresses the discarded messages


ERRORS Suppresses the error message
FEEDBACK Suppresses the commit point reached messages.
HEADER Suppresses the messages that SQL *Loader displays on the screen
when we first launch the executable.
PARTITION Suppresses the per-partition statistics, when loading a direct
path load of a partitioned table.

34

35

Das könnte Ihnen auch gefallen