MySQL 之 LOAD DATA INFILE 快速导入数据
Posted LEROI
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL 之 LOAD DATA INFILE 快速导入数据相关的知识,希望对你有一定的参考价值。
SELECT INTO OUTFILE
> help select; Name: ‘SELECT‘ Description: Syntax: SELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [MAX_STATEMENT_TIME = N] [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] select_expr [, select_expr ...] [FROM table_references [PARTITION partition_list] [WHERE where_condition] [GROUP BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]] [HAVING where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC], ...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] [PROCEDURE procedure_name(argument_list)] [INTO OUTFILE ‘file_name‘ [CHARACTER SET charset_name] export_options | INTO DUMPFILE ‘file_name‘ | INTO var_name [, var_name]] [FOR UPDATE | LOCK IN SHARE MODE]]
举例:
> select * from e; +------+-------+-------+ | id | fname | lname | +------+-------+-------+ | 1669 | Jim | Smith | | 337 | Mary | Jones | | 2005 | Linda | Black | +------+-------+-------+ > select * from e into outfile "/data/mysql/e.sql";
或 > select * into outfile "/data/mysql/e.sql" from e; # cat e.sql 1669 Jim Smith 337 Mary Jones 2005 Linda Black
可以看到,select...into outfile 的结果只包含了表数据,默认以 Tab 分隔,也可指定分隔符:
> select * from e into outfile "/data/mysql/e.sql" fields terminated by ‘,‘; # cat e.sql 1669,Jim,Smith 337,Mary,Jones 2005,Linda,Black
注意:outfile ‘/path/file‘,中的 path 需要有mysql的权限,否则会报错:
> select * from t into outfile "/root/backup/mysql/t.sql"; ERROR 1 (HY000): Can‘t create/write to file ‘/root/backup/mysql/t.sql‘ (Errcode: 13 - Permission denied)
LOAD DATA INFILE
LOAD DATA INFILE 语句以非常高的速度从文本文件中读取行到表中。
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE ‘file_name‘ [REPLACE | IGNORE] INTO TABLE tbl_name [PARTITION (partition_name,...)] [CHARACTER SET charset_name] [{FIELDS | COLUMNS} [TERMINATED BY ‘string‘] [[OPTIONALLY] ENCLOSED BY ‘char‘] [ESCAPED BY ‘char‘] ] [LINES [STARTING BY ‘string‘] [TERMINATED BY ‘string‘] ] [IGNORE number {LINES | ROWS}] [(col_name_or_user_var,...)] [SET col_name = expr,...]
举例:
> delete from e;
> load data infile "/data/mysql/e.sql" into table e fields terminated by ‘,‘; Query OK, 3 rows affected (0.01 sec) Records: 3 Deleted: 0 Skipped: 0 Warnings: 0 > select * from e; +------+-------+-------+ | id | fname | lname | +------+-------+-------+ | 1669 | Jim | Smith | | 337 | Mary | Jones | | 2005 | Linda | Black | +------+-------+-------+
因为我们前面指定的分隔符是 ‘,‘,load data 时也要指定分隔符,否则也会报错:
> load data infile "/data/mysql/e.sql" into table e; ERROR 1265 (01000): Data truncated for column ‘id‘ at row 1
如果数据被某种符号封闭着,需要指定 ‘ fields enclosed by ’ :
# cat e.sql "1669" "Jim" "Smith"
> load data infile "/data/mysql/e.sql" into table e;
ERROR 1366 (HY000): Incorrect integer value: ‘"1669"‘ for column ‘id‘ at row 1
> load data infile "/data/mysql/e.sql" into table e fields enclosed by ‘"‘;
Query OK, 1 row affected (0.01 sec)
Records: 1 Deleted: 0 Skipped: 0 Warnings: 0
如上所示,数据被双引号封闭着,如果直接执行,会报错。
另外还有一些格式上的限制,如 LINES TERMINATED BY ‘string‘ ,指定 file 的换行符,如 ‘\n’ 。