转:

http://venublog.com/2007/11/07/load-data-infile-performance/

I often noticed that people complain about the LOAD DATA performance when loading the table with large number of rows of data. Even today I saw a case where the LOAD DATA on a simple 3 column table with about 5 million rows taking ~15 minutes of time. This is because the server did not had any tuning in regards to bulk insertion.

Consider the following simple MyISAM table on Redhat Linux 32-bit.

 
 
 

Shell

 
1
2
3
4
5
6
7
8
 
CREATE TABLE load1 (
  `col1` varchar(100) NOT NULL default '',
  `col2` int(11) default NULL,
  `col3` char(1) default NULL,
  PRIMARY KEY  (`col1`)
) TYPE=MyISAM;
 

The table has a string key column. Here is the data file(download here) that I used it for testing:

 
 
 

Shell

 
1
2
3
4
5
6
7
 
[vanugant@escapereply:t55 tmp]$ wc loaddata.csv
  5164946   5164946 227257389 loaddata.csv
[vanugant@escapereply:t55 tmp]$ ls -alh loaddata.csv
-rw-r--r--  1 vanugant users 217M Nov  6 14:42 loaddata.csv
[vanugant@escapereply:t55 tmp]$
 

Here is the default mysql system variables related to LOAD DATA:

 
 
 

Shell

 
1
2
3
4
5
6
7
8
9
10
 
mysql> show variables;
+-------------------------+---------+
| Variable_name              | Value   |
+-------------------------+---------+
| bulk_insert_buffer_size   | 8388608 |
| myisam_sort_buffer_size   | 16777216 |
| key_buffer_size            | 33554432 |
+-------------------------+----------+
 

and here is the actual LOAD DATA query to load all ~5m rows (~256M of data) to the table and its timing.

 
 
 

Shell

 
1
2
3
4
5
 
mysql> LOAD DATA INFILE '/home/vanugant/tmp/loaddata.csv' IGNORE INTO TABLE load1 FIELDS TERMINATED BY ',';
Query OK, 4675823 rows affected (14 min 56.84 sec)
Records: 5164946  Deleted: 0  Skipped: 489123  Warnings: 0
 

Now, lets experiment by disabling the keys in the table before running the LOAD DATA:

 
 
 

Shell

 
1
2
3
4
5
6
7
8
9
10
11
 
mysql> SET SESSION BULK_INSERT_BUFFER_SIZE=314572800;
Query OK, 0 rows affected (0.00 sec)
 
mysql> alter table load1 disable keys;
Query OK, 0 rows affected (0.00 sec)
 
mysql> LOAD DATA INFILE '/home/vanugant/tmp/loaddata.csv' IGNORE INTO TABLE load1 FIELDS TERMINATED BY ',';
Query OK, 4675823 rows affected (13 min 47.50 sec)
Records: 5164946  Deleted: 0  Skipped: 489123  Warnings: 0
 

No use, just 1% increase or same…., now lets set the real MyISAM values… and try again…

 
 
 

Shell

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
mysql> SET SESSION BULK_INSERT_BUFFER_SIZE=256217728;
Query OK, 0 rows affected (0.00 sec)
 
mysql> set session MYISAM_SORT_BUFFER_SIZE=256217728;
Query OK, 0 rows affected (0.00 sec)
 
mysql> set global KEY_BUFFER_SIZE=256217728;
Query OK, 0 rows affected (0.05 sec)
 
mysql> alter table load1 disable keys;
Query OK, 0 rows affected (0.00 sec)
 
mysql> LOAD DATA INFILE '/home/vanugant/tmp/loaddata.csv' IGNORE INTO TABLE load1 FIELDS TERMINATED BY ',';
Query OK, 4675823 rows affected (1 min 55.05 sec)
Records: 5164946  Deleted: 0  Skipped: 489123  Warnings: 0
 
mysql> alter table load1 enable keys;
Query OK, 0 rows affected (0.00 sec)
 

Wow…thats almost 90% increase in the performance. So, disabling the keys in MyISAM is not just the key, but tuning the buffer size does play role based on the input data.

For the same case with Innodb, here is the status by adjusting the Innodb_buffer_pool_size=1G andInnodb_log_file_size=256M along with innodb_flush_logs_at_trx_commit=1.

 
 
 

Shell

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
mysql> show variables like '%innodb%size';
+---------------------------------+------------+
| Variable_name                   | Value      |
+---------------------------------+------------+
| innodb_additional_mem_pool_size | 26214400   |
| innodb_buffer_pool_size         | 1073741824 |
| innodb_log_buffer_size          | 8388608    |
| innodb_log_file_size            | 268435456  |
+---------------------------------+------------+
 
mysql> LOAD DATA INFILE '/home/vanugant/tmp/loaddata.csv' IGNORE INTO TABLE load1 FIELDS TERMINATED BY ',';
Query OK, 4675823 rows affected (2 min 37.53 sec)
Records: 5164946  Deleted: 0  Skipped: 489123  Warnings: 0
 

With innodb_flush_logs_at_trx_commit=2, innodb_flush_method=O_DIRECT and innodb_doublewrite=0; it will be another 40% difference (use all these variables with caution, unless you know what you are doing)

 
 
 

Shell

 
1
2
3
4
5
 
mysql> LOAD DATA INFILE '/home/vanugant/tmp/loaddata.csv' IGNORE INTO TABLE load1 FIELDS TERMINATED BY ',';
Query OK, 4675823 rows affected (1 min 53.69 sec)
Records: 5164946  Deleted: 0  Skipped: 489123  Warnings: 0

LOAD DATA INFILE – performance case study的更多相关文章

  1. LOAD DATA INFILE Syntax--官方

    LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name' [REPLACE | IGNORE] INTO TABLE tbl_n ...

  2. Data Visualization – Banking Case Study Example (Part 1-6)

    python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...

  3. Mysql load data infile 导入数据出现:Data truncated for column

    [1]Mysql load data infile 导入数据出现:Data truncated for column .... 可能原因分析: (1)数据库表对应字段类型长度不够或修改为其他数据类型( ...

  4. Mysql load data infile 命令导入含中文csv源数据文件 【错误代码 1300】

    [1]Load data infile 命令导入含中文csv源数据文件 报错:Invalid utf8 character string: '??֧' (1)问题现象 csv格式文件源数据: 导入SQ ...

  5. Mysql load data infile 命令格式

    [1]Linux系统环境下 LOAD DATA INFILE /usr/LOCAL/lib/ubcsrvd/datacsv/201909_source.csv INTO TABLE np_cdr_20 ...

  6. Mysql 命令 load data infile 权限问题

    [1]Mysql命令load data infile 执行权限问题 工作中,经常会遇到往线上环境mysql数据库批量导入源数据的场景. 针对这个场景问题,mysql有一个很高效的命令:load dat ...

  7. mysql load data infile的使用 和 SELECT into outfile备份数据库数据

    LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name.txt' [REPLACE | IGNORE] INTO TABLE t ...

  8. SQL基本语句(3) LOAD DATA INFILE

    使用LOAD语句批量录入数据 语法: LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name' [REPLACE | IGNOR ...

  9. mysql导入数据load data infile用法

    mysql导入数据load data infile用法 基本语法: load data [low_priority] [local] infile 'file_name txt' [replace | ...

随机推荐

  1. Map中如何把没有定义操作符<的类作为key

    Map中如何把没有定义操作符<的类作为key 其实,为了实现快速查找,map内部本身就是按序存储的(比如红黑树).在我们插入<key, value>键值对时,就会按照key的大小顺序 ...

  2. STL中set底层实现方式

    Q:STL中set底层实现方式? 为什么不用hash? A: 第一个问题:set底层实现方式为RB树(即红黑树). 第二个问题: 首先set,不像map那样是key-value对,它的key与valu ...

  3. hdu 1515 Anagrams by Stack

    题解: 第一:两个字符不相等(即栈顶字符与目标字符不相等):这种情况很容易处理,将匹配word的下一个字符入栈,指针向后挪已为继续递归. 第二:两个字符相等(即栈顶字符与目标字符相等):这种情况有两种 ...

  4. 使用xshell链接本地虚拟机中的Linux

    昨天想在自己机器上安装一下Linux,并使用xshell访问,可是费了很长时间,在xshell端都提示“Could not connect to '192.168.54.100' (port 22): ...

  5. 使用qmake生成Makefile

    Qmake自动生成Makefile 手动写Makefile是一件痛苦的事情,稍不小心就会出错,不过qmake可以让你脱离苦海 qmake可以根据你提供的.pro文件,生成Makefile不过他可比Ma ...

  6. A Bug

    A Bug Time Limit:   1000MS       Memory Limit:   65535KB Submissions:   231       Accepted:    Descr ...

  7. C# Server.MapPath()

     ./当前目录 /网站主目录../上层目录~/网站虚拟目录 如果当前的网站目录为E:\wwwroot   应用程序虚拟目录为E:\wwwroot\company 浏览的页面路径为E:\wwwroot\ ...

  8. CCNU-线段树练习题-A-单点更新1

    A - 单点更新1 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Des ...

  9. SQL[连载2]语法及相关实例

    SQL[连载2]语法及相关实例 SQL语法 数据库表 一个数据库通常包含一个或多个表.每个表由一个名字标识(例如:"Websites"),表包含带有数据的记录(行). 在本教程中, ...

  10. Java深入学习之--初始化

    目录 1.方法重载 2.默认构造器 3.this关键字 4.static关键字 5.初始化 1.方法重载 java中方法重载的意思是在同一个类中可以存在方法名相同的方法,而方法的参数类型不同,即使两个 ...