SQL Loader
SQL Loader
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
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
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.
Log File
SQL *Loader
Bad File
Loading Method
Basically there are 3 Loading Methods Conventional Path Load Direct Path Load External Table Load( from Oracle 9i).
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
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 : Very High Speed Disadvantage : Cannot Load Object Types, Collection Types (Nested Table, VARRAY),LOB(CLOB,NCLOB,BLOB, BFILE)
13
Field Conversion
Field 1
DATA FILE Field 2
a a a
CHAR(5) Ctrl File Spec
b b b
CHAR(5)
SQL LOADER
BIND ARRAY
aaa
Table
bbb
DATABASE
Column 1
Column 2
SERVER
a a a_ _
CHAR(5) Column Data Type
bbb
VARCHAR(5)
14
Record Filtering
Record in Data file
Read in
Database
Control file
Bad File
Selected
Inserted
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
18
$ sqlldr control=control2.ctl userid=apps/secretone Sql_ldr2: N1 N2 N3 111 222 666 333 444 666 555 666 666 777 888 666 999 000 666
19
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) )
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
21
Field Separator
data5.dat 1111;2222; 3333;4444; 1111;2222; 3333;4444; 1111;2222;
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
22
23
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 N1 11 33 55 N2 22 44 66
Sql_ldr102 N1 11 33 55 N2 22 44 66
24
Skipping records
data2.dat 111 222 666 333 444 666 555 666 666 777 888 666 999 000 666
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
aa cc
bbb ddd
27
12 56 91
34 78 1
28
29
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
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.
31
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
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
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.
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