Sqlldr把文字檔案匯入到ORACLE中
目錄
1 檢視命令引數
$ sqlldr --help
有效的關鍵字: userid -- ORACLE 使用者名稱/口令 control -- 控制檔名 log -- 日誌檔名 bad -- 錯誤檔名 data -- 資料檔名 discard -- 廢棄檔名 discardmax -- 允許廢棄的檔案的數目 (全部預設) skip -- 要跳過的邏輯記錄的數目 (預設 0) load -- 要載入的邏輯記錄的數目 (全部預設) errors -- 允許的錯誤的數目 (預設 50) rows -- 常規路徑繫結陣列中或直接路徑儲存資料間的行數 (預設: 常規路徑 64, 所有直接路徑) bindsize -- 常規路徑繫結陣列的大小 (以位元組計) (預設 256000) silent -- 執行過程中隱藏訊息 (標題,反饋,錯誤,廢棄,分割槽) direct -- 使用直接路徑 (預設 FALSE) parfile -- 引數檔案: 包含引數說明的檔案的名稱 parallel -- 執行並行載入 (預設 FALSE) file -- 要從以下物件中分配區的檔案 skip_unusable_indexes -- 不允許/允許使用無用的索引或索引分割槽 (預設 FALSE) skip_index_maintenance -- 沒有維護索引, 將受到影響的索引標記為無用 (預設 FALSE) commit_discontinued -- 提交載入中斷時已載入的行 (預設 FALSE) readsize -- 讀取緩衝區的大小 (預設 1048576) external_table -- 使用外部表進行載入; NOT_USED, GENERATE_ONLY, EXECUTE (預設 NOT_USED) columnarrayrows -- 直接路徑列陣列的行數 (預設 5000) streamsize -- 直接路徑流緩衝區的大小 (以位元組計) (預設 256000) multithreading -- 在直接路徑中使用多執行緒 resumable -- 啟用或禁用當前的可恢復會話 (預設 FALSE) resumable_name -- 有助於標識可恢復語句的文字字串 resumable_timeout -- RESUMABLE 的等待時間 (以秒計) (預設 7200) date_cache -- 日期轉換快取記憶體的大小 (以條目計) (預設 1000) no_index_errors -- 出現任何索引錯誤時中止載入 (預設 FALSE) PLEASE NOTE: 命令列引數可以由位置或關鍵字指定 。前者的例子是 'sqlldr scott/tiger foo'; 後一種情況的一個示例是 'sqlldr control=foo userid=scott/tiger'。位置指定引數的時間必須早於 但不可遲於由關鍵字指定的引數。例如, 允許'sqlldr scott/tiger control=foo logfile=log', 但是 不允許'sqlldr scott/tiger control=foo log', 即使 引數'log' 的位置正確
2 檢視需要匯入的文字內容
2.1 檢視文字的行數
$ wc -l test-sqlldr.csv 9415010 test-sqlldr.csv
2.2 檢視文字的大小
$ du -sh test-sqlldr.csv 2.8G test-sqlldr.csv
2.3 檢視文字的內容
$ head -n 2 test-sqlldr.csv K1|!?|K2|!?|GSMC|!?|JGLX|!?|KGS|!?|RZS|!?|ISJW|!?|SYR|!?|SYRLX|!?|CGZB|!?|TZLX|!?|KGLJS|!?|RZLX|!?|BSM |!?||!?|北京羅馬科技有限公司|!?|有限責任公司(臺港澳法人獨資)|!?|1|!?|3|!?|是|!?|馬闖|!?|高階管理人員|!?||!?||!?||!?|總經理,執行董事,法定代表人/負責人/執行事務合夥人|!?|V1!ihMJSIrj/CeN5d2an64ys/iGqJnBLgEGHM9HVY1Xt91K5Pia73LMxGYHJewNqFBff6slsPFRHkRt<n>x0pV/J58JQ==
3 編寫導資料的CTL檔案
3.1 檢視CTL檔案
$ cat load-to-oracle.ctl load data CHARACTERSET UTF8 infile "/dazhihui/databack/b.csv" append into table xiaoxu.test_sqlldr fields terminated by '|!?|' trailing nullcols ( K1, K2, GSMC, JGLX, KGS, RZS, ISJW, SYR, SYRLX, CGZB, TZLX, KGLJS, RZLX, BSM )
infile 後面是檔案的路徑
xiaoxu.test_sqlldr 需要的匯入的表明
fields terminated by 後面跟著分隔符
trailing nullcols 欄位的列表,需要全部大寫與ORACLE保持一致,如果oracle欄位小寫需要改成小寫
append 是追加的形式,也可以是insert (為預設方式,在資料裝載開始時要求表為空),append(在表中追加新記錄) ,
replace (刪除舊記錄(用 delete from table 語句),替換成新裝載的記錄),truncate(刪除舊記錄(用 truncate table 語句),替換成新裝載的記錄)
3.2 檢視SHELL指令碼
$ cat import-data.sh #!bin/bash
sqlldr username/[email protected]:1521/etlpdb control=load.ctl log=test-log.log bad=bad.log errors=5000 rows=1000 bindsize=10485760
4 檢視執行的效率
4.1 檢視匯入到ORACLE資料的效率
$ sh import-data.sh
************ Commit point reached - logical record count 9414355 Commit point reached - logical record count 9415010
Table XIAOXU.TEST_SQLLDR: 9415010 Rows successfully loaded.
Check the log file: test-log.log for more information about the load.
real 10m15.357s user 1m27.074s sys 0m2.278s
在以上可以看出匯入進9415010行資料,用時 10m15.357s
4.2 檢視統計的日誌
$ cat test-log.log
SQL*Loader: Release 12.2.0.1.0 - Production on Fri Nov 9 17:46:51 2018
Copyright (c) 1982, 2017, Oracle and/or its affiliates. All rights reserved.
Control File: load.ctl Character Set UTF8 specified for all input.
Data File: /dazhihui/databack/b.csv Bad File: bad.log Discard File: none specified (Allow all discards)
Number to load: ALL Number to skip: 0 Errors allowed: 5000 Bind array: 1000 rows, maximum of 10485760 bytes Continuation: none specified Path used: Conventional
Table XIAOXU.TEST_SQLLDR, loaded from every logical record. Insert option in effect for this table: APPEND TRAILING NULLCOLS option in effect
Column Name Position Len Term Encl Datatype ------------------------------ ---------- ----- ---- ---- --------------------- K1 FIRST * CHARACTER Terminator string : '|!?|' K2 NEXT * CHARACTER Terminator string : '|!?|' GSMC NEXT * CHARACTER Terminator string : '|!?|' JGLX NEXT * CHARACTER Terminator string : '|!?|' KGS NEXT * CHARACTER Terminator string : '|!?|' RZS NEXT * CHARACTER Terminator string : '|!?|' ISJW NEXT * CHARACTER Terminator string : '|!?|' SYR NEXT * CHARACTER Terminator string : '|!?|' SYRLX NEXT * CHARACTER Terminator string : '|!?|' CGZB NEXT * CHARACTER Terminator string : '|!?|' TZLX NEXT * CHARACTER Terminator string : '|!?|' KGLJS NEXT * CHARACTER Terminator string : '|!?|' RZLX NEXT * CHARACTER Terminator string : '|!?|' BSM NEXT * CHARACTER Terminator string : '|!?|'
Table XIAOXU.TEST_SQLLDR: 9415010 Rows successfully loaded. 0 Rows not loaded due to data errors. 0 Rows not loaded because all WHEN clauses were failed. 0 Rows not loaded because all fields were null.
Space allocated for bind array: 3612000 bytes(1000 rows) Read buffer bytes:10485760
Total logical records skipped: 0 Total logical records read: 9415010 Total logical records rejected: 0 Total logical records discarded: 0
Run began on Fri Nov 09 17:46:51 2018 Run ended on Fri Nov 09 17:57:06 2018
Elapsed time was: 00:10:15.28 CPU time was: 00:01:29.33
5 效率總結
在以上的結果中可以看出效率並不是很高,按以上計算9415010/615 = 15308 / S