SQL*Loader 笔记 (一) 热身练习

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介:

SQL*Loader是加载数据的主流方法
传统路径:SQLLDR会利用SQL插入的方式加载数据
直径路径:直接格式化数据块

从一个平面文件读取数据,并将其直接写至格式化的数据库块,而绕开整个SQL引擎,同时还能避免redo、undo的生成。要在一个没有任何数据的数据库中充分加载数据,最快的方法就是采用并行直接路径加载

外部表:允许访问操作系统文件,就好像它们是数据库表一样

数据卸载技术

平面文件卸载(flat file unload)
数据泵卸载(data pump unload)

实验1

文件路径
[oracle@node1 mysqlldr]$ pwd
/home/oracle/mysqlldr
[oracle@node1 mysqlldr]$ ll
total 16
-rw-r--r-- 1 oracle oinstall   76 Jul 22 09:53 afiedt.buf
-rw-r--r-- 1 oracle oinstall  173 Jul 22 09:56 demo1.ctl
-rw-r--r-- 1 oracle oinstall 1611 Jul 22 09:57 demo1.log
-rw-r--r-- 1 oracle oinstall  111 Jul 22 09:47 dept.sql

配置文件demo1.ctl
[oracle@node1 mysqlldr]$ cat demo1.ctl
LOAD DATA
INFILE *
INTO TABLE DEPT
FIELDS TERMINATED BY ','
(DEPTNO,DNAME,LOC)
BEGINDATA
10,Sales,Virginia
20,Accounting,Virginia
30,Consulting,Virginia
40,Finance,Virginia
[oracle@node1 mysqlldr]$

创建表脚本
[oracle@node1 mysqlldr]$ cat dept.sql
create table dept
(deptno number(2) constraint dept_pk primary key,
dname varchar2(14),
loc   varchar2(13)
)
/

执行命令
sqlldr userid=alex/alex control=/home/oracle/mysqlldr/demo1.ctl

实验2

配置文件中第四条记录的最后一个字段有很多的字符
[oracle@node1 mysqlldr]$ cat demo2.ctl
LOAD DATA
INFILE *
INTO TABLE DEPT
FIELDS TERMINATED BY ','
(DEPTNO,DNAME,LOC)
BEGINDATA
10,Sales,Virginia
20,Accounting,Virginia
30,Consulting,Virginia
40,Finance,Virginia text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text moretext more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text moretext more text more text more text more text more text more text more text moretext more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text moretext more text more text more text more

清空之前插入的数据
ALEX@devdb1 >truncate table dept;


执行命令
[oracle@node1 mysqlldr]$ sqlldr userid=alex/alex control=/home/oracle/mysqlldr/demo2.ctl


在demo2.log中出现了exceeds maximum length 最大长度不够
[oracle@node1 mysqlldr]$ cat demo2.log

   Column Name                  Position   Len  Term Encl Datatype
------------------------------ ---------- ----- ---- ---- ---------------------
DEPTNO                              FIRST     *   ,       CHARACTER
DNAME                                NEXT     *   ,       CHARACTER
LOC                                  NEXT     *   ,       CHARACTER

Record 4: Rejected - Error on table DEPT, column LOC.
Field in data file exceeds maximum length

Table DEPT:
  3 Rows successfully loaded.
  1 Row 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.

数据只成功加载3条,最后一条没有成功
ALEX@devdb1 >select * from dept;

DEPTNO DNAME                                      LOC
---------- ------------------------------------------ ---------------------------------------
        10 Sales                                      Virginia
        20 Accounting                                 Virginia
        30 Consulting                                 Virginia

demo2.bad文件中记录了加载失败的数据
[oracle@node1 mysqlldr]$ cat demo2.bad
40,Finance,Virginia text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text moretext more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text moretext more text more text more text more text more text more text more text moretext more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text moretext more text more text more text more


修改控制文件demo2.ctl,将char默认的255扩展到1000
[oracle@node1 mysqlldr]$ cat demo2.ctl
LOAD DATA
INFILE *
INTO TABLE DEPT
FIELDS TERMINATED BY ','
(DEPTNO,DNAME ,LOC char(1000))
BEGINDATA
10,Sales,Virginia
20,Accounting,Virginia
30,Consulting,Virginia
40,Finance,Virginia text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text moretext more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text moretext more text more text more text more text more text more text more text moretext more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text more text moretext more text more text more text more

更改表dept的字段loc为varchar2(1000)
ALEX@devdb1 >alter table dept modify loc varchar2(1000);

Table altered.

ALEX@devdb1 >desc dept;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 DEPTNO                                    NOT NULL NUMBER(2)
 DNAME                                              VARCHAR2(14)
 LOC                                                VARCHAR2(1000)

清空表后重新加载

truncate table dept;

$ sqlldr userid=alex/alex control=/home/oracle/mysqlldr/demo2.ctl

验证结果插入成功

select * from dept;

    DEPTNO DNAME
---------- ------------------------------------------
LOC
--------------------------------------------------------------------------------
        10 Sales
Virginia

        20 Accounting
Virginia

        30 Consulting
Virginia


    DEPTNO DNAME
---------- ------------------------------------------
LOC
--------------------------------------------------------------------------------
        40 Finance
Virginia text more text more text more text more text more text more text more t
ext more text more text more text more text more text more text more text more t
ext moretext more text more text more text more text more text more text more te
xt more text more text more text more text more text more text more text more te
xt more text more text more text more text moretext more text more text more tex
t more text more text more text more text moretext more text more text more text
 more text m
ore text more text more text more text more text more text more text
 more text more text more text more text more text more text more text more text

    DEPTNO DNAME
---------- ------------------------------------------
LOC
--------------------------------------------------------------------------------
 more text more text more text more text more text more text more text more text
 moretext more text more text more text more

实验3

配置文件中加入选项“ ”区域中代表一个完整的字段,可忽略其中的,号。

如果字段中含有”,需要用”“来替代
[oracle@node1 mysqlldr]$ cat demo3.ctl
LOAD DATA
INFILE *
INTO TABLE DEPT
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
(DEPTNO,DNAME,LOC)
BEGINDATA
10,Sales,"Virginia,USA"
20,Accounting,"Va,""USA"""
30,Consulting,Virginia
40,Finance,Virginia


ALEX@devdb1 >select * from dept;

    DEPTNO DNAME      LOC
---------- ---------- --------------------
        10 Sales      Virginia,USA   <===字段中包含分隔符 , 
        20 Accounting Va,”USA”    <===字段中包含” 
        30 Consulting Virginia
        40 Finance    Virginia

实验4 使用FILLER参数来过滤掉不需要的列

[oracle@node1 mysqlldr]$ cp demo3.ctl demo4.ctl
[oracle@node1 mysqlldr]$ vi demo4.ctl

LOAD DATA
INFILE *
INTO TABLE DEPT
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
(DEPTNO,DNAME,LOC,dummy1 filler)
BEGINDATA
10,Sales,"Virginia,USA",haha
20,Accounting,"Va,""USA""",haha
30,Consulting,Virginia,haha
40,Finance,Virginia,haha

truncate table dept;

sqlldr userid=alex/alex control=/home/oracle/mysqlldr/demo4.ctl
 
[oracle@node1 mysqlldr]$ cat demo4.log


   Column Name                  Position   Len  Term Encl Datatype
------------------------------ ---------- ----- ---- ---- ---------------------
DEPTNO                              FIRST     *   ,  O(") CHARACTER           
DNAME                                NEXT     *   ,  O(") CHARACTER           
LOC                                  NEXT     *   ,  O(") CHARACTER           
DUMMY1                               NEXT     *   ,  O(") CHARACTER           
  (FILLER FIELD)


[oracle@node1 mysqlldr]$ sqlplus alex/alex

ALEX@devdb1 >select * from dept;

    DEPTNO DNAME           LOC
---------- --------------- ---------------
        10 Sales           Virginia,USA
        20 Accounting      Va,"USA"
        30 Consulting      Virginia
        40 Finance         Virginia

实验5 日期类型的加载

ALEX@devdb1 >alter table dept add last_updated date;

Table altered.

ALEX@devdb1 >desc dept;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 DEPTNO                                    NOT NULL NUMBER(2)
 DNAME                                              VARCHAR2(14)
 LOC                                                VARCHAR2(1000)
 LAST_UPDATED                                       DATE


[oracle@node1 mysqlldr]$ cat demo5.ctl
LOAD DATA
INFILE *
INTO TABLE DEPT
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
(DEPTNO,DNAME,LOC,LAST_UPDATED date 'yyyy-mm-dd')
BEGINDATA
10,Sales,Virginia,2014-05-01
20,Accounting,"Va,""USA""",2014-06-03
30,Consulting,Virginia,2014-07-22
40,Finance,Virginia,2014-07-23

[oracle@node1 mysqlldr]$ sqlldr userid=alex/alex control=/home/oracle/mysqlldr/demo5.ctl

col dname for a15
col loc for a15

select * from dept;

    DEPTNO DNAME           LOC             LAST_UPDATED
---------- --------------- --------------- -------------------
        10 Sales           Virginia        2014/05/01 00:00:00
        20 Accounting      Va,"USA"        2014/06/03 00:00:00
        30 Consulting      Virginia        2014/07/22 00:00:00
        40 Finance         Virginia        2014/07/23 00:00:00

实验6 函数加载

[oracle@node1 mysqlldr]$ cat demo6.ctl
LOAD DATA
INFILE *
INTO TABLE DEPT
REPLACE
FIELDS TERMINATED BY ','
(DEPTNO,
DNAME "upper(:dname)",
LOC "upper(:loc),
LAST_UPDATED date 'yyyy-mm-dd')
BEGINDATA
10,Sales,Virginia,1998-09-01
20,Accounting,Virginia,2002-08-09
30,Consulting,Virginia,2008-08-08
40,Finance,Virginia,2014-06-03

[oracle@node1 mysqlldr]$ sqlldr userid=alex/alex control=demo6.ctl
ALEX@devdb1 >select * from dept;

    DEPTNO DNAME           LOC             LAST_UPDATED
---------- --------------- --------------- -------------------
        10 SALES           VIRGINIA        1998/09/01 00:00:00
        20 ACCOUNTING      VIRGINIA        2002/08/09 00:00:00
        30 CONSULTING      VIRGINIA        2008/08/08 00:00:00
        40 FINANCE         VIRGINIA        2014/06/03 00:00:00

本文转自ICT时空 dbasdk博客,原文链接: SQL*Loader 笔记 (一) 热身练习,如需转载请自行联系原博主。

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
7月前
|
SQL Perl
PL/SQL经典练习
PL/SQL经典练习
|
7月前
|
SQL 关系型数据库 MySQL
【MySQL实战笔记】02.一条SQL更新语句是如何执行的-1
【4月更文挑战第4天】SQL更新语句执行涉及查询和日志模块,主要为`redo log`和`binlog`。`redo log`先写日志再写磁盘,保证`crash-safe`;`binlog`记录逻辑日志,支持所有引擎,且追加写入。执行过程分为执行器查找数据、更新内存和`redo log`(prepare状态)、写入`binlog`、提交事务(`redo log`转commit)。两阶段提交确保日志逻辑一致,支持数据库恢复至任意时间点。
57 0
|
6月前
|
SQL 存储 数据库
技术好文:SQL查询语句基本练习
技术好文:SQL查询语句基本练习
|
6月前
|
SQL 存储 关系型数据库
技术笔记:MYSQL常用基本SQL语句总结
技术笔记:MYSQL常用基本SQL语句总结
39 0
|
7月前
|
SQL 关系型数据库 MySQL
【MySQL系列笔记】常用SQL
常用SQL分为三种类型,分别为DDL,DML和DQL;这三种类型的SQL语句分别用于管理数据库结构、操作数据、以及查询数据,是数据库操作中最常用的语句类型。 在后面学习的多表联查中,SQL是分析业务后业务后能否实现的基础,以及后面如何书写动态SQL,以及完成级联查询的关键。
251 6
|
7月前
|
SQL 存储 关系型数据库
【MySQL系列笔记】SQL优化
SQL优化是通过调整数据库查询、索引、表结构和配置参数等方式,提高SQL查询性能和效率的过程。它旨在减少查询执行时间、减少系统资源消耗,从而提升数据库系统整体性能。优化方法包括索引优化、查询重写、表分区、适当选择和调整数据库引擎等。
292 3
|
7月前
|
SQL 存储 Oracle
《SQL必知必会》个人笔记
《SQL必知必会》个人笔记
46 1
|
7月前
|
SQL 存储 缓存
【MySQL实战笔记】01.一条SQL查询语句是如何执行的?
【4月更文挑战第3天】MySQL执行SQL的流程包括连接器、查询缓存、分析器、优化器和执行器。连接器负责建立连接、权限验证,查询缓存(MySQL 8.0已移除)存储查询结果,分析器解析SQL确保语法正确,优化器选择最佳索引和查询路径,执行器执行查询并管理权限。连接器使用长连接可能导致内存问题,可定期断开或使用`mysql_reset_connection`。注意,更新操作会导致查询缓存失效。
42 3
|
7月前
|
SQL 存储 关系型数据库
【MySQL实战笔记】02.一条SQL更新语句是如何执行的-2
【4月更文挑战第5天】两阶段提交是为确保`redo log`和`binlog`逻辑一致,避免数据不一致。若先写`redo log`, crash后数据可能丢失,导致恢复后状态错误;若先写`binlog`,crash则可能导致重复事务,影响数据库一致性。一天一备相较于一周一备,能缩短“最长恢复时间”,但需权衡额外的存储成本。
51 1
|
7月前
|
SQL 存储 数据库
【数据库SQL server】自学终极笔记
【数据库SQL server】自学终极笔记
214 0