๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ORACLE/ADMIN

[Oracle] SQL Loader ๋ฐ์ดํ„ฐ ์ด๋™ ์‚ฌ์šฉ๋ฒ•

by ๐ŸŒปโ™š 2020. 7. 19.

SQL Loader

๋น„์˜ค๋ผํด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์˜ค๋ผํด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ด๋™ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ SQL Loader๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. SQL Loader๋Š” csv๋‚˜ tsv ํŒŒ์ผ์ฒ˜๋Ÿผ ํŠน์ • ๊ตฌ๋ถ„์ž๋‚˜ ์œ„์น˜๋กœ ์ •์˜๋˜์–ด ์žˆ๋Š” ํ…์ŠคํŠธํŒŒ์ผ ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด์„œ importํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. SQL Loader๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด๋Ÿฌํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ด๊ฒจ์ ธ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ํŒŒ์ผ๊ณผ importํ•˜๋Š” ๋ฐ์ดํ„ฐ ํŒŒ์ผ ๋ฐ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๊ธฐ์ˆ ๋˜์–ด์ ธ ์žˆ๋Š” ์ปจํŠธ๋กค ํŒŒ์ผ ๋‘๊ฐ€์ง€๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.

 

 

SQL Loader ์‹ค์Šต

์‹ค์Šตํ™˜๊ฒฝ

OS : Red Hat Enterprise Linux Server release 6.10
DB : Oracle 11.2.0.1
SID : orcl
host : 601d2fce71dc

์‹ค์Šต๋ชฉํ‘œ

- import์šฉ Data file์„ ๊ฐ ํ˜•์‹๋ณ„๋กœ ์ƒ์„ฑ
- import์šฉ Control File ํ˜•์‹๋ณ„ ์˜ต์…˜๋ณ„ ์ƒ์„ฑ
- orcl hr.departments ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ hr.dept_loader ํ…Œ์ด๋ธ”๋กœ ๋ณต์ œ

 

 

 

Import์šฉ Data file

[oracle@601d2fce71dc ~]$ sqlplus hr/hr

HR ๊ณ„์ •์œผ๋กœ ์ ‘์†ํ•ด์„œ ๋ฐ์ดํ„ฐํŒŒ์ผ์€ ๊ตฌ๋ถ„์ž๋ฅผ ์ด์šฉํ•œ ์นผ๋Ÿผ์˜ ๊ตฌ๋ถ„๊ณผ ์œ„์น˜๋กœ ๊ตฌ๋ถ„ํ•œ ๋ฐ์ดํ„ฐํŒŒ์ผ 2๊ฐ€์ง€๋ฅผ ์ด์šฉํ•ด์„œ ์‹ค์Šต ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

Comma Seperated
HR@orcl> 
select 
    department_id 
    || ',' || 
    department_name 
    || ',' || 
    manager_id 
    || ',' || 
    location_id 
from departments;

Comma ๋‹จ์œ„๋กœ ๊ตฌ๋ถ„๊ฐ’์„ ๊ฐ–๋Š” ๋ฐ์ดํ„ฐ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

 

Position
HR@orcl>
select
    rpad(department_id,5)
    || rpad(department_name,31)
    || rpad(manager_id,7)
    || location_id
from departments;

์œ„์น˜๋กœ ๊ตฌ๋ถ„ํ•œ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

column1 (1:4)

column2 (6:35)

column3 (37:42)

column4 (44:47)

 

 

Oracle์—์„œ ์ œ๊ณตํ•˜๋Š” ์—ฐ๊ฒฐ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ ํ˜•์‹๋ณ„๋กœ Import์šฉ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์•ˆ์— ๋„ฃ์„ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ณต์‚ฌํ•ด์„œ Import์šฉ ๋ฐ์ดํ„ฐํŒŒ์ผ์„ ๋งŒ๋“ค์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ๋งŽ์€ ๊ฒฝ์šฐ spool ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐ”๋กœ ํŒŒ์ผ์„ ๋งŒ๋“œ๋Š” ์ž‘์—…์„ ํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.

 

 

[oracle@601d2fce71dc ~]$ vi /opt/oracle/departments.dat

๋ณต์‚ฌํ•œ ๋‚ด์šฉ์„ ํŒŒ์ผ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

 

 

Import Control File

[oracle@601d2fce71dc ~]$ vi /opt/oracle/department.ctl

 

append(Comma Seperated)
load data
infile '/opt/oracle/departments.dat'
append
into table hr.dept_loader
fields terminated by ','
(
  department_id
  ,department_name
  ,manager_id
  ,localtion_id
)

append ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ์กด์— ์žˆ๋Š” ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ์—์„œ ์ถ”๊ฐ€์ ์œผ๋กœ insertํ•˜๋Š” ์ž‘์—…์„ ํ•ฉ๋‹ˆ๋‹ค. 

infile - ๋ฐ์ดํ„ฐ ํŒŒ์ผ์˜ ์œ„์น˜

into table - table ์˜ค๋ธŒ์ ํŠธ์˜ ์ด๋ฆ„

fields terminated by - ๊ตฌ๋ถ„์ž 

 

 

truncate(Comma Seperated)
load data
infile '/opt/oracle/departments.dat'
truncate
into table hr.dept_loader
fields terminated by ','
(
  department_id
  ,department_name
  ,manager_id
  ,localtion_id
)

truncate ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ธฐ์กด์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์šฐ๊ณ  Importํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ ๋ฐ˜์˜ํ•ฉ๋‹ˆ๋‹ค.

 

 

position datafile import
load data
infile '/opt/oracle/departments.dat'
append
into table hr.dept_loader
(
  department_id       position(1:4)
  ,department_name    position(6:35)
  ,manager_id         position(37:42)
  ,location_id        position(44:47)
)

์œ„์น˜๊ธฐ๋ฐ˜์œผ๋กœ Importํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

Import

HR@orcl> create table dept_loader as select * from departments where 1=0;

SQL Loader๋ฅผ ์‚ฌ์šฉํ• ๋•Œ๋Š” ๋ฏธ๋ฆฌ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ด์•ผ Import๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

 

[oracle@601d2fce71dc ~]$ sqlldr hr/hr control=/opt/oracle/departments.ctl

27๊ฐœ์˜ ํ–‰์ด ์„ฑ๊ณต์ ์œผ๋กœ Import๋œ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ Control File ์˜ต์…˜์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ์˜ import ์ƒํƒœ๋ฅผ ์ดํ›„ ํ™•์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

 

Direct  Option

[oracle@601d2fce71dc ~]$ sqlldr hr/hr control=/opt/oracle/departments.ctl direct=true
[oracle@601d2fce71dc ~]$ vi departments.log 

Direct Option์„ ์‚ฌ์šฉํ•˜๊ณ  ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด Path used ๊ฐ’์ด Conventional์ด ์•„๋‹Œ Direct๋กœ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Direct ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๋น ๋ฅด๊ฒŒ Import ์ž‘์—…์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด Conventionalํ•œ ๋ฐฉ์‹์€ Importํ•˜๋Š” ๊ณผ์ •์—์„œ SGA ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ๋ฉ๋‹ˆ๋‹ค. ์šด์˜์ค‘์ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๊ณต์œ ๋˜๋Š” ๋ฉ”๋กœ๋ฆฌ๋กœ Import์ž‘์—…์„ ํ•˜๊ฒŒ๋˜๋ฉด ๊ฒฝํ•ฉ์ด ๋ฐœ์ƒํ•˜์—ฌ Waiting Event๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๊ณ  ๊ณต์œ ๋˜๋Š” ๊ณต๊ฐ„์ด๋‹ค๋ณด๋‹ˆ ์ž‘์—…์ด Directํ•œ ๋ฐฉ์‹๋ณด๋‹ค ์ƒ๋Œ€์ ์œผ๋กœ ๋Š๋ฆฝ๋‹ˆ๋‹ค. Directํ•œ ๋ฐฉ์‹์€ PGA์˜์—ญ์„ ์‚ฌ์šฉํ•ด์„œ datafile์— ๋ฐ”๋กœ Insertํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ์‹์œผ๋กœ SGA ๊ณต์œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

 

Import ์ดํ›„

Import๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ์ž‘์—… ๋„์ค‘ ์„ธ๋ถ€์ ์œผ๋กœ ์–ด๋–ค ์ผ์ด ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ๊ธฐ๋ก๋˜๋Š” logํŒŒ์ผ์ดํ•˜๋‚˜ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. Import๊ฐ€ ์„ฑ๊ณตํ• ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์˜ ๋ฌธ์ œ๋กœ ์‹คํŒจํ• ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹คํŒจ์‹œ Discard File๊ณผ Bad File ๋‘๊ฐ€์ง€ ๋กœ๊ทธํŒŒ์ผ์ด ์ƒ์„ฑ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Discard File

Control File์•ˆ์— ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค์ง€ ๋ชปํ•œ ํ–‰๋“ค์ด ๊ธฐ๋ก๋ฉ๋‹ˆ๋‹ค.

 

Bad File

Insert ํ•˜๊ณ ์žํ•˜๋Š” ํ…Œ์ด๋ธ”์— ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ Import๋˜๋Š” ๊ฒฝ์šฐ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋˜ ํ–‰๋“ค์ด ๊ธฐ๋ก๋ฉ๋‹ˆ๋‹ค.

 

 

 

 

 

๋Œ“๊ธ€