SQL*LOADER是ORACLE的數據加載工具,通常用來將操作系統文件遷移到ORACLE數據庫中。SQL*LOADER是大型數據倉庫選擇使用的加載方法,因為它提供了最快速的途徑(DIRECT,PARALLEL)。現在,我們拋開其理論不談,用實例來使您快速掌握SQL*LOADER的使用方法。
首先,我們認識一下SQL*LOADER。
在NT下,SQL*LOADER的命令為SQLLDR,在UNIX下一般為sqlldr/sqlload。
如執行:d:\oracle>sqlldr
SQL*Loader: Release 8.1.6.0.0 - Production on 星期二 1月 8 11:06:42 2002
(c) Copyright 1999 Oracle Corporation. All rights reserved.
用法: SQLLOAD 關鍵字 = 值 [,keyword=value,...]
有效的關鍵字:
userid -- ORACLE username/password
control -- Control file name(控制文件)
log -- Log file name(記錄的日志文件)
bad -- Bad file name(壞數據文件)
data -- Data file name(數據文件)
discard -- Discard file name(丟棄的數據文件)
discardmax -- Number of discards to allow(允許丟棄數據的最大值) (全部默認)
skip -- Number of logical records to skip (默認0)
load -- Number of logical records to load (全部默認)
errors -- Number of errors to allow(允許的錯誤記錄數)(默認50)
rows -- Number of rows in conventional path bind array or between direct path data saves
(每次提交的記錄數,默認: 常規路徑 64, 所有直接路徑)
bindsize -- Size of conventional path bind array in bytes(默認65536)
首先,我們認識一下SQL*LOADER。
在NT下,SQL*LOADER的命令為SQLLDR,在UNIX下一般為sqlldr/sqlload。
如執行:d:\oracle>sqlldr
SQL*Loader: Release 8.1.6.0.0 - Production on 星期二 1月 8 11:06:42 2002
(c) Copyright 1999 Oracle Corporation. All rights reserved.
用法: SQLLOAD 關鍵字 = 值 [,keyword=value,...]
有效的關鍵字:
userid -- ORACLE username/password
control -- Control file name(控制文件)
log -- Log file name(記錄的日志文件)
bad -- Bad file name(壞數據文件)
data -- Data file name(數據文件)
discard -- Discard file name(丟棄的數據文件)
discardmax -- Number of discards to allow(允許丟棄數據的最大值) (全部默認)
skip -- Number of logical records to skip (默認0)
load -- Number of logical records to load (全部默認)
errors -- Number of errors to allow(允許的錯誤記錄數)(默認50)
rows -- Number of rows in conventional path bind array or between direct path data saves
(每次提交的記錄數,默認: 常規路徑 64, 所有直接路徑)
bindsize -- Size of conventional path bind array in bytes(默認65536)
(每次提交記錄的緩沖區的大小(字節為單位,默認256000))
silent -- Suppress messages during run (header,feedback,errors,discards,partitions)(禁止輸出信息)
direct -- use direct path (使用直通路徑方式導入) (默認FALSE)
parfile -- parameter file: name of file that contains parameter specifications
parallel -- do parallel load (並行導入) (默認FALSE)
file -- File to allocate extents from
silent -- Suppress messages during run (header,feedback,errors,discards,partitions)(禁止輸出信息)
direct -- use direct path (使用直通路徑方式導入) (默認FALSE)
parfile -- parameter file: name of file that contains parameter specifications
parallel -- do parallel load (並行導入) (默認FALSE)
file -- File to allocate extents from
(與bindsize成對使用,其中較小者會自動調整到較大者sqlldr先計算單條記錄長度,乘以rows,如小bindsize 不 會rows以填充bindsize;如超出,則以bindsize為准。)
skip_unusable_indexes -- disallow/allow unusable indexes or index partitions(默認FALSE)
skip_index_maintenance -- do not maintain indexes, mark affected indexes as unusable(默認FALSE)
commit_discontinued -- commit loaded rows when load is discontinued(默認FALSE)
readsize -- Size of Read buffer (默認1048576)
PLEASE NOTE: 命令行參數可以由位置或關鍵字指定。
skip_unusable_indexes -- disallow/allow unusable indexes or index partitions(默認FALSE)
skip_index_maintenance -- do not maintain indexes, mark affected indexes as unusable(默認FALSE)
commit_discontinued -- commit loaded rows when load is discontinued(默認FALSE)
readsize -- Size of Read buffer (默認1048576)
PLEASE NOTE: 命令行參數可以由位置或關鍵字指定。
前者的例子是 'sqlload scott/tiger foo';后者的例子是 'sqlload control=foo userid=scott/tiger'.位置指定參數的時間必須早於但不可遲於由關鍵字指定的參數。例如,
'SQLLOAD SCott/tiger control=foo logfile=log', 但'不允許 sqlload scott/tiger control=foo log',即使允許
參數 'log' 的位置正確。
d:\oracle>
我們可以從中看到一些基本的幫助信息,這里,我用到的是中文的WIN2000 ADV SERVER。
我們知道,SQL*LOADER只能導入純文本,所以我們現在開始以實例來講解其用法。
一、已存在數據源result.csv,欲倒入ORACLE中FANCY用戶下。
result.csv內容:
1,默認 Web 站點,192.168.2.254:80:,RUNNING
2,other,192.168.2.254:80:test.com,STOPPED
3,third,192.168.2.254:81:thirdabc.com,RUNNING
從中,我們看出4列,分別以逗號分隔,為變長字符串。
二、制定控制文件result.ctl
'SQLLOAD SCott/tiger control=foo logfile=log', 但'不允許 sqlload scott/tiger control=foo log',即使允許
參數 'log' 的位置正確。
d:\oracle>
我們可以從中看到一些基本的幫助信息,這里,我用到的是中文的WIN2000 ADV SERVER。
我們知道,SQL*LOADER只能導入純文本,所以我們現在開始以實例來講解其用法。
一、已存在數據源result.csv,欲倒入ORACLE中FANCY用戶下。
result.csv內容:
1,默認 Web 站點,192.168.2.254:80:,RUNNING
2,other,192.168.2.254:80:test.com,STOPPED
3,third,192.168.2.254:81:thirdabc.com,RUNNING
從中,我們看出4列,分別以逗號分隔,為變長字符串。
二、制定控制文件result.ctl
命令
說明
load data .......... 控制文件標識
infile 'model.txt' ............ 要輸入的數據文件名為test.txt
append into table system.表名 ............ 向表test中追加記錄
fields terminated by X'09' ....... 指定分隔符,字段終止於X'09',是一個制表符(TAB)
(編號,名稱,大小) ......... 定義列對應表中順序
如下實例 result.ctl內容:
load data
infile 'result.csv'
into table resultxt
fields terminated by ','
TRAILING NULLCOLS..........表示如表的字段沒有對應的值時允許為空。
(resultid POSITION(1:8),
website ,
ipport char terminated by ',',
status char terminated by whitespace)
說明:
infile 指數據源文件 這里我們省略了默認的 discardfile result.dsc badfile result.bad
into table resultxt 默認是INSERT,也可以into table resultxt APPEND為追加方式,或REPLACE
terminated by ',' 指用逗號分隔
terminated by whitespace 結尾以空白分隔
(resultid POSITION(1:8),
website ,
ipport char terminated by ',',
status char terminated by whitespace)
說明:
infile 指數據源文件 這里我們省略了默認的 discardfile result.dsc badfile result.bad
into table resultxt 默認是INSERT,也可以into table resultxt APPEND為追加方式,或REPLACE
terminated by ',' 指用逗號分隔
terminated by whitespace 結尾以空白分隔
控制文件中指定插入數據的方式關鍵字
l insert,為缺省方式,在數據裝載開始時要求表為空
l append,在表中追加新記錄
l replace,刪除舊記錄,替換成新裝載的記錄
l truncate,同上
position(m:n)表示該字段是從位置m到位置n。
三、此時我們執行加載:
D:\>sqlldr userid=fancy/testpass control=result.ctl log=resulthis.out
SQL*Loader: Release 8.1.6.0.0 - Production on 星期二 1月 8 10:25:42 2002
(c) Copyright 1999 Oracle Corporation. All rights reserved.
SQL*Loader-941: 在描述表RESULTXT時出現錯誤
ORA-04043: 對象 RESULTXT 不存在
提示出錯,因為數據庫沒有對應的表。
四、在數據庫建立表
create table resultxt
(resultid varchar2(500),
website varchar2(500),
ipport varchar2(500),
status varchar2(500))
五、重新執行加載
D:\>sqlldr userid=fancy/k1i7l6l8 control=result.ctl log=resulthis.out
SQL*Loader: Release 8.1.6.0.0 - Production on 星期二 1月 8 10:31:57 2002
(c) Copyright 1999 Oracle Corporation. All rights reserved.
達到提交點,邏輯記錄計數2
達到提交點,邏輯記錄計數3
已經成功!我們可以通過日志文件來分析其過程:resulthis.out內容如下:
SQL*Loader: Release 8.1.6.0.0 - Production on 星期二 1月 8 10:31:57 2002
(c) Copyright 1999 Oracle Corporation. All rights reserved.
控制文件: result.ctl
數據文件: result.csv
錯誤文件: result.bad
廢棄文件: 未作指定(可廢棄所有記錄)
裝載數: ALL
跳過數: 0
允許的錯誤: 50
綁定數組: 64 行,最大 65536 字節
繼續: 未作指定
所用路徑: 常規
表RESULTXT
已載入從每個邏輯記錄
插入選項對此表INSERT生效
列名 位置 長度 中止 包裝數據類型
------------------------------ ---------- ----- ---- ---- ---------------------
RESULTID FIRST * , CHARACTER
WEBSITE NEXT * , CHARACTER
IPPORT NEXT * , CHARACTER
STATUS NEXT * WHT CHARACTER
表RESULTXT:
3 行載入成功
由於數據錯誤, 0 行沒有載入。
由於所有 WHEN 子句失敗, 0 行沒有載入。
由於所有字段都為空的, 0 行沒有載入。
為結合數組分配的空間: 65016字節(63行)
除綁定數組外的內存空間分配: 0字節
跳過的邏輯記錄總數: 0
讀取的邏輯記錄總數: 3
拒絕的邏輯記錄總數: 0
廢棄的邏輯記錄總數: 0
從星期二 1月 08 10:31:57 2002開始運行
在星期二 1月 08 10:32:00 2002處運行結束
經過時間為: 00: 00: 02.70
CPU 時間為: 00: 00: 00.10(可
六、並發操作
sqlldr userid=/ control=result1.ctl direct=true parallel=true
sqlldr userid=/ control=result2.ctl direct=true parallel=true
sqlldr userid=/ control=result3.ctl direct=true parallel=true
當加載大量數據時(大約超過10GB),最好抑制日志的產生:
SQL>ALTER TABLE RESULTXT nologging;
這樣不產生REDOLOG,可以提高效率。然后在CONTROL文件中load data上面加一行:unrecoverable
此選項必須要與DIRECT共同應用。
在並發操作時,ORACLE聲稱可以達到每小時處理100GB數據的能力!其實,估計能到1-10G就算不錯了,開始可用結構相同的文件,但只有少量數據,成功后開始加載大量數據,這樣可以避免時間的浪費。