You are on page 1of 40

19

Loading Data into a Database

Copyright © Oracle Corporation, 2002. All rights reserved.


Objectives

After completing this lesson, you should be able to do


the following:
• Demonstrate usage of Direct Load operations
• Describe the usage of SQL*Loader
• Perform basic SQL*Loader operations
• List guidelines for using SQL*Loader and Direct
Load

19-2 Copyright © Oracle Corporation, 2002. All rights reserved.


Data Loading Methods

Other applications

Oracle
database
Export
SQL*Loader

Import

Oracle
database
Direct Load

19-3 Copyright © Oracle Corporation, 2002. All rights reserved.


Direct Load

Direct Load insert can be performed in the following


ways:
• Normal (serially), or in parallel
• Into partitioned tables, nonpartitioned tables, or
single partitions of a table
• With or without logging of redo data

19-4 Copyright © Oracle Corporation, 2002. All rights reserved.


19-5 Copyright © Oracle Corporation, 2002. All rights reserved.
Serial Direct Load

INSERT /*+ APPEND */ INTO emp


NOLOGGING
SELECT * FROM t_employees;
COMMIT;
Server
process
EMPLOYEES table

Used block High-water mark

Free space after delete Blocks used by inserted rows

19-6 Copyright © Oracle Corporation, 2002. All rights reserved.


Parallel Direct Load

ALTER SESSION ENABLE PARALLEL DML;


INSERT /*+PARALLEL(hr.employees,2) */
INTO hr.employees NOLOGGING
SELECT * FROM hr.old_employees;

Slave Slave
process process
EMPLOYEES table

Used block High-water mark


Free space after delete Temporary segments

19-7 Copyright © Oracle Corporation, 2002. All rights reserved.


19-8 Copyright © Oracle Corporation, 2002. All rights reserved.
SQL*Loader
Loader control file
Input data files

Parameter file
SQL*Loader Rejected
(optional)
Field processing
Discarded Accepted
Record selection Bad
file
Selected

Oracle server
Discard file
Inserted Rejected
(optional)

Log file

Database data files

19-9 Copyright © Oracle Corporation, 2002. All rights reserved.


19-10 Copyright © Oracle Corporation, 2002. All rights reserved.
Using SQL*Loader

$sqlldr hr/hr \
> control=case1.ctl \
> log=case1.log direct=Y

case1.ctl

SQL*Loader

EMPLOYEES table

case1.log

19-11 Copyright © Oracle Corporation, 2002. All rights reserved.


19-12 Copyright © Oracle Corporation, 2002. All rights reserved.
SQL*Loader Control File

The loader control file tells SQL*Loader:


• Where to find the load data
• The data format
• Configuration details:
– Memory management
– Record rejection
– Interrupted load handling details
• How to manipulate the data

19-13 Copyright © Oracle Corporation, 2002. All rights reserved.


19-14 Copyright © Oracle Corporation, 2002. All rights reserved.
19-15 Copyright © Oracle Corporation, 2002. All rights reserved.
19-16 Copyright © Oracle Corporation, 2002. All rights reserved.
Control File Syntax Considerations

• The syntax is free-format.


• Syntax is not case sensitive.
• Comments extend from the two hyphens (--) that
mark the beginning of the comment to the end of the
line.
• The CONSTANT keyword is reserved.

19-17 Copyright © Oracle Corporation, 2002. All rights reserved.


Input Data and Data Files

• SQL*Loader reads data from one or more files


specified in the control file.
• From SQL*Loader’s perspective, the data in the data
file is organized as records.
• A data file can be in one of three formats:
– Fixed-record format
– Variable-record format
– Stream-record format

19-18 Copyright © Oracle Corporation, 2002. All rights reserved.


19-19 Copyright © Oracle Corporation, 2002. All rights reserved.
19-20 Copyright © Oracle Corporation, 2002. All rights reserved.
19-21 Copyright © Oracle Corporation, 2002. All rights reserved.
Logical Records

SQL*Loader can be instructed to follow one of the


following two logical record-forming strategies:
• Combine a fixed number of physical records to form
each logical record.
• Combine physical records into logical records while
a certain condition is true.

19-22 Copyright © Oracle Corporation, 2002. All rights reserved.


Loading Methods

Instance
Array SGA Shared pool
insert

Conventional

Data Direct
Table save path

High-water
mark
Space used only by conventional load

19-23 Copyright © Oracle Corporation, 2002. All rights reserved.


19-24 Copyright © Oracle Corporation, 2002. All rights reserved.
19-25 Copyright © Oracle Corporation, 2002. All rights reserved.
Comparing Direct and Conventional
Path Loads
Conventional Load Direct Path Load
Uses COMMITs to make Uses data saves
changes permanent
Redo entries always Generates redo only under
generated specific conditions
Enforces all constraints Enforces only primary key,
unique, and NOT NULL
INSERT triggers fire INSERT triggers do not fire
Can load into clustered Cannot load into
tables clustered tables
Other users can make Other users cannot
changes to tables make changes to tables

19-26 Copyright © Oracle Corporation, 2002. All rights reserved.


19-27 Copyright © Oracle Corporation, 2002. All rights reserved.
Parallel Direct Path Load

Temporary segments
load1.dat SQL*Loader
load1.ctl

SQL*Loader
load2.dat
load2.ctl

SQL*Loader
load3.dat
load3.ctl
Table

High-water
mark

19-28 Copyright © Oracle Corporation, 2002. All rights reserved.


Data Conversion

During a conventional path load, data fields in the data


file are converted into columns in the database in two
steps:
• The field specifications in the control file are used to
interpret the format of the data file and convert it to
a SQL INSERT statement using that data.
• The Oracle database server accepts the data and
executes the INSERT statement to store the data in
the database.

19-29 Copyright © Oracle Corporation, 2002. All rights reserved.


Discarded or Rejected Records

• Bad file:
– SQL*Loader rejects records when the input format is
invalid.
– If the Oracle database finds that the row is invalid,
then the record is rejected and SQL*Loader puts it in
the bad file.
• Discard file:
– This can be used only if it has been enabled.
– This file contains records that were filtered out
because they did not match any record-selection
criteria specified in the control file.

19-30 Copyright © Oracle Corporation, 2002. All rights reserved.


19-31 Copyright © Oracle Corporation, 2002. All rights reserved.
19-32 Copyright © Oracle Corporation, 2002. All rights reserved.
19-33 Copyright © Oracle Corporation, 2002. All rights reserved.
Log File Contents

• Header information
• Global information
• Table information
• Data file information
• Table load information
• Summary statistics
• Additional statistics for Direct path loads and
multithreading information

19-34 Copyright © Oracle Corporation, 2002. All rights reserved.


19-35 Copyright © Oracle Corporation, 2002. All rights reserved.
SQL*Loader Guidelines

• Use a parameter file to specify commonly used


command line options.
• Place data within the control file only for a small,
one-time load.
• Improve performance by:
– Allocating sufficient space
– Sorting the data on the largest index
– Specifying different files for temporary segments for
parallel loads

19-36 Copyright © Oracle Corporation, 2002. All rights reserved.


Summary

In this lesson, you should have learned how to:


• Describe the usage of SQL*Loader
• Perform basic SQL*Loader operations
• Demonstrate proficiency using Direct Load
operations
• List guidelines for using SQL*Loader and Direct
Load operations

19-37 Copyright © Oracle Corporation, 2002. All rights reserved.


Practice 19 Overview

This practice covers the following topics:


• Using SQL*Loader to restore data:
– Using a control file
– Using a data file
• Using Direct Load to load data

19-38 Copyright © Oracle Corporation, 2002. All rights reserved.


19-39 Copyright © Oracle Corporation, 2002. All rights reserved.
19-40 Copyright © Oracle Corporation, 2002. All rights reserved.

You might also like