mysql优化导入数据_MySQL 批量导入数据优化

--MyISAM表

mysql> show create table test\G

*************************** 1. row ***************************

Table: test

Create Table: CREATE TABLE `test` (

`id` int(11) NOT NULL,

`last_name` char(30) NOT NULL,

`first_name` char(30) NOT NULL,

PRIMARY KEY (`id`),

KEY `name` (`last_name`,`first_name`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1

1 row in set (0.00 sec)

mysql> show keys from test;

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| test  |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |

| test  |          1 | name     |            1 | last_name   | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |

| test  |          1 | name     |            2 | first_name  | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

3 rows in set (0.00 sec)

mysql> alter table test disable keys;

Query OK, 0 rows affected (0.00 sec)

mysql>  load data infile '/var/lib/mysql-files/test_out.txt' into table test charset gbk fields terminated by ',' enclosed by '"';

Query OK, 5 rows affected (0.02 sec)

Records: 5  Deleted: 0  Skipped: 0  Warnings: 0

mysql> alter table test enable keys;

Query OK, 0 rows affected (0.00 sec)

--InnoDB表

导入的数据按照主键的顺序排列;

将unique_checks参数置为0;

mysql> show variables like '%unique%';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| unique_checks | ON    |

+---------------+-------+

1 row in set (0.01 sec)

mysql> set unique_checks = 0;

Query OK, 0 rows affected (0.10 sec)

mysql> show variables like '%unique%';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| unique_checks | OFF   |

+---------------+-------+

1 row in set (0.00 sec)

mysql>  load data infile '/var/lib/mysql-files/test_out.txt' into table test charset gbk fields terminated by ',' enclosed by '"';

Query OK, 5 rows affected (0.00 sec)

Records: 5  Deleted: 0  Skipped: 0  Warnings: 0

将autocommit参数设为0

mysql> show variables like 'autocommit';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| autocommit    | ON    |

+---------------+-------+

1 row in set (0.00 sec)

mysql> set autocommit = 0;

Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'autocommit';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| autocommit    | OFF   |

+---------------+-------+

1 row in set (0.00 sec)

mysql> load data infile '/var/lib/mysql-files/test_out.txt' into table test charset gbk fields terminated by ',' enclosed by '"';

Query OK, 5 rows affected (0.00 sec)

Records: 5  Deleted: 0  Skipped: 0  Warnings: 0

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26506993/viewspace-2121909/,如需转载,请注明出处,否则将追究法律责任。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值