最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

合理使用mysql中的loaddatainfile导入数据_MySQL

来源:动视网 责编:小采 时间:2020-11-09 19:07:24
文档

合理使用mysql中的loaddatainfile导入数据_MySQL

合理使用mysql中的loaddatainfile导入数据_MySQL:bitsCN.com 基本语法:load data [low_priority] [local] infile 'file_name txt' [replace | ignore]into table tbl_name[fields[terminated by't'][OPTIONALLY] enclosed by ''][escaped by'' ]
推荐度:
导读合理使用mysql中的loaddatainfile导入数据_MySQL:bitsCN.com 基本语法:load data [low_priority] [local] infile 'file_name txt' [replace | ignore]into table tbl_name[fields[terminated by't'][OPTIONALLY] enclosed by ''][escaped by'' ]


bitsCN.com

基本语法:

load data [low_priority] [local] infile 'file_name txt' [replace | ignore]
into table tbl_name
[fields
[terminated by't']
[OPTIONALLY] enclosed by '']
[escaped by'' ]]
[lines terminated by'n']
[ignore number lines]
[(col_name, )]

load data infile语句从一个文本文件中以很高的速度读入一个表中。使用这个命令之前,mysqld进程(服务)必须已经在运行。为了安全原因,当读取位于服务器上的文本文件时,文件必须处于数据库目录或可被所有人读取。另外,为了对服务器上文件使用load data infile,在服务器主机上你必须有file的权限。


把千万级甚至亿级的数据写入mysql,实在是一件很让人头痛的事情。
load data local infile貌似是最快的方法了, 可是load一个亿级的文件,仍然需要数十分钟。

如果有主从结构的话,在主服务器上面load数十分钟,文件数据会写入binary log,再传输到从服务器,
然后从服务器也需要数十分钟来做load操作,如果从服务器只有一个sql_thread来执行binary log,那么在这数十分钟内,
从服务器就只能做这一个load操作,无法对后续的sql语句做出响应,导致主从之间的延迟有数十分钟。

而且,如果load了20分钟,www.111cn.net然后发现数据源有问题,需要取消load,那么mysql至少还要20分钟才能回滚成功…
这个时候非常无奈。

所以有必要把这种大文件拆分成若干个小文件,然后分别load.

下面给出一些测试数据:

[root@yw-0-0 huarong]# wc -l cfi.txt
204227 cfi.txt, 行数20M
有九个字段,varchar(255),没有key。
文件大小 4,078,099,848 (3.8G),每行平均195字节。

t1.sh 直接load,作用是预热。
这个时间数据竟然丢失了...

innodb t1.sh 再次直接load
time mysql test -e "load data local infile '/d01/huarong/cfi.txt' into table cfi"
[root@yw-0-0 huarong]# ./t1.sh
real 6m4.720s
user 0m0.492s
sys 0m2.213s

innodb t2.sh不写binlog.
time mysql test -e "set foreign_key_checks=0; set sql_log_bin=0; set unique_checks=0; load data local infile '/d01/huarong/cfi.txt' into table cfi" www.111cn.net
[root@yw-0-0 huarong]# ./t2.sh
real 5m3.9s
user 0m0.586s
sys 0m2.788s

innodb t3.sh fifo,每次load 1M行数据。
wget http://www.maatkit.org/get/mk-fifo-split
perl ./mk-fifo-split ./cfi.txt --fifo /tmp/cfi.fifo --lines 1000000
while [ -e /tmp/cfi.fifo ]; do
time mysql test -e "set foreign_key_checks=0; set sql_log_bin=0; set unique_checks=0; load data local infile '/tmp/cfi.fifo' into table cfi"
sleep 1;
done

real: 5m25.84s
user: 0m2.197s
sys: 0m11.244s


myisam: t2.sh不写binlog
real 3m24.838s
user 0m0.626s
sys 0m2.939s

更多详细内容请查看:http://www.111cn.net/database/mysql/56628.htm

bitsCN.com

文档

合理使用mysql中的loaddatainfile导入数据_MySQL

合理使用mysql中的loaddatainfile导入数据_MySQL:bitsCN.com 基本语法:load data [low_priority] [local] infile 'file_name txt' [replace | ignore]into table tbl_name[fields[terminated by't'][OPTIONALLY] enclosed by ''][escaped by'' ]
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top